What language? Detecting preferred language via browser
Page: prev. | 1 | next
Depending on the site and its contents, there are obviously occasions when knowing the preferred language of a visitor is helpful, so that perhaps text can be presented in that language, the visitor redirected to a version in that language or content switched depending on cultural variation. Indeed, it could be considered a foundation strand of the new buzz word “contextual web” experience (wn.com).
Server-side
It’s actually a fairly simple matter as most browsers actually set a language preference header which can be examined by a server-side script, with PHP using $_SERVER['HTTP_ACCEPT_LANGUAGE']
.
The $_SERVER
array element contains the contents of the Accept-Language: header from the browser’s request in the form of a two letter country code, usually combined with another if there is a variation of the language spoke—e.g. en for English, or or en-us for the United States, es for Spanish, es-es for Spanish spoke in Spain or es-mx in Mexico, or es-co in Colombia—and a relative quality factor (q=), a measure of preference in the case where multiple acceptable languages are present in bilingual cultures.
You can check what your browser returns here: HTTP header viewer (pgl.yoyo.org).
Or… well…
And a full list of language identification codes is available here: Web browser language identification codes (metamodpro.com).
The $_SERVER array element can be examined in a server-side script and decisions made.
- <?php
- if (isset ($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
- $language = $_SERVER["HTTP_ACCEPT_LANGUAGE"];
- }else {
- $language = 'None'; /* Possibly Paleolithic cave-dweller on the evolutionary way to or header just not set by browser. */
- }
- /* The $language variable now contains language identification code, e.g. en-gb, en-us, es-mx etc. A switch statement or similar could be used here to proceed based on. */
- //Get first preference
- if ($language != 'None') {
- x = strpos ($language,',');
- /* Note strpos is zero based (first character is 0 not 1) so if comma is third character it shall return 2 */
- if (x === false) {
- /* Make sure use 'identical' (===) operator not just 'equal' (==) as this can be problematic with booleans as '0' can be interpreted as 'false'.
A comma (,) not found in string thus only one language preference set. Look for semi-colon (;) instead. */ - x = strpos ($language,';');
- }
- if (x !== false) { //Using 'not identical' operator
- switch (substr ($language,0,x)) {
- case 'en':
- /* Note: most browsers will actually return en-us as they are American made and no significant difference seen unless specifically set in the browsers options. For example, by default it will likely return en-US,en;q=0.8 with default English (United States) set and en,en-US;q=0.8 only if British English is specifically set. This is why HTTP_ACCEPT_LANGUAGE is only for detecting preferred language rather than actual local. */
- // And do whatever action…
- break;
- case 'en-us':
- // Do whatever action…
- break;
- //More case expressions etc.
- }
- }
- }else {
- //Language preference header not present…
- }
- ?>
Client-side
- <script type="text/javascript">
- $language = navigator.language;
- </script>
An identical switch
statement with Javascript could then be used but, of course, the client-side nature may limit to presenting a clickable link for preferred language options unless a lot of re-pokeage with inner and/or outerHTML or such is undertaken.
Never forgetting the internet is truly global, I hope it may prove useful to you.
Related entries
- Page jumps with internal links
- HTML5Shiv for HTML5 support for IE < 9
- Resize an image proportionally with Javascript
- Disabling the right-click menu for an image
- Adding a Javascript Event Listener
- Javascript witchery: Passing parameters between pages with query string
- Resizing browser with JavaScript to test site display at different screen Dimensions
Page: prev. | 1 | next