How can I get device localization or language using PhoneGap assembly?

I am using Phonegap Build to develop an iOS and Android app.

I would like to define a locale (like "en-US") for the device, although I would settle for the current language setting or even the app my app was installed in (it's been a long day).

Following the instructions here for the globalization plugin, I think I've got it all right, but nothing is working on the iPhone 6 or Samsung Galaxy Nexus I'm using for testing.

The relevant part of my config.xml file looks like this:

<gap:plugin name="org.apache.cordova.globalization" />

      

My function to get the locale from the plugin looks like this:

var getPhoneGapLocaleName = function() {
    var loc = 'unknown';

    if (navigator.globalization !== undefined) {
        navigator.globalization.getLocaleName(
            function (locale) {
                if (locale !== undefined) {
                        loc = locale.value;
                }
            },
            function () {
                // nothing
            }
        );
    }

    return loc;
};

      

Note. On both devices navigator.globalization.getLocaleName is present and displayed correctly, evaluating a function similar to what I would expect based on the documentation.

+3


source to share


3 answers


The problem was that the 'loc' variable was declared outside of the successful or failed callbacks, which of course happen after a few short moments.

I fixed it by changing the function this way:

var refreshPhoneGapLocaleName = function() {
    if (navigator.globalization !== undefined) {
        navigator.globalization.getLocaleName(
            function (locale) {
                if (locale !== undefined) {
                    localStorage['pg.locale'] = locale.value;
                }
            },
            function () {
                // nothing
            }
        );
    }
};

      

Now call it in onDeviceReady to update the values ​​when the app starts.



A few seconds later (not immediately), you can use the following function to get the locale value:

var getLocale = function() {
    return localStorage['pg.locale']();
};

      

The most interesting thing about StackOverflow is how often it helps to solve some silly mistakes. :)

+2


source


While the previous answers return the desired result and provide the ability to retrieve the current name of the local phone call, they did not explain the topic of the starter, why its function is not working, and how to configure its function to work the way it does (i.e. not use localStorage and don't show locale in console but give real time response as a result)

I am posting this answer as I was looking for a quick function to get device localization and this post was my first result. While the initial post gave me everything I needed, I would like to answer this question for future visitors with the same purpose as me. Sorry for posting this old in the thread, but I hope I can help others with my answer.

The reason the theme's starter function doesn't work is because the plugin returns the locale asynchronously. Therefore, the loc = locale.value line is executed only after the return function statement. To fix this, we can write a wrapper function to simplify the output of plugins as follows. Keep in mind that we must use this function asynchronously, since the plugin's output is also asynchronous.

Function:



var getPhoneGapLocaleName = function ( callback ) {
    var unknownLocation = 'unknown'; //Default value
    if ( navigator.globalization !== undefined ) {
        navigator.globalization.getLocaleName(
            function ( locale ) {
                if ( locale !== undefined ) {
                    callback( locale.value );
                } else {
                    callback( unknownLocation );
                }
            },
            function () {
                callback( unknownLocation );
            }
        );
    } else {
        callback( unknownLocation );
    }
};

      

Use the function like this:

getPhoneGapLocaleName( function( loc ){ console.log( 'The locale was set as follows: ' + loc ); } );

      

+2


source


Try this code

When the browser is set to the en_US locale, this should display a popup dialog with text language: English:

navigator.globalization.getPreferredLanguage (

function (language) {alert('language: ' + language.value + '\n');},
function () {alert('Error getting language\n');}

      

);

Complete example

<!DOCTYPE HTML>
<html>
  <head>
    <title>getPreferredLanguage Example</title>
    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">

    function checkLanguage() {
      navigator.globalization.getPreferredLanguage(
        function (language) {alert('language: ' + language.value + '\n');},
        function () {alert('Error getting language\n');}
      );
    }
    </script>
  </head>
  <body>
    <button onclick="checkLanguage()">Click for language</button>
  </body>
</html> 
      

Run codeHide result


0


source







All Articles