import en from './en.json'//英语语言包
|
|
import zhHans from './zh-Hans.json'//英语语言包
|
|
import Vue from 'vue'
|
|
import VueI18n from 'vue-i18n'
|
|
|
|
// 国际化字段映射
|
|
import objectLangMap from './objectLangMap'
|
|
Vue.use(VueI18n)
|
|
|
|
|
|
// 获取系统语言
|
|
const systemInfo = uni.getSystemInfoSync();
|
|
const systemLang = 'en';
|
|
// const systemLang = systemInfo.language || 'en';
|
|
|
|
console.log(systemInfo.language, systemLang);
|
|
|
|
// 语言映射,将系统语言映射到我们支持的语言包
|
|
const langMap = {
|
|
'zh': 'zh-Hans',
|
|
'zh_CN': 'zh-Hans',
|
|
'zh-Hans': 'zh-Hans',
|
|
'zh-Hant': 'zh-Hans', // 暂时都映射到简体中文
|
|
'zh-TW': 'zh-Hans',
|
|
'zh-HK': 'zh-Hans',
|
|
'en': 'en',
|
|
'en-US': 'en',
|
|
'en-GB': 'en'
|
|
};
|
|
|
|
|
|
// 获取语言:优先缓存 -> 系统语言 -> 默认英语
|
|
const lang = uni.getStorageSync('language') || langMap[systemLang] || 'en';
|
|
// const lang = 'en';
|
|
// VueI18n构造函数所需要的配置
|
|
const i18nConfig = {
|
|
locale: lang,//当前语言
|
|
// 所需要用的语言包
|
|
messages:{
|
|
en,
|
|
'zh-Hans' : zhHans,
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//将对象国际化
|
|
Vue.prototype.$ot = (obj, type, key) => {
|
|
// 如果对象不存在,返回空字符串
|
|
if (!obj) return '';
|
|
|
|
// 获取当前语言
|
|
const currentLang = i18n.locale;
|
|
|
|
// 获取字段映射配置
|
|
const typeConfig = objectLangMap[type];
|
|
if (!typeConfig) return obj[key] || '';
|
|
|
|
const fieldConfig = typeConfig[key];
|
|
if (!fieldConfig) return obj[key] || '';
|
|
|
|
// 获取当前语言对应的字段名
|
|
const fieldName = fieldConfig[currentLang];
|
|
if (!fieldName) return obj[key] || '';
|
|
|
|
// 返回对象中对应字段的值,如果不存在则返回默认字段的值
|
|
return obj[fieldName] || obj[key] || '';
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const i18n = new VueI18n(i18nConfig)
|
|
export default i18n
|