Jump to content

Recommended Posts

hey guys,

 

within the site logger i created for my site, i have a piece of code which is supposed to use the Useragent to find the users prefared language, this works fine in Firefox, IE, Opera,  but not in Safari or Chrome.

 

this is the code:

<?php
$languageList = array
(
'Afrikaans' => 'af',
'Albanian' => 'sq',
'Arabic (Algeria)' => 'ar-dz',
'Arabic (Bahrain)' => 'ar-bh',
'Arabic (Egypt)' => 'ar-eg',
'Arabic (Iraq)' => 'ar-iq',
'Arabic (Jordan)' => 'ar-jo',
'Arabic (Kuwait)' => 'ar-kw',
'Arabic (Lebanon)' => 'ar-lb',
'Arabic (libya)' => 'ar-ly',
'Arabic (Morocco)' => 'ar-ma',
'Arabic (Oman)' => 'ar-om',
'Arabic (Qatar)' => 'ar-qa',
'Arabic (Saudi Arabia)' => 'ar-sa',
'Arabic (Syria)' => 'ar-sy',
'Arabic (Tunisia)' => 'ar-tn',
'Arabic (U.A.E.)' => 'ar-ae',
'Arabic (Yemen)' => 'ar-ye',
'Arabic' => 'ar',
'Armenian' => 'hy',
'Assamese' => 'as',
'Azeri' => 'az',
'Basque' => 'eu',
'Belarusian' => 'be',
'Bengali' => 'bn',
'Bulgarian' => 'bg',
'Catalan' => 'ca',
'Chinese (China)' => 'zh-cn',
'Chinese (Hong Kong SAR)' => 'zh-hk',
'Chinese (Macau SAR)' => 'zh-mo',
'Chinese (Singapore)' => 'zh-sg',
'Chinese (Taiwan)' => 'zh-tw',
'Chinese' => 'zh',
'Croatian' => 'hr',
'Czech' => 'cs',
'Danish' => 'da',
'Divehi' => 'div',
'Dutch (Belgium)' => 'nl-be',
'Dutch (Netherlands)' => 'nl',
'English (Australia)' => 'en-au',
'English (Belize)' => 'en-bz',
'English (Canada)' => 'en-ca',
'English (Ireland)' => 'en-ie',
'English (Jamaica)' => 'en-jm',
'English (New Zealand)' => 'en-nz',
'English (Philippines)' => 'en-ph',
'English (South Africa)' => 'en-za',
'English (Trinidad)' => 'en-tt',
'English (United Kingdom)' => 'en-gb',
'English (United States)' => 'en-US',
'English (Zimbabwe)' => 'en-zw',
'English' => 'en',
'English (United States)' => 'us',
'Estonian' => 'et',
'Faeroese' => 'fo',
'Farsi' => 'fa',
'Finnish' => 'fi',
'French (Belgium)' => 'fr-be',
'French (Canada)' => 'fr-ca',
'French (Luxembourg)' => 'fr-lu',
'French (Monaco)' => 'fr-mc',
'French (Switzerland)' => 'fr-ch',
'French (France)' => 'fr',
'FYRO Macedonian' => 'mk',
'Gaelic' => 'gd',
'Georgian' => 'ka',
'German (Austria)' => 'de-at',
'German (Liechtenstein)' => 'de-li',
'German (Luxembourg)' => 'de-lu',
'German (Switzerland)' => 'de-ch',
'German (Germany)' => 'de',
'Greek' => 'el',
'Gujarati' => 'gu',
'Hebrew' => 'he',
'Hindi' => 'hi',
'Hungarian' => 'hu',
'Icelandic' => 'is',
'Indonesian' => 'id',
'Italian (Switzerland)' => 'it-ch',
'Italian (Italy)' => 'it',
'Japanese' => 'ja',
'Kannada' => 'kn',
'Kazakh' => 'kk',
'Konkani' => 'kok',
'Korean' => 'ko',
'Kyrgyz' => 'kz',
'Latvian' => 'lv',
'Lithuanian' => 'lt',
'Malay' => 'ms',
'Malayalam' => 'ml',
'Maltese' => 'mt',
'Marathi' => 'mr',
'Mongolian (Cyrillic)' => 'mn',
'Nepali (India)' => 'ne',
'Norwegian (Bokmal)' => 'nb-no',
'Norwegian (Nynorsk)' => 'nn-no',
'Norwegian (Bokmal)' => 'no',
'Oriya' => 'or',
'Polish' => 'pl',
'Portuguese (Brazil)' => 'pt-br',
'Portuguese (Portugal)' => 'pt',
'Punjabi' => 'pa',
'Rhaeto-Romanic' => 'rm',
'Romanian (Moldova)' => 'ro-md',
'Romanian' => 'ro',
'Russian (Moldova)' => 'ru-md',
'Russian' => 'ru',
'Sanskrit' => 'sa',
'Serbian' => 'sr',
'Slovak' => 'sk',
'Slovenian' => 'ls',
'Sorbian' => 'sb',
'Spanish (Argentina)' => 'es-ar',
'Spanish (Bolivia)' => 'es-bo',
'Spanish (Chile)' => 'es-cl',
'Spanish (Colombia)' => 'es-co',
'Spanish (Costa Rica)' => 'es-cr',
'Spanish (Dominican Republic)' => 'es-do',
'Spanish (Ecuador)' => 'es-ec',
'Spanish (El Salvador)' => 'es-sv',
'Spanish (Guatemala)' => 'es-gt',
'Spanish (Honduras)' => 'es-hn',
'Spanish (Mexico)' => 'es-mx',
'Spanish (Nicaragua)' => 'es-ni',
'Spanish (Panama)' => 'es-pa',
'Spanish (Paraguay)' => 'es-py',
'Spanish (Peru)' => 'es-pe',
'Spanish (Puerto Rico)' => 'es-pr',
'Spanish (United States)' => 'es-us',
'Spanish (Uruguay)' => 'es-uy',
'Spanish (Venezuela)' => 'es-ve',
'Spanish (Traditional Sort)' => 'es',
'Sutu' => 'sx',
'Swahili' => 'sw',
'Swedish (Finland)' => 'sv-fi',
'Swedish' => 'sv',
'Syriac' => 'syr',
'Tamil' => 'ta',
'Tatar' => 'tt',
'Telugu' => 'te',
'Thai' => 'th',
'Tsonga' => 'ts',
'Tswana' => 'tn',
'Turkish' => 'tr',
'Ukrainian' => 'uk',
'Urdu' => 'ur',
'Uzbek' => 'uz',
'Vietnamese' => 'vi',
'Xhosa' => 'xh',
'Yiddish' => 'yi',
'Zulu' => 'zu',
'Unknown Language' => '()'
);

foreach($languageList as $CurrLanguage=>$Match)
{
        if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
        {
                break;
        }
}
?> 

 

the useragent strings i have go as follows:

Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/532.0 (KHTML, like Gecko) Chrome/3.0.195.38 Safari/532.0  //Chrome

Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/531.21.10    //Safari

 

 

from those useragents,  its comming back with my language code being 'af  -  Afrikaans' before grabbing the 'en-US', all i can think of is that the array is being matched with the 'af' from 'safari' first; but as thats at the end of the string, shouldnt it find the 'en-US' first?

 

can anyone please help with this?

 

thanks guys

Link to comment
https://forums.phpfreaks.com/topic/189222-language-detection-problem/
Share on other sites

--UPDATE--

 

i added a space before every language code, (eg: 'English (United Kingdom)' => ' en-gb', 'English (United States)' => ' en-us',)  which made it work fine in Chrome and Safari as usually with all useragents there will be a single space before the language code to seperate it from the previous part of the useragent string.

 

my problem is now that IE doesnt show the correct language, it shows 'Malay - ms', but then i had a look at my useragent string for IE, and i dont actually see any language code within it anyway

Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; OfficeLiveConnector.1.4; OfficeLivePatch.1.3)

 

so is it even possible to find the user language code through IE?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.