Jump to content


Photo

PHP "hello" in different languages ???


  • Please log in to reply
3 replies to this topic

#1 j4mes_bond25

j4mes_bond25
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 17 June 2006 - 01:43 PM

I've been trying to display a simple & short message in viewer's own language for which I'm using the following script, right at the top of their screen i.e. just above the banner (in my website on: members.lycos.co.uk/darsh25/ .................. select "contact.php"

Although, in my browser, this script identifies that my browser being set to "English" & hence displays "Hello", as it can be seen from:

<?php

function lixlpixel_get_env_var($Var)
{
     if(empty($GLOBALS[$Var]))
     {
         $GLOBALS[$Var]=(!empty($GLOBALS['_SERVER'][$Var]))?
         $GLOBALS['_SERVER'][$Var] : (!empty($GLOBALS['HTTP_SERVER_VARS'][$Var])) ? $GLOBALS['HTTP_SERVER_VARS'][$Var]:'';
     }
}

function lixlpixel_detect_lang()
{
     // Detect HTTP_ACCEPT_LANGUAGE & HTTP_USER_AGENT.
     lixlpixel_get_env_var('HTTP_ACCEPT_LANGUAGE');
     lixlpixel_get_env_var('HTTP_USER_AGENT');

     $_AL=strtolower($GLOBALS['HTTP_ACCEPT_LANGUAGE']);
     $_UA=strtolower($GLOBALS['HTTP_USER_AGENT']);
  
     // Try to detect Primary language if several languages are accepted.
     foreach($GLOBALS['_LANG'] as $K)
     {
         if(strpos($_AL, $K)===0)
         return $K;
     }
  
     // Try to detect any language if not yet detected.
     foreach($GLOBALS['_LANG'] as $K)
     {
         if(strpos($_AL, $K)!==false)
         return $K;
     }
     foreach($GLOBALS['_LANG'] as $K)
     {
         if(preg_match("/[\[\( ]{$K}[;,_\-\)]/",$_UA))
         return $K;
     }

     // Return default language if language is not yet detected.
     return $GLOBALS['_DLANG'];
}

// Define default language.
$GLOBALS['_DLANG']='en';

// Define all available languages.
// WARNING: uncomment all available languages

$GLOBALS['_LANG'] = array(
'af', // afrikaans.
'ar', // arabic.
'bg', // bulgarian.
'ca', // catalan.
'cs', // czech.
'da', // danish.
'de', // german.
'el', // greek.
'en', // english.
'es', // spanish.
'et', // estonian.
'fi', // finnish.
'fr', // french.
'gl', // galician.
'he', // hebrew.
'hi', // hindi.
'hr', // croatian.
'hu', // hungarian.
'id', // indonesian.
'it', // italian.
'ja', // japanese.
'ko', // korean.
'ka', // georgian.
'lt', // lithuanian.
'lv', // latvian.
'ms', // malay.
'nl', // dutch.
'no', // norwegian.
'pl', // polish.
'pt', // portuguese.
'ro', // romanian.
'ru', // russian.
'sk', // slovak.
'sl', // slovenian.
'sq', // albanian.
'sr', // serbian.
'sv', // swedish.
'th', // thai.
'tr', // turkish.
'uk', // ukrainian.
'zh' // chinese.
);

// Redirect to the correct location.

//header('location: [a href=\"http://www.your_site.com/index_'.lixlpixel_detect_lang().'.php');\" target=\"_blank\"]http://www.your_site.com/index_'.lixlp...#39;.php');[/a] // Example Implementation

if ($GLOBALS['_LANG'] = 'en') {
echo '<span class="colorTextRed">"Hello"';
} 
else if ($GLOBALS['_LANG'] = 'fr') {
echo '<span class="colorTextRed">"Bonjour"';
}
else {
echo '<span class="colorTextRed">"Hello"';
}

//echo '<span class="colorTextRed">The Language detected is: '.lixlpixel_detect_lang(); // For Demonstration
?> 

I managed to get this script from somewhere over the Internet, however, I added "if.....else" right at the end, since I want this script identify the browser language (or something like that) & then based on the language identified, I wish to simply display the equivalent of "Hello" in user's own language.

I tried changing my browser's language to French, in order to TEST this script as if it displays "Bonjour" but that didn't work.

Could anyone around help me ???

#2 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 17 June 2006 - 02:53 PM

Try this:

<?php

function getLang() {
   $pref=array();
   foreach(split(',', $_SERVER["HTTP_ACCEPT_LANGUAGE"]) as $lang) {
       if (preg_match('/^([a-z]+).*?(?:;q=([0-9.]+))?/i', $lang.';q=1.0', $split)) {
           $pref[]=strtolower($split[1]);
       }
   }
   ksort($pref);
   return $pref;
}

$lang = getLang();

switch ($lang[0]) {

case 'fr':
   echo 'Bonjour';
   break;

case 'de':
   echo 'Hallo';
   break;

case 'es':
   echo 'Hola';
   break;
   
default:
   echo 'Hello';
   break;

}

?>

~ D Kuang

#3 aebstract

aebstract
  • Members
  • PipPipPip
  • Advanced Member
  • 1,105 posts

Posted 17 June 2006 - 03:01 PM

I don't think what you said would work though, poirot. I mean for hello yes, and if thats all he wants it will.. but if he wants it to be more advanced, like eventually translating his whole website depending on language?

There is an area of the mind that could be called unsane, beyond sanity, and yet
not insane. Think of a circle with a fine split in it. At one end there's
insanity. You go around the circle to sanity, and on the other end of the
circle, close to insanity, but not insanity, is unsanity.


#4 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 17 June 2006 - 03:10 PM

[!--quoteo(post=384988:date=Jun 17 2006, 08:01 AM:name=aebstract)--][div class=\'quotetop\']QUOTE(aebstract @ Jun 17 2006, 08:01 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I don't think what you said would work though, poirot. I mean for hello yes, and if thats all he wants it will.. but if he wants it to be more advanced, like eventually translating his whole website depending on language?
[/quote]
No problem.

getLang() will return an array with the browser's languages, then he or she can use that and display different content based on them if he or she wants to.

But I think he needs that for greetings only... Besides, if he needs to translate the whole site; sure this is a start only - PHP can't translate the whole thing.
~ D Kuang




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users