Check client language with Javascript

It is simple enough to detect the end-users preferred language:

var language = window.navigator.userLanguage || window.navigator.language;

This will be a country code, for example ‘it’ or ‘en-US’


This particular example uses the Google Translate service and will trigger the translate widget to show up if their language is different from your target language:


&lt;div id="google_translate_element"&gt;&lt;/div&gt;
function googleTranslateElementInit() {
	new google.translate.TranslateElement({pageLanguage: 'en', layout: google.translate.TranslateElement.FloatPosition.TOP_LEFT, gaTrack: true, gaId: 'UA-YOUR-GA-ID'}, 'google_translate_element');

// detect language and conditionally call the googleTranslateElementInit() function
var checkLanguage = function(){
	    language = window.navigator.userLanguage || window.navigator.language;
	if(language != TARGET_LANGUAGE){
&lt;/script&gt;&lt;script type="text/javascript" src="//"&gt;&lt;/script&gt;


Leave a Reply