文章目录
1.添加插件1.1 添加插件1.2 添加插件中报 npm ERR! 的解决方案1.3 插件使用的演示1.4 插件在github上地址
2.相关实现代码language.jschinese.jsenglish.jsother.js
3.iOS端项目名实现多语言4.安卓端项目名实现多语言5.参考博客
1.添加插件
1.1 添加插件
cordova plugin add cordova-plugin-globalization
1.2 添加插件中报 npm ERR! 的解决方案
npm ERR! request to https://registry.cnpmjs.org/cordova-plugin-globalization failed
1.3 插件使用的演示
Cordova国际化
1.4 插件在github上地址
github - cordova-plugin-globalization
2.相关实现代码
language.js
$(function () {
if (localStorage
.getItem("languageMy")) {
languageMy
= localStorage
.getItem("languageMy")
if(languageMy
== 1){
Chinese
.done()
}else{
English
.done()
}
} else {
getLanguage()
}
})
var languageMy
= 1;
function getLanguage() {
navigator
.globalization
.getPreferredLanguage(onSuccess
, onError
);
function onSuccess(language
) {
if (language
.value
.indexOf('zh') != -1) {
languageMy
= 1
Chinese
.done()
} else {
languageMy
= 2
English
.done()
}
}
function onError() {
alert('Error getting language');
}
}
$(".lantR").on("click", function () {
let className
= $(".lantR")[0].className
if (className
.indexOf("choosed") != -1) {
Push
.home();
if (languageMy
== 1) {
languageMy
= 2
English
.done()
} else {
languageMy
= 1
Chinese
.done()
}
localStorage
.setItem("languageMy", languageMy
)
}
})
$(".hmL9").on("click", function () {
Push
.language();
if (languageMy
== 1) {
$(".lanItemIcon").removeClass("choosed")
$(".lanItemIcon0").addClass("choosed")
} else {
$(".lanItemIcon").removeClass("choosed")
$(".lanItemIcon1").addClass("choosed")
}
})
$(".lanItem").on("click", function () {
let index
= $(this).index() / 2;
if (index
== 0) {
$(".lanItemIcon").removeClass("choosed")
$(".lanItemIcon0").addClass("choosed")
} else {
$(".lanItemIcon").removeClass("choosed")
$(".lanItemIcon1").addClass("choosed")
}
if (languageMy
- 1 != index
) {
$(".lantR").addClass("choosed")
} else {
$(".lantR").removeClass("choosed")
}
})
chinese.js
Chinese
= {}
Language
= {}
Language
.editing
Language
.done
Chinese
.done = function () {
Language
.editing
= "编辑"
Language
.done
= "完成"
$(".lantSpan").html("语言设置")
}
english.js
English
= {}
English
.done = function () {
Language
.editing
= "Editing"
Language
.done
= "Done"
$(".lantSpan").html("Language")
}
other.js
$(".mltR").on("click",function(){
if($(".mltR").html()==Language
.editing
){
$(".mltR").html(Language
.done
);
}else{
$(".mltR").html(Language
.editing
);
}
})
3.iOS端项目名实现多语言
打开iOS端的项目,参照下面的博客 swift ios的国际化(本地化,全球化,多语言)
4.安卓端项目名实现多语言
打开安卓端的项目,参照下面的博客。 注意:values-zh在语言选项里找不到,只能硬写。写values-zh没用
Android 多语言适配
5.参考博客
cordova国际化(本地化)多语言