information
|detect user language
| see demonstration of detect user languageuse this as a gateway to detect a users accepted language and for example redirect him to the right page.
full source of detect user language [ line 1 - 89 ] | download detect user language
| 1 | <?php |
| 2 | function lixlpixel_get_env_var($Var) |
| 3 | { |
| 4 | if(empty($GLOBALS[$Var])) |
| 5 | { |
| 6 | $GLOBALS[$Var]=(!empty($GLOBALS['_SERVER'][$Var]))? |
| 7 | $GLOBALS['_SERVER'][$Var]: |
| 8 | (!empty($GLOBALS['HTTP_SERVER_VARS'][$Var]))? |
| 9 | $GLOBALS['HTTP_SERVER_VARS'][$Var]:''; |
| 10 | } |
| 11 | } |
| 12 | function lixlpixel_detect_lang() |
| 13 | { |
| 14 | // Detect HTTP_ACCEPT_LANGUAGE & HTTP_USER_AGENT. |
| 15 | lixlpixel_get_env_var('HTTP_ACCEPT_LANGUAGE'); |
| 16 | lixlpixel_get_env_var('HTTP_USER_AGENT'); |
| 17 | $_AL=strtolower($GLOBALS['HTTP_ACCEPT_LANGUAGE']); |
| 18 | $_UA=strtolower($GLOBALS['HTTP_USER_AGENT']); |
| 19 | // Try to detect Primary language if several languages are accepted. |
| 20 | foreach($GLOBALS['_LANG'] as $K) |
| 21 | { |
| 22 | if(strpos($_AL, $K)===0) |
| 23 | return $K; |
| 24 | } |
| 25 | // Try to detect any language if not yet detected. |
| 26 | foreach($GLOBALS['_LANG'] as $K) |
| 27 | { |
| 28 | if(strpos($_AL, $K)!==false) |
| 29 | return $K; |
| 30 | } |
| 31 | foreach($GLOBALS['_LANG'] as $K) |
| 32 | { |
| 33 | if(preg_match("/[[( ]{$K}[;,_-)]/",$_UA)) |
| 34 | return $K; |
| 35 | } |
| 36 | // Return default language if language is not yet detected. |
| 37 | return $GLOBALS['_DLANG']; |
| 38 | } |
| 39 | // Define default language. |
| 40 | $GLOBALS['_DLANG']='en'; |
| 41 | // Define all available languages. |
| 42 | // WARNING: uncomment all available languages |
| 43 | $GLOBALS['_LANG'] = array( |
| 44 | 'af', // afrikaans. |
| 45 | 'ar', // arabic. |
| 46 | 'bg', // bulgarian. |
| 47 | 'ca', // catalan. |
| 48 | 'cs', // czech. |
| 49 | 'da', // danish. |
| 50 | 'de', // german. |
| 51 | 'el', // greek. |
| 52 | 'en', // english. |
| 53 | 'es', // spanish. |
| 54 | 'et', // estonian. |
| 55 | 'fi', // finnish. |
| 56 | 'fr', // french. |
| 57 | 'gl', // galician. |
| 58 | 'he', // hebrew. |
| 59 | 'hi', // hindi. |
| 60 | 'hr', // croatian. |
| 61 | 'hu', // hungarian. |
| 62 | 'id', // indonesian. |
| 63 | 'it', // italian. |
| 64 | 'ja', // japanese. |
| 65 | 'ko', // korean. |
| 66 | 'ka', // georgian. |
| 67 | 'lt', // lithuanian. |
| 68 | 'lv', // latvian. |
| 69 | 'ms', // malay. |
| 70 | 'nl', // dutch. |
| 71 | 'no', // norwegian. |
| 72 | 'pl', // polish. |
| 73 | 'pt', // portuguese. |
| 74 | 'ro', // romanian. |
| 75 | 'ru', // russian. |
| 76 | 'sk', // slovak. |
| 77 | 'sl', // slovenian. |
| 78 | 'sq', // albanian. |
| 79 | 'sr', // serbian. |
| 80 | 'sv', // swedish. |
| 81 | 'th', // thai. |
| 82 | 'tr', // turkish. |
| 83 | 'uk', // ukrainian. |
| 84 | 'zh' // chinese. |
| 85 | ); |
| 86 | // Redirect to the correct location. |
| 87 | //header('location: http://www.your_site.com/index_'.lixlpixel_detect_lang().'.php'); // Example Implementation |
| 88 | echo 'The Language detected is: '.lixlpixel_detect_lang(); // For Demonstration |
| 89 | ?> |
43 hits by 8 users in the last 30 minutes.