Monday, November 3, 2014

Javascript to get the current language of the browser


function getLanguageCode() {
    var lang;
    if (getQueryVariable("language")) {
        lang = getQueryVariable("language");
    } else if (navigator) {
        if (navigator.userLanguage) {
            lang = navigator.userLanguage;
        } else if (navigator.browserLanguage) {
            lang = navigator.browserLanguage;
        } else if (navigator.systemLanguage) {
            lang = navigator.systemLanguage;
        } else if (navigator.Language) {
            lang = navigator.Language;
        } else if (navigator.language) {
            lang = navigator.language;
        }
    }
    alert(lang);
}

function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
            return pair[1];
        }
    }
    return false;
}

Credits : Amit Kumar 
Email : amitagl27@gmail.com

No comments:

Post a Comment