Localization of HTML5 mobile applications using javascript and PhoneGap

I am creating an HTML5 mobile application that works on all 3 mobile platforms (Android, iOS a, d Windows Mobile 8). I use javascript for localization ( https://github.com/eligrey/l10n.js/#readme ).

The application works fine in the browser. But when I deploy it on a mobile emulator, localization does not work.

I think the problem is that javascript gets language information from the browser, but on the mobile device we run HTML5 using PhoneGap.

Is there a way to enable localization using javascript in PhoeGap.

+5
source share
3 answers

​​ , PhoneGap , .

, Android, :

var message = Locale.getDefault().getLanguage();

Javascript, , . en, JSON, . JSON :

MyApp.Language = en: {
    'Player'  : 'Player',
    'Players' : 'Players',
    'Not Set' : 'Not Set'
},
fi: {
    'Player'  : 'Pelaaja',
    'Players' : 'Pelaajat',
    'Not Set' : 'Ei määritetty'
}

Android :

JS

window.localizeMe = {
    getDefaultLocale: function( callback ) {
        cordova.exec(
            callback,
            function(err) {
                callback( 'Error: ' + err.code );
            },
            "LocalizeMe",
            "getDefaultLocale",
            []);
    }
}

Java

public class LocalizeMe extends CordovaPlugin {
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        if (action.equals("getDefaultLocale")) {
            String message = Locale.getDefault().getLanguage();
            this.getDefaultLocale(message, callbackContext);
            return true;
        }
        return false;
    }

    private void getDefaultLocale(String message, CallbackContext callbackContext) {
        if (message != null && message.length() > 0) { 
            callbackContext.success(message);
        } else {
            callbackContext.error("Expected one non-empty string argument.");
        }
    }

}

, , JS :

window.localizeMe.getDefaultLocale( function( result ) {
    if ( result != null && result.length > 0 ) {
        if ( result.toLowerCase().indexOf( 'fi' ) !== -1 ) {
            MyApp.Lang = MyApp.Language.fi;
        } else {
            MyApp.Lang = MyApp.Language.en;
        }
    }
});
+2

JavaScript, . , .

, JavaScript window.navigator.language iOS , Android en.

UserAgent (, !),

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

lang (, "en-IE" ), :

if (lang.indexOf('-') != -1) lang = lang.substring(0, lang.indexOf('-'));

, , HTML5 PhoneGap, .

+3

, , :
de_DE, en_GB .. - .

l10n (or in my case globalize.js ) use a hyphen to separate the language and country - so no matching culture is found, and it encounters a default reserve.

Put a console.login your application to reset the locale string you get, then go to the console and check with the help adb logcatif it could be on your device. Then just change the line to allow matching (e.g., locale.value.replace('_', '-'))

+1
source

All Articles