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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14870587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:48:19  来源:igfitidea点击:

HTML5 Mobile app localization using javascript and PhoneGap

javascripthtmlcordovalocalization

提问by Thanushka

I'm creating a HTML5 mobile app that runs on all 3 mobile platforms (Android, iOS a,d Windows Mobile 8). I'm using javascript for localization(https://github.com/eligrey/l10n.js/#readme).

我正在创建一个可以在所有 3 个移动平台(Android、iOS 和 Windows Mobile 8)上运行的 HTML5 移动应用程序。我正在使用 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 启用本地化。

采纳答案by micadelli

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();

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

and then in Javascript side, when you've got that language name back, eg. en, you would use the JSON object that it named after that language. The example of JSON object would look like this:

然后在 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'
}

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;
        }
    }
});

回答by Ian Devlin

You can write your own JavaScript to grab the localisation language. This is the exact same reply I wrote elsewhere on here.

您可以编写自己的 JavaScript 来获取本地化语言。这与我在这里其他地方写的完全相同。

In general, the JavaScript window.navigator.languageusually works for iOS and newer Androids, but earlier versions of Android have it hardcoded to en.

一般来说,JavaScriptwindow.navigator.language通常适用于 iOS 和更新的 Android,但早期版本的 Android 将其硬编码为en.

For older Androids I suggest pulling the language parameter out of the UserAgent string (yes sniffing!), e.g.

对于较旧的 Android,我建议从 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 (e.g. '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,并且运行良好。

回答by Philzen

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 hyphento separate language and country - thus no matching culture is found and it comes up with the default fallback.

l10n (或在我的情况下,globalize.js)使用连字符来分隔语言和国家 - 因此没有找到匹配的文化,它提供了默认的回退。

Put a console.login your app to dump the locale string you are getting, then go the the console and check with adb logcatwhether this might be the case on your device. Then simply modify the string to allow matching (e.g. locale.value.replace('_', '-'))

将 aconsole.log放入您的应用程序中以转储您获得的区域设置字符串,然后进入控制台并检查adb logcat您的设备是否可能出现这种情况。然后简单地修改字符串以允许匹配(例如locale.value.replace('_', '-')