cordova多语言(国际化,本地化,全球化)

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;   //1:中文简体   2:英语

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国际化(本地化)多语言

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页