我刚接触CORDOVA 3.5.0。我使用Cordova开发了一个应用程序,我想在我的应用程序上实现国际化。 请建议如何做同样的事情。
提前致谢
答案 0 :(得分:0)
翻译的一些代码:
1.使用
app.util.I18n.translate(menu_title)
2.放在你的初始化部分
在init上获取cordova的默认语言:
navigator.globalization.getPreferredLanguage(
function (language) {
//alert(language.value);
app.common.language=language.value.toLowerCase().substring(0,2);
//alert(app.common.language);
if(app.util.I18n.getTranslationsNames().indexOf(app.common.language) == -1){
app.common.language = app.util.I18n.getDefaultLanguage();
}
inicializacionI18n();
},
function () {
app.common.language = app.util.I18n.getDefaultLanguage();
inicializacionI18n();
}
);
app.util.I18n = { singleton:是的,
config : {
defaultLanguage : 'en',
availableLanguages: ['en', 'es'],
translations : {
'en' : {
// Menu
'menu_title' : 'Welcome on board {0}',
'menu_guest' : 'guest'
},
'es' : {
// Menu
'menu_title' : 'Bienvenido a bordo {0}',
'menu_guest' : 'invitado',
}
}
},
constructor: function(config) {
this.initConfig(config);
this.callParent([config]);
},
getDefaultLanguage: function() {
return this.config.defaultLanguage;
},
getLanguage: function() {
if(app.common.language == undefined){
return this.config.defaultLanguage;
}
return app.common.language;
},
getTranslations: function() {
return this.config.translations;
},
getTranslationsNames: function() {
return this.config.availableLanguages;
},
translate: function (key) {
// Translate
//alert(key + "-" +app.common.language);
if(app.common.language == undefined){
app.common.language=this.getDefaultLanguage();
}
var translation = "[" + key + "]";
if (this.getTranslations()[app.common.language][key] === undefined) {
// Key not found in language : tries default one
if (this.getTranslations()[this.getDefaultLanguage()][key] !== undefined) {
translation = this.getTranslations()[this.getDefaultLanguage()][key];
}
} else {
// Key found
translation = this.getTranslations()[app.common.language][key];
}
// If there is more than one argument : format string
if (arguments.length > 1) {
var tokenCount = arguments.length - 2;
for( var token = 0; token <= tokenCount; token++ )
{
translation = translation.replace( new RegExp( "\\{" + token + "\\}", "gi" ), arguments[ token + 1 ] );
}
}
return translation;
},
format: function(text) {
var tokenCount = arguments.length - 2;
for( var token = 0; token <= tokenCount; token++ )
{
text = text.replace( new RegExp( "\\{" + token + "\\}", "gi" ), arguments[token + 1] );
}
return text;
}
};