Big O
Big O

Reputation: 417

Detecting and Applying Current System Language on HTML 5 App on Android

I am working on a HTML5 app which uses PhoneGap and Backbone JS.

Problem is that Language changed on Android device through settings doesn't get in my HTML 5 app, though i can see it getting changed in independent Browser instance.

To support globalization of the app I am using i18n plugin to select current language for the app. My app works just fine with iOS. However when i try this on Android, language selected by user doesn't get reflected For example if i choose french as my language from system setting and try to see what is the value of window.navigator.language it remains en as oppose to fr, this gets reflected perfectly on iOS. Just for your information ,as recommended by PhoneGap, on Android I am calling

`public class myApp extends DroidGap {
    private String pageURl = "somepath";
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.loadUrl(pageUrl); // this will load my app's main html page.
    }
}`

Could somebody please help me in figuring out what the problem is?

Thanks Big O

Upvotes: 1

Views: 4076

Answers (2)

Tuomas Laatikainen
Tuomas Laatikainen

Reputation: 172

Selected answer doesn't seem to work with Cordova-Crosswalk project. Actually with Moto G2 and Cordova-Crosswalk in returns "99" because the userAgent is like this:

userAgent with Cordova-Crosswalk and Moto G2

I would recommend to use Cordova Globalization plugin or make own simple plugin with Java method: Locale locale = Locale.getDefault();

Upvotes: 1

dhaval
dhaval

Reputation: 7659

Try fetching language from user agent string with following patch, it should work:

    var lang;
    if (navigator
            && navigator.userAgent
            && (lang = navigator.userAgent
                    .match(/android.*\W(\w\w)-(\w\w)\W/i))) {
        lang = lang[1];
    }

    if (!lang && navigator) {
        if (navigator.language) {
            lang = navigator.language;
        } else if (navigator.browserLanguage) {
            lang = navigator.browserLanguage;
        } else if (navigator.systemLanguage) {
            lang = navigator.systemLanguage;
        } else if (navigator.userLanguage) {
            lang = navigator.userLanguage;
        }
        lang = lang.substr(0, 2);
    }

    console.log("current language is", lang);

Upvotes: 10

Related Questions