PHP Language detector

PHP Language detector

7 32145
PHP Language detector

PHP Language detector

In this tutorial I will show you how to detect and display the visitor`s preferable language in PHP. I think that language detection is one of important features of any serious multilanguage website. As minimum – you can set necessary language for website interface for your visitors. Our script will show you one of 180 possible languages (depends on your browser’s preferences. Ready? Lets start.

Live Demo

Now – download the source files and lets start developing !


Step 1. PHP

In the first – I will put an array of all possible languages in this array (into separate file):

langs.php

<?php
// all the possible languages codes
$aLanguages = array(
'aa' => 'Afar',
'ab' => 'Abkhaz',
'ae' => 'Avestan',
'af' => 'Afrikaans',
'ak' => 'Akan',
'am' => 'Amharic',
'an' => 'Aragonese',
'ar' => 'Arabic',
'as' => 'Assamese',
'av' => 'Avaric',
'ay' => 'Aymara',
'az' => 'Azerbaijani',
'ba' => 'Bashkir',
'be' => 'Belarusian',
'bg' => 'Bulgarian',
'bh' => 'Bihari',
'bi' => 'Bislama',
'bm' => 'Bambara',
'bn' => 'Bengali',
'bo' => 'Tibetan Standard, Tibetan, Central',
'br' => 'Breton',
'bs' => 'Bosnian',
'ca' => 'Catalan; Valencian',
'ce' => 'Chechen',
'ch' => 'Chamorro',
'co' => 'Corsican',
'cr' => 'Cree',
'cs' => 'Czech',
'cu' => 'Old Church Slavonic, Church Slavic, Church Slavonic, Old Bulgarian, Old Slavonic',
'cv' => 'Chuvash',
'cy' => 'Welsh',
'da' => 'Danish',
'de' => 'German',
'dv' => 'Divehi; Dhivehi; Maldivian;',
'dz' => 'Dzongkha',
'ee' => 'Ewe',
'el' => 'Greek, Modern',
'en' => 'English',
'eo' => 'Esperanto',
'es' => 'Spanish; Castilian',
'et' => 'Estonian',
'eu' => 'Basque',
'fa' => 'Persian',
'ff' => 'Fula; Fulah; Pulaar; Pular',
'fi' => 'Finnish',
'fj' => 'Fijian',
'fo' => 'Faroese',
'fr' => 'French',
'fy' => 'Western Frisian',
'ga' => 'Irish',
'gd' => 'Scottish Gaelic; Gaelic',
'gl' => 'Galician',
'gn' => 'GuaranA­',
'gu' => 'Gujarati',
'gv' => 'Manx',
'ha' => 'Hausa',
'he' => 'Hebrew (modern)',
'hi' => 'Hindi',
'ho' => 'Hiri Motu',
'hr' => 'Croatian',
'ht' => 'Haitian; Haitian Creole',
'hu' => 'Hungarian',
'hy' => 'Armenian',
'hz' => 'Herero',
'ia' => 'Interlingua',
'id' => 'Indonesian',
'ie' => 'Interlingue',
'ig' => 'Igbo',
'ii' => 'Nuosu',
'ik' => 'Inupiaq',
'io' => 'Ido',
'is' => 'Icelandic',
'it' => 'Italian',
'iu' => 'Inuktitut',
'ja' => 'Japanese (ja)',
'jv' => 'Javanese (jv)',
'ka' => 'Georgian',
'kg' => 'Kongo',
'ki' => 'Kikuyu, Gikuyu',
'kj' => 'Kwanyama, Kuanyama',
'kk' => 'Kazakh',
'kl' => 'Kalaallisut, Greenlandic',
'km' => 'Khmer',
'kn' => 'Kannada',
'ko' => 'Korean',
'kr' => 'Kanuri',
'ks' => 'Kashmiri',
'ku' => 'Kurdish',
'kv' => 'Komi',
'kw' => 'Cornish',
'ky' => 'Kirghiz, Kyrgyz',
'la' => 'Latin',
'lb' => 'Luxembourgish, Letzeburgesch',
'lg' => 'Luganda',
'li' => 'Limburgish, Limburgan, Limburger',
'ln' => 'Lingala',
'lo' => 'Lao',
'lt' => 'Lithuanian',
'lu' => 'Luba-Katanga',
'lv' => 'Latvian',
'mg' => 'Malagasy',
'mh' => 'Marshallese',
'mi' => 'Maori',
'mk' => 'Macedonian',
'ml' => 'Malayalam',
'mn' => 'Mongolian',
'mr' => 'Marathi',
'ms' => 'Malay',
'mt' => 'Maltese',
'my' => 'Burmese',
'na' => 'Nauru',
'nb' => 'Norwegian Bokmal',
'nd' => 'North Ndebele',
'ne' => 'Nepali',
'ng' => 'Ndonga',
'nl' => 'Dutch',
'nn' => 'Norwegian Nynorsk',
'no' => 'Norwegian',
'nr' => 'South Ndebele',
'nv' => 'Navajo, Navaho',
'ny' => 'Chichewa; Chewa; Nyanja',
'oc' => 'Occitan',
'oj' => 'Ojibwe, Ojibwa',
'om' => 'Oromo',
'or' => 'Oriya',
'os' => 'Ossetian, Ossetic',
'pa' => 'Panjabi, Punjabi',
'pi' => 'Pali',
'pl' => 'Polish',
'ps' => 'Pashto, Pushto',
'pt' => 'Portuguese',
'qu' => 'Quechua',
'rm' => 'Romansh',
'rn' => 'Kirundi',
'ro' => 'Romanian, Moldavian, Moldovan',
'ru' => 'Russian',
'rw' => 'Kinyarwanda',
'sa' => 'Sanskrit',
'sc' => 'Sardinian',
'sd' => 'Sindhi',
'se' => 'Northern Sami',
'sg' => 'Sango',
'si' => 'Sinhala, Sinhalese',
'sk' => 'Slovak',
'sl' => 'Slovene',
'sm' => 'Samoan',
'sn' => 'Shona',
'so' => 'Somali',
'sq' => 'Albanian',
'sr' => 'Serbian',
'ss' => 'Swati',
'st' => 'Southern Sotho',
'su' => 'Sundanese',
'sv' => 'Swedish',
'sw' => 'Swahili',
'ta' => 'Tamil',
'te' => 'Telugu',
'tg' => 'Tajik',
'th' => 'Thai',
'ti' => 'Tigrinya',
'tk' => 'Turkmen',
'tl' => 'Tagalog',
'tn' => 'Tswana',
'to' => 'Tonga (Tonga Islands)',
'tr' => 'Turkish',
'ts' => 'Tsonga',
'tt' => 'Tatar',
'tw' => 'Twi',
'ty' => 'Tahitian',
'ug' => 'Uighur, Uyghur',
'uk' => 'Ukrainian',
'ur' => 'Urdu',
'uz' => 'Uzbek',
've' => 'Venda',
'vi' => 'Vietnamese',
'vo' => 'Volapuk',
'wa' => 'Walloon',
'wo' => 'Wolof',
'xh' => 'Xhosa',
'yi' => 'Yiddish',
'yo' => 'Yoruba',
'za' => 'Zhuang, Chuang',
'zh' => 'Chinese',
'zu' => 'Zulu',
);
?>

Now we are going to prepare our main function to determine languages:

index.php

// determine preferable language
function tryToFindLang($aLanguages, $sWhere, $sDefaultLang) {
// set current language as default language
$sLanguage = $sDefaultLang;
// initial better quality
$fBetterQuality = 0;
// search for all matched params
preg_match_all(&quot;/([[:alpha:]]{1,8})(-([[:alpha:]|-]{1,8}))?(\s*;\s*q\s*=\s*(1\.0{0,3}|0\.\d{0,3}))?\s*(,|$)/i&quot;, $sWhere, $aMatches, PREG_SET_ORDER);
foreach ($aMatches as $aMatch) {
// get language prefix
$sPrefix = strtolower ($aMatch[1]);
// prepare temp language
$sTempLang = (empty($aMatch[3])) ? $sPrefix : $sPrefix . '-' . strtolower ($aMatch[3]);
// get quality (if it exists)
$fQuality = (empty($aMatch[5])) ? 1.0 : floatval($aMatch[5]);
if ($sTempLang) {
// determinate best quality
if ($fQuality &gt; $fBetterQuality &amp;&amp; in_array($sTempLang, array_keys($aLanguages))) {
// set current language as temp language and update quality value
$sLanguage = $sTempLang;
$fBetterQuality = $fQuality;
} elseif (($fQuality*0.9) &gt; $fBetterQuality &amp;&amp; in_array($sPrefix, array_keys($aLanguages))) {
// set current language as prefix value and update quality value
$sLanguage = $sPrefix;
$fBetterQuality = $fQuality * 0.9;
}
}
}
return $sLanguage;
}

Main idea – to parse global variable: $_SERVER[‘HTTP_ACCEPT_LANGUAGE’], as we know – this variable contains Accept-Language request header, which is similar to Accept, but restricts the set of natural languages that are preferred as a response to the request. If you would like to understand it in details – you can read it here. I left comments in my code in order to better understand it. Now, how can we use it? – Easily, look:

// obtain all the possible languages codes
require_once('langs.php');
$sLanguage = tryToFindLang($aLanguages, $_SERVER['HTTP_ACCEPT_LANGUAGE'], 'en');

And now our $sLanguage variable contains our preferred language code.


Live Demo

Conclusion

And again, we have made another one interesting PHP tutorial. You are welcome to use it in your projects. Good luck in your work!


7 COMMENTS

  1. on my Wamp localhost, PHP (5.4.3) spit out:

    ( ! ) SCREAM: Error suppression ignored for
    ( ! ) Parse error: syntax error, unexpected ‘;’, expecting :: (T_PAAMAYIM_NEKUDOTAYIM) in C:\wamp\www\testScripte\index.php on line 11

    • Hi Peter,
      Interesting, so about me – I developed it on Wamp too, but, my PHP version is 5.3.0.
      Did you use non-touched version from our package, or you copied our code into your PHP?

  2. HELLO!

    This is what I was looking for!
    Just tell me please. How can I make riderekt to the right partition?

    Thanks.

Leave a Reply