简体   繁体   English

使用javascript和PhoneGap进行HTML5移动应用本地化

[英]HTML5 Mobile app localization using javascript and PhoneGap

I'm creating a HTML5 mobile app that runs on all 3 mobile platforms (Android, iOS a,d Windows Mobile 8). 我正在创建一个可在所有3个移动平台(Android,iOS a和Windows Mobile 8)上运行的HTML5移动应用程序。 I'm using javascript for localization( https://github.com/eligrey/l10n.js/#readme ). 我正在使用javascript进行本地化( https://github.com/eligrey/l10n.js/#readme )。

The app works fine on the browser. 该应用程序在浏览器上正常工作。 But when I deploy it on the mobile emulator the localization is not working. 但是当我在移动模拟器上部署它时,本地化不起作用。

I think the issue is that javascript gets language information from the browser, but in the mobile we run the HTML5 using PhoneGap. 我认为问题是javascript从浏览器获取语言信息,但在移动设备中我们使用PhoneGap运行HTML5。

Is there any way that I can enable localization using javascript in PhoeGap. 有什么方法可以在PhoeGap中使用javascript启用本地化。

You can write your own JavaScript to grab the localisation language. 您可以编写自己的JavaScript来获取本地化语言。 This is the exact same reply I wrote elsewhere on here. 这是我在其他地方写的完全相同的回复。

In general, the JavaScript window.navigator.language usually works for iOS and newer Androids, but earlier versions of Android have it hardcoded to en . 通常,JavaScript window.navigator.language通常适用于iOS和更新的Androids,但早期版本的Android将其硬编码为en

For older Androids I suggest pulling the language parameter out of the UserAgent string (yes sniffing!), eg 对于较旧的机器人,我建议从UserAgent字符串中提取语言参数(是的,嗅探!),例如

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

Here, lang might also contain the country code (eg 'en-IE') so you might have to remove that also: 在这里,lang可能还包含国家/地区代码(例如'en-IE'),因此您可能还必须删除它:

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

This is what I've used in a recent app, using HTML5 and PhoneGap, and it works fine. 这是我在最近的应用程序中使用的HTML5和PhoneGap,它工作正常。

I've just solved same kind of problem by creating a custom PhoneGap plugins for each platforms that only returns the user's current locale. 我刚刚通过为每个只返回用户当前语言环境的平台创建自定义PhoneGap插件解决了同样的问题。

for example, on Android, the plugin only checks: 例如,在Android上,该插件仅检查:

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

and then in Javascript side, when you've got that language name back, eg. 然后在Javascript方面,当你有这个语言名称时,例如。 en , you would use the JSON object that it named after that language. en ,你可以使用它是语言命名的JSON对象。 The example of JSON object would look like this: JSON对象的示例如下所示:

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

The plugin for Android is simple as this: Android的插件很简单,因为:

JS file JS文件

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

Java file 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.");
        }
    }

}

And finally, in your main JS file, you set your app's language: 最后,在您的主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;
        }
    }
});

There's one common pitfall with localisation, maybe this is your problem: 本地化有一个常见的陷阱,也许这是你的问题:
Many devices report locales in the form of de_DE , en_GB , etc... - note the underscore . 许多设备以de_DEen_GB等形式报告语言环境... - 请注意下划线

l10n (or in my case, globalize.js ) use a hyphen to separate language and country - thus no matching culture is found and it comes up with the default fallback. l10n(或者在我的情况下, globalize.js )使用连字符来分隔语言和国家 - 因此没有找到匹配的文化,它提供了默认的回退。

Put a console.log in your app to dump the locale string you are getting, then go the the console and check with adb logcat whether this might be the case on your device. 在您的应用程序中放置console.log以转储您获得的区域设置字符串,然后转到控制台并使用adb logcat检查您的设备上是否存在这种情况。 Then simply modify the string to allow matching (eg locale.value.replace('_', '-') ) 然后只需修改字符串以允许匹配(例如locale.value.replace('_', '-')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM