I created a PHP script that checks the HTTP_ACCEPT_LANGUAGE
and loads the website using the appropriate language from the 1st two characters:
$http_lang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2);
switch ($http_lang) {
case 'en':
$SESSION->conf['language'] = 'english';
break;
case 'es':
$SESSION->conf['language'] = 'spanish';
break;
default:
$SESSION->conf['language'] = $PREFS->conf['languages'][$SESSION->conf['language_id']];
}
If I change the language to Spanish in Firefox the website loads in Spanish fine. However I have had several reports that people in Colombia see the website in english.
Details: "es-co" LCID = 9226 Spanish(Colombia)
Anyone have any ideas as to why this is happening? I thought this was the best way to check what language users support.
I used the regex from @GabrielAnderson and devised this function which behaves according to RFC 2616 (when no quality value is given to a language, it defaults to 1).
When several languages share the same quality value, the most specific are given priority over the less specific ones. (this behaviour is not part of the RFC which provides no recommendation for this specific case)
Example:
Outputs:
As you can notice, 'en-US' appears in first position despite the fact that 'en' was first in the given string.
So you could use this function and just replace your first line of code by:
if you want to store languages in array, i do this:
this output an array to languages e other with values
this output an array like this:
I put my trust in the skilled programmers who work for PHP and think ahead. Here is my version of a label for the Google translator drop down.
I will use full locale code to refer language, because like
zh-TW
andzh-CN
is 2 different language.In the end I went with this solution:
I would like to thank AJ for the links. Also thanks to all that replied.
A more contemporary method would be to use
http_negotiate_language()
:If you don't have the http extension installed (and not the intl one as well), there is yet another workaround in the comments (user-note #86787 (Nov 2008; by Anonymous)):