Detect Locale with javascript

Language detectionThere may be times where you would want to detect the language of the users browser. I would normally use php to detect the users browser language. However, when this is not possible, it can also be done using the following javascript:

 thelocale = 'en-GB'; // set default locale
if ( navigator ) { if ( navigator.language ) {// for netscape thelocale = navigator.language; } else if ( navigator.browserLanguage ) {// internet explorer 5.5 or above
        thelocale = navigator.browserLanguage;
    }
    else if ( navigator.systemLanguage ) {// windows specific
        thelocale = navigator.systemLanguage;
    }
    else if ( navigator.userLanguage ) {
        thelocale = navigator.userLanguage;
    }
}

Tags: , , , , ,

Leave a Comment

You must be logged in to post a comment.