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: http://www.your_site.com/index_'.lixlpixel_detect_lang().'.php'); // 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 ???

Recommended Answers

All 3 Replies

Have you tried setting the system language to french?

You may want to consult the documentation on your language detect features on php.net. (The comments there are especially helpful.)

There isn't only one implementation of language in HTTP Headers, and Language is Optional.

In addition to accept-language theres also content-language, which isn't predefined in php.
Language can also be appended to the content-encoding header.

Different browsers may prefer different ways of implementing http 1.0 and 1.1.
What I would do is download a "network protocol sniffer" and take a look at the headers being sent back and forth on differnet browsers.

For the purpose of your script, you can easily test it by setting $_SEVER to what ever language you want at the top of the script.
You can also do this for $_GLOBALS if this is where your script retrieves the http accept-language header.

Have you tried setting the system language to french?

You may want to consult the documentation on your language detect features on php.net. (The comments there are especially helpful.)

I only changed language through my browser's Tools >> Language >> French. Wouldn't that make the script detecting French language itself ???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.