HTML5 Mobile应用程序本地化使用javascript和PhoneGap [英] HTML5 Mobile app localization using javascript and PhoneGap
问题描述
我正在创建一个可在所有3个移动平台(Android,iOS a,d Windows Mobile 8)上运行的HTML5移动应用程序。我使用javascript进行本地化( https://github.com/eligrey/l10n.js/#readme)。
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).
该应用程序在浏览器上正常工作。但是当我在移动模拟器上部署它时,本地化不工作。
The app works fine on the browser. But when I deploy it on the mobile emulator the localization is not working.
我认为问题是javascript从浏览器获取语言信息,但在移动中我们使用PhoneGap运行HTML5。
I think the issue is that javascript gets language information from the browser, but in the mobile we run the HTML5 using PhoneGap.
有什么方法可以使用Javascript在PhoeGap中进行本地化。
Is there any way that I can enable localization using javascript in PhoeGap.
推荐答案
我刚刚解决了同样的问题,通过为每个只返回用户当前区域设置的平台创建一个自定义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.
,该插件只检查:
var message = Locale.getDefault()。getLanguage();
然后在Javascript端,当你有这个语言名称,例如。 en
,您将使用以该语言命名的JSON对象。 JSON对象的示例如下:
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:
MyApp.Language = en: {
'Player' : 'Player',
'Players' : 'Players',
'Not Set' : 'Not Set'
},
fi: {
'Player' : 'Pelaaja',
'Players' : 'Pelaajat',
'Not Set' : 'Ei määritetty'
}
这个Android插件很简单:
The plugin for Android is simple as this:
JS文件 b
$ b
JS file
window.localizeMe = {
getDefaultLocale: function( callback ) {
cordova.exec(
callback,
function(err) {
callback( 'Error: ' + err.code );
},
"LocalizeMe",
"getDefaultLocale",
[]);
}
}
Java文件 p>
Java file
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;
}
}
});
这篇关于HTML5 Mobile应用程序本地化使用javascript和PhoneGap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!