2

I'm developing a Phonegap application using jQuery Mobile. It's a very basic application, its purpose is to show information about a big organization in Spanish and English. On the first page the application shows 2 options, Spanish and English. If the user selects Spanish, the information displayed must be Spanish and vice versa.

Using SQLite DB will probably give some problems on Windows Phones since it is not yet supported (see Phonegap Storage).

There is the File Storage option too. And raw JSON files, as well.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
jorgevenegas
  • 101
  • 1
  • 5

2 Answers2

3

The way I do it is to create a language specific json file to hold all my strings. In this case english.json and spanish.json. Structure the json like:

{ 
    help: "Help",
    ok: "Okay"
}

On the first page of your app when the user clicks the Spanish button for instance it should set a value in localStorage.

localStorage.setItem("lang", "spanish");

Then in the second page once you get the "deviceready" event you should load the correct json file using XHR.

var request = new XMLHttpRequest();
    request.open("GET", localStorage.getItem("lang") + ".json", true);
    request.onreadystatechange = function(){//Call a function when the state changes.
        if (request.readyState == 4) {
            if (request.status == 200 || request.status == 0) {
                langStrings = JSON.parse(request.responseText);
            }
        }
    }
    request.send();

Now whenever you want to use a translated string you get it from langStrings.

langStrings.ok;

Make sense?

Simon MacDonald
  • 23,253
  • 5
  • 58
  • 74
  • Finally i wrote 2 files with the contents in spanish and english. Then i used localStorage to set the preferences at the index.html. Thank you, your answer has guided me. – jorgevenegas Dec 18 '12 at 14:30
  • Glad I was able to help. I think I should write a small bit of code to make this easier for folks. – Simon MacDonald Dec 19 '12 at 19:21
  • dont want to be a problem spotter but will this work when i have to make an app with offline support? – Bhavik Shah Oct 16 '13 at 05:17
0

For persistance I successfully used Html5 Local Storage. It works on Android, iOS and Windows Phone 7(I tried it on these platforms).
I use it like this.

For i18n you can use any javascript i18n library. I created own simple solution.

Stan
  • 4,169
  • 2
  • 31
  • 39