+

Search Tips   |   Advanced Search

Globalization of web services

Use the Cordova globalization method to get the user locale preference, and check what user locale is used.

In some situations, localized results are obtained by calling web services. The Cordova globalization method getLocaleName returns the user locale preference, which can be used in client-driven service calls.

The following listing shows how the user locale is used to collate a list of words. The locale of the returned word list can be checked to verify that the user locale was used or a substitute locale was used instead.


Listing: Locale-based service call

function getWords(){
  var services;
  require(
    ["dojox/rpc/Service"], function(Service){
      services = new Service({
        target: "{Your Web Service URL}",     transport: "POST",     envelope: "JSON-RPC-1.0",     contentType:
          "application/json",       services: {
            "sorter.getWordList": {
              returns: {"type": "object"},           parameters: [{"type": "string"}]
            }
          }
      });
    }
  );
  g11n.getLocaleName(
    function(locale){
      // invoke the JSON web service to get the list of sorted words
      var deferred = services.sorter.getWordList(locale.value);
      deferred.addCallback(
        function(result){
          var wordsView = dojo.byId("words");
          require(
            [
              "dojox/mobile/RoundRectList",           "dojox/mobile/ListItem",           "dojox/mobile/Heading"
            ],         function(RoundRectList, ListItem, Heading){
              var wordsList = new RoundRectList({
                id: "words_list"}).placeAt(wordsView);
              items = result.words.list;
              for (var i = 0; i < items.length; ++i) {
                var word = new ListItem({label: items[i]});
                wordsList.addChild(word);
              }
              var wordsFooter = new Heading({
                label: result.localeName}).placeAt(wordsView);
            });
        }
      )
    }, function(code){
      alert("Error: " + code);
    }
  );
};


Parent topic: Develop globalized hybrid applications