use this as a gateway to detect a users accepted language and for example
redirect him to the right page.
“detect user language” makes use of the built-in PHP functions
strtolower( ),
strpos( ) and
preg_match( ).
001 <?php
002
003 function lixlpixel_get_env_var($Var)
004 {
005 if(empty($GLOBALS[$Var]))
006 {
007 $GLOBALS[$Var] = (!empty($GLOBALS['_SERVER'][$Var])) ?
008 $GLOBALS['_SERVER'][$Var] :
009 (!empty($GLOBALS['HTTP_SERVER_VARS'][$Var])) ?
010 $GLOBALS['HTTP_SERVER_VARS'][$Var] : '';
011 }
012 }
013
014 function lixlpixel_detect_lang()
015 {
016 // Detect HTTP_ACCEPT_LANGUAGE & HTTP_USER_AGENT.
017 lixlpixel_get_env_var('HTTP_ACCEPT_LANGUAGE');
018 lixlpixel_get_env_var('HTTP_USER_AGENT');
019
020 $_AL=strtolower($GLOBALS['HTTP_ACCEPT_LANGUAGE']);
021 $_UA=strtolower($GLOBALS['HTTP_USER_AGENT']);
022
023 // Try to detect Primary language if several languages are accepted.
024 foreach($GLOBALS['_LANG'] as $K)
025 {
026 if(strpos($_AL, $K) === 0)
027 return $K;
028 }
029
030 // Try to detect any language if not yet detected.
031 foreach($GLOBALS['_LANG'] as $K)
032 {
033 if(strpos($_AL, $K) !== false)
034 return $K;
035 }
036 foreach($GLOBALS['_LANG'] as $K)
037 {
038 if(preg_match("/[\[\( ]{$K}[;,_\-\)]/",$_UA))
039 return $K;
040 }
041
042 // Return default language if language is not yet detected.
043 return $GLOBALS['_DLANG'];
044 }
045
046 // Define default language.
047 $GLOBALS['_DLANG']='en';
048
049 // Define all available languages.
050 // WARNING: uncomment all available languages
051
052 $GLOBALS['_LANG'] = array(
053 'af', // afrikaans.
054 'ar', // arabic.
055 'bg', // bulgarian.
056 'ca', // catalan.
057 'cs', // czech.
058 'da', // danish.
059 'de', // german.
060 'el', // greek.
061 'en', // english.
062 'es', // spanish.
063 'et', // estonian.
064 'fi', // finnish.
065 'fr', // french.
066 'gl', // galician.
067 'he', // hebrew.
068 'hi', // hindi.
069 'hr', // croatian.
070 'hu', // hungarian.
071 'id', // indonesian.
072 'it', // italian.
073 'ja', // japanese.
074 'ko', // korean.
075 'ka', // georgian.
076 'lt', // lithuanian.
077 'lv', // latvian.
078 'ms', // malay.
079 'nl', // dutch.
080 'no', // norwegian.
081 'pl', // polish.
082 'pt', // portuguese.
083 'ro', // romanian.
084 'ru', // russian.
085 'sk', // slovak.
086 'sl', // slovenian.
087 'sq', // albanian.
088 'sr', // serbian.
089 'sv', // swedish.
090 'th', // thai.
091 'tr', // turkish.
092 'uk', // ukrainian.
093 'zh' // chinese.
094 );
095
096 // Example Implementations
097
098 // Redirect to the correct location.
099 //header('location: http://www.your_site.com/index_'.lixlpixel_detect_lang().'.php');
100
101 // simply display the detected language
102 echo '<p>the language detected is: '.lixlpixel_detect_lang().'</p>';
103
104 ?>