• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

5. Browser Feature Detection > 5.8. Detecting the Browser Written Language

Detecting the Browser Written Language

NN 4, IE 4

Problem

You wish to direct users automatically to a path in your web site tailored for a specific written language.

Solution

Version 4 and later browsers provide properties of the navigator object that let you read the code for the native language for which the user’s browser was developed. Unfortunately, the property names are different for each browser, so you must perform some object property detection in the process. The solution below assumes that all users will be shunted to the English page unless their browsers indicate that the native language is German:

// verify browser language
function getLang(type) {
    var lang;
    if (typeof navigator.userLanguage != "undefined") {
        lang = navigator.userLanguage.toUpperCase( );
    } else if (typeof navigator.language != "undefined") {
        lang = navigator.language.toUpperCase( );
    }
    return (lang && lang.indexOf(type.toUpperCase( )) =  = 0)
}
...
location.href = (getLang("de")) ? "de/home.html" : "en/home.html";

PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint