[All-OS, Web sites] Internationalizing Web sites

PRODUCT: PHP All versions Web browsers
OP/SYS: All operating systems
COMPONENT: HTTP_USER_AGENT environment variable HTTP_ACCEPT_LANGUAGE environment variable
SOURCE: Philippe Vouters Fontainebleau/France
HIGH QUALITY MOBILES+TABLETS: http://android-land.fr
OVERVIEW: This article highlights a way to internationalize Web pages or Web sites. The main idea is to redirect the remote Web browser to a Web page according to the Web browser's configured or accepted language. The browser's configured language is often set by some Web browsers such as Firefox, Opera, Chrome or Safari in the HTTP_USER_AGENT environment variable. The browser's accepted languages is set by all graphical Web browsers in the HTTP_ACCEPT_LANGUAGE environment variable. It should be noted that both Konqueror and Internet Explorer do not give any clue as to what is the language they are configured in.
*** CAUTION *** This sample PHP script has been tested using PHP Version 5.2.9 on Linux Fedora 11. However, we cannot guarantee its effectiveness because of the possibility of error in transmitting or implementing it. It is meant to be used as a template for writing your own script, and may require modification for use on your system.
ACTUAL INTERNATIONAL WEB SITES: Consider the internationalization aspect of three Web sites and how they solved it: First http://www.uefa.com/ : --------------------------- The national language is selectable in the upper right screen of the browser window. Next http://www.google.com/ : ---------------------------- Using Wireshark to trace traffic on port 80 and Opera (English version) under Windows XP SP3, whichever the way you set your only preferred language, either French or English, meanwhile having first deleted any Google's cookies immediately redirects you to http://www.google.fr/ when http://www.google.com/ is activated from France. Only selecting Russian as the preferred language, no Web site redirection occurs (you stay on http://www.google.com/) and the Russian's speaking Web page mentions Google France when the URL is activated from France. So a first approach is that this technique appears to use a geographical localisation based upon the Internet IP address. However, things are more complicated as there is also a tradeoff based upon the Opera's preferred languages. If you only set English as the preferred language without any other idiom, from France you are redirected to http://www.google.fr/ and a French speaking page. If you add French as the second language and always deleting any Google's cookie, requesting http://www.google.com/, you are redirected to http://www.google.fr/ and an English speaking page. If you add Russian, move it up on top of the preferred languages, always deleting any Google's cookies and with http://www.google.com/ in the address bar, you are no longer redirected to http://www.google.fr/ (you stay on http://www.google.com/) and the page displays in Russian. Apparent conclusion for Google: a mixture of HTTP_ACCEPT_LANGUAGE environment variable handling and geographical localization. Last http://www.hp.com/ : ------------------------ Using exclusively Opera (English version) under Windows XP SP3, first delete any HP's cookies (Tools->Preferences - Advanced Tab - Cookies - Manage Cookies button). Depending on whether you set the first preferred language in the list to French or English, you are automatically redirected to a French speaking page or an English speaking page. Not deleting the English stored HP's cookies and moving up back to French as the first preferred language leads you to a "Sorry, we can't find that page" HP's response. Using Wireshark and tracing network traffic on port 80 shows that opening http://www.hp.com/ highlights the preferred language in the browser's network sent "Accept-Language" information which can be retrieved in the HTTP_ACCEPT_LANGUAGE environment variable information (without Wireshark, activate /cgi-bin/printenv to check). Apparent conclusion for HP site: This Web site ought to rely upon two informations : the information contained in HTTP_ACCEPT_LANGUAGE environment variable combined with the content of its cookies.
ALTERNATIVE TO MANUAL TRANSLATION: Another approach to multi-language Web sites would be to use the following new PHP class: Name: PHP Website Multi Language Author: Sandeep Kumar Description: This class can be used to translate application texts using Google Translate. It can take a script with an array of texts in English and sends requests to the Google Translate Web site to translate each of the texts to another idiom. The class generates a new script for the target language with an array with the texts translated to that language. URL: http://www.phpclasses.org/browse/package/5766.html Support forum: http://www.phpclasses.org/discuss/package/5766/
SOME NOTES ABOUT AUTOMATIC TRANSLATIONS: Automatic translation of Web content by tools such as offered by Google or any other Web site offering such a service has a real drawback. The translation can be approximate, sometimes leading to confusions for the reader. According to the author, an automatic translation ought to not yet be used by commercial sites. As an example of the statement contained in the above paragraph and for those well knowing English and another language, read the English version of http://www.fedoraforum.org/ and its automatic translation by Google. You will get Google's translation by clicking on the non-English language found at the bottom of this Web page. More information on computer assisted translations can be found at the following link : http://en.wikipedia.org/wiki/Computer-assisted_translation
SCRIPT NOTES: Activate /cgi-bin/printenv. Read the HTTP_USER_AGENT and HTTP_ACCEPT_LANGUAGE environment variables content. This is what an Apache Web server sees. It contains the specific language information your browser sends over the network. If HTTP_ACCEPT_AGENT first contains "fr" for French or "en" for English or "ru" for Russian, activating ../multinational/ifrjson.php will cause your browser to automatically redirect you to : ../javascript/ifrjson.fr.html for the French version or ../javascript/ifrjson.en.html for the English version or ../javascript/ifrjson.ru.html for the Russian version respectively. If your browser gives no clue as to its preferred language, the redirection defaults you to the language your browser is installed in ("fr", "en" or "ru") using the HTTP_USER_AGENT information or if nothing applicable to ../javascript/ifrjson.en.html which is the English version of the Web page.
SCRIPT: COPYRIGHT (C) 2009 BY HEWLETT-PACKARD COMPANY ALL RIGHTS RESERVED. THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND COPIED ONLY IN ACCORDANCE WITH THE TERMS OF SUCH LICENSE AND WITH THE INCLUSION OF THE ABOVE COPYRIGHT NOTICE. THIS SOFTWARE OR ANY OTHER COPIES THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY OTHER PERSON. NO TITLE TO AND OWNERSHIP OF THE SOFTWARE IS HEREBY TRANSFERRED. THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE AND SHOULD NOT BE CONSTRUED AS A COMMITMENT BY HEWLETT-PACKARD COMPANY. HP ASSUMES NO RESPONSIBILITY FOR THE USE OR RELIABILITY OF ITS SOFTWARE ON EQUIPMENT THAT IS NOT SUPPLIED BY HP. NO RESPONSIBILITY IS ASSUMED FOR THE USE OR RELIABILITY OF SOFTWARE ON EQUIPMENT THAT IS NOT SUPPLIED BY HEWLETT-PACKARD COMPANY. SUPPORT FOR THIS SOFTWARE IS NOT COVERED UNDER ANY HP SOFTWARE PRODUCT SUPPORT CONTRACT, BUT MAY BE PROVIDED UNDER THE TERMS OF THE CONSULTING AGREEMENT UNDER WHICH THIS SOFTWARE WAS DEVELOPED. ******* * ifrjson.php ******* <?php $acc_language="Unknown"; if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){ $accept_language = $_SERVER['HTTP_ACCEPT_LANGUAGE']; preg_match("/(.*)[,;]?(.*)/",$accept_language,$matches); $acc_language=substr($matches[0],0,2); } if (isset($_SERVER['HTTP_USER_AGENT'])){ $browser=$_SERVER['HTTP_USER_AGENT']; } /* * What is the browser's language installed in ? * Tested with Firefox and Opera under Linux Fedora 12 * and Windows 7. Microsoft Internet Explorer reports * nothing as to what IE's installed language is. */ if (isset($browser) and preg_match("/(.*); (([a-z]{2,2})(\-[A-Z]{2,2})?)(;|\)) (.*)$/", $browser,$matches)){ $language=$matches[2]; } /* * Browser's language installation not recognized; defaulting to English. */ if (!isset($language)){ $language = "Unknown"; } ?> <html> <head> <title>Redirection en htm</title> <?php if (($acc_language == "fr") || ($acc_language == "en") || ($acc_language == "ru")){ echo "<meta http-equiv='refresh' content='5; URL=../javascript/ifrjson.$acc_language.html'>"; } else{ if (($language == "fr") || ($language == "en") || ($language == "ru")){ echo "<meta http-equiv='refresh' content='5; URL=../javascript/ifrjson.$language.html'>"; } else{ echo "<meta http-equiv='refresh' content='5; URL=../javascript/ifrjson.en.html'>"; } } ?> </head> <body> <?php echo "The Web server has determined your browser is configured in "; switch ($language){ case "fr": echo "French."; break; case "ru": echo "Russian."; break; case "en"; echo "English."; break; default: echo "Unhandled. Reports $language"; echo "<br>Your browser accepts "; switch ($acc_language){ case "fr": echo "French."; break; case "ru": echo "Russian."; break; case "en"; echo "English."; break; default: echo "$acc_language. Defaulting to English"; } } echo "<br>"; ?> You will be redirected according to your browser national language. </body> </html>
REFERENCE(S): Public Web search engines researches So that the ifrjson."lang".html national Web pages work indifferently under Firefox, Opera, Safari, Internet Explorer 6 and Konqueror Web browsers, parts of the javascript code and html tags have been taken from the solution at: ../tima/All-OS-Web_Browsers-JavaScript-JavaScript_Code_Rewritting_to_improve_performances.html