How to Detect Language?

admin

Administrator
Staff member
I'm not sure what forum this belongs in.. Although I write this code in PHP, these questions go out to everybody.. I figured most people would see it in this forum.<br />
<br />
Anyway, to the point.<br />
I'm writing a pretty small site, that will currently support seven languages, and those include English, German, French, Spanish, Swedish, Danish and Icelandic.<br />
I'm basically wondering where these languages are official languages?<br />
Swedish, Danish and Icelandic are only spoken in the respective nations(unless I'm way off), so those aren't interesting. English will be the default language of my site, i.e. if your country is not found in the list, you'll view the site in English.<br />
This is the a sample from the code I'm using for detecting where my visitors come from, it's simple PHP with some simple Regular Expressions. It is also a list of the countries I know so far, in TLD code.<br />
<br />
<br />
...<br />
else if(eregi("(de|at|ch|li)$",gethostbyaddr($_SERVER['REMOTE_ADDR']))) {<br />
$lang = "de"; // Set language to German<br />
}<br />
else if(eregi("(fr|be|bj|bi|bf|cm|cf|td|cd|km|cg|ci|dj|ga|gn|lu|mg|ml|mc|sn|tg)$",<br />
gethostbyaddr($_SERVER['REMOTE_ADDR']))) {<br />
$lang = "fr"; // Set language to French<br />
}<br />
else if(eregi("(es|cl|ar|bo|co|cr|cu|do|ec|sv|gq|gt|hn|mx|ni|pa|py|pe|uy|ve)$",<br />
gethostbyaddr($_SERVER['REMOTE_ADDR']))) {<br />
$lang = "es"; // Set language to Spanish<br />
}<br />
...<br />
<br />
<br />
Well it simply reads the end of the visitors hostname, to determine the language. Do you have any countries do add? Any changes? I'm interested in official languages, and when there are more than one, then the language most widely spoken. For example, English and French are official in Canada, but since most people speak English, I don't list Canada as a French speaking nation. A list of TLD codes can be found here (<!-- m --><a class="postlink" href="http://www.iana.org/cctld/cctld-whois.htm">http://www.iana.org/cctld/cctld-whois.htm</a><!-- m -->).<br />
<br />
My second question is more client side scripting related I suppose... Is there any other way of finding out the nationality of a visitor? What I'm using now isn't that great, since a lot of ISP's use .com or .net addresses, no matter where they're from. What does JS have to offer?<!--content-->Whatever you do, you'll still need clickable links to each section of the website, either using an image of the flag of each country, or a text link with the name of the language, for those users who do nave javascript, or who have switched it off.<br />
<br />
There is no way you can get a reliable test using javascript, or any sort of server-side code, as native speakers of any language may choose to live or work in any country, anywhere in the world.<br />
<br />
<br />
You'll be needing ISO 3166 for a list of country codes, and ISO 639 for a list of language codes.<br />
<br />
Additionally, and related, ISO 4217 deals with codes for currency, and ISO 8601 deals with formats for date and time.<!--content-->giz why don't you tell peoople what to code instead of showing us the ISO and the validator and stuff nobody cares about. just like the dates. who cares what format they are in. show me how to do it like you say. show us the way to use the ISO's. bet you don't know how. what he might want to know is how to use the ISO's in a page.<br />
<br />
but, YES you can get the language of the browser in serverside code.<br />
<br />
"$HTTP_ACCEPT_LANGUAGE" is holding the languages that the browser accepts!<br />
If your "register_globals" is "off" you can access the variable trough the<br />
super global arrays $_SERVER or $_ENV! Like: $_SERVER["HTTP_ACCEPT_LANGUAGE"]<br />
<br />
that tells what language the users browser is and then use javascript or css to load the correct css or what ever.<br />
<br />
But if you want more advanced solution you could look at "browscap"!<br />
Begin here:<br />
<!-- m --><a class="postlink" href="http://www.php.net/manual/en/function.get-browser.php">http://www.php.net/manual/en/function.get-browser.php</a><!-- m --><br />
<br />
I suggest you don't use the remote address and the host. not everybodies browser sends that info.<!--content-->giz, I'm perfectly aware that no matter how good I make the script, it can always be wrong, and of course I will have links to all the languages. It will be stored in a cookie as well, so they'll just have to make the choice once, ever.<br />
<br />
As you might notice, I am currently using the correct codes for language and country, so yes, I have looked them up already. I'm not sure the codes in ISO 3166 are all the same as the TLD code, but it looks like it. The TLD code is the intresting part anyway.<br />
<br />
And scoutt, thanks a lot, I'll look into browscap etc... I guess the best solution would be to use all the methods, and make the script make the most likely guess.<br />
<br />
No countries to add though, people? :) I guess I caught most of them ;)<!--content-->
 
Back
Top