elementUI分页组件国际化

以我的项目为例
elementUI分页组件国际化_第1张图片
语言文件夹下index.js文件中代码如下:

import elementEnLocale from 'element-ui/lib/locale/lang/en'; // element-ui lang
import elementZhLocale from 'element-ui/lib/locale/lang/zh-CN';
import en from './en';
import zh from './zh';

export default {
    en: {
        ...en,
        ...elementEnLocale
    },
    zh: {
        ...zh,
        ...elementZhLocale
    }
};

locales.js中的代码如下:

import Vue from "vue";
import VueI18n from "vue-i18n";
Vue.use(VueI18n,{
    i18n: function(path, options) {
        let value = i18n.t(path, options);
        if (value !== null && value !== undefined) {
            return value;
        }
        return '';
    }
});
const DEFAULT_LANG = "zh";
const LOCALE_KEY = "local_lang";
import messages from '@/assets/lang';
const i18n = new VueI18n({
    locale: localStorage.getItem(LOCALE_KEY) || DEFAULT_LANG,
    messages: messages,
    silentTranslationWarn: true
});
// 设置默认语言
i18n.$InitLang = function(lang) {
    if (lang === undefined) {
        lang = localStorage.getItem(LOCALE_KEY);
        if (messages[lang] === undefined) {
            lang = DEFAULT_LANG;
        }
    }
    localStorage.setItem(LOCALE_KEY, lang);
    Vue.config.lang = lang;
    i18n.locale = lang; 
};
i18n.$InitLang();
export default i18n;

main.js文件中多语言的核心代码如下:

import Vue from 'vue'
import ElementUI from 'element-ui'
// 多语言
import i18n from '@/plugins/locales';
Vue.use(ElementUI, {
    i18n: (key, value) => i18n.t(key, value)
})

在lang.vue文件中使用

// 元素上切换语言绑定的方法为checkLang

以上就是自己编辑内容及使用elementUI组件国际化的全部代码,欢迎指点或采纳!

你可能感兴趣的:(elementUI分页组件国际化)