Search code examples
androidjqueryhtmlcordovaphonegap-plugins

How to get device/browser language with phonegap


I tried to get the devices language value with the globalization plugin. I followed the installation procedure, read the docs and other questions here for help but none of them helped me.

navigator.globalization.getLocaleName(function (language) {
$('#test').html(language.value);
console.log(language.value);
});

This is the code I used. I don't recieve any console log after refreshing the site. It seems like the function isn't called at all. Has someone worked with this plugin before and might now help me to figure out a solution?


Solution

  • Are you waiting until deviceready has fired? An example would be:

    document.addEventListener("deviceready", onDeviceReady, false);
    
    function onDeviceReady() {
    
        navigator.globalization.getLocaleName(function (language) {
    
            console.log(language.value);
    
        },
    
        function () {
    
            console.log('Error getting locale\n');
    
        }
    
        );
    
    }
    

    You are also missing the error callback. Try the code above.