165 lines
3.5 KiB
JavaScript
165 lines
3.5 KiB
JavaScript
import Vue from "vue";
|
|
// 引入语言包,注意路径
|
|
import enMessage from "./en.js";
|
|
import jpMessage from "./jp.js";
|
|
import koMessage from "./ko.js";
|
|
// VueI18n
|
|
import VueI18n from "@/common/i18n/vue-i18n.min.js";
|
|
import Request from "@/common/http/luch-request/index";
|
|
import config from "@/common/config";
|
|
const http = new Request();
|
|
http.setConfig((conf) => {
|
|
/* 设置全局配置 */
|
|
conf.baseURL = config.baseUrl;
|
|
conf.header = {
|
|
...conf.header,
|
|
};
|
|
return conf;
|
|
});
|
|
// VueI18n
|
|
Vue.use(VueI18n);
|
|
// 网络加载国际化译文
|
|
// 系统支持的语言
|
|
const supportLans = [
|
|
{
|
|
value: "zh",
|
|
label: "中文",
|
|
},
|
|
{
|
|
value: "en",
|
|
label: "English",
|
|
},
|
|
{
|
|
value: "ja",
|
|
label: "日本語",
|
|
},
|
|
{
|
|
value: "ko",
|
|
label: "한국어",
|
|
},
|
|
{
|
|
value: "zh_tw",
|
|
label: "繁體中文",
|
|
},
|
|
];
|
|
|
|
// 语言包
|
|
const messages = {
|
|
zh: {
|
|
// 'APP概要':'APP概要'
|
|
},
|
|
en: {},
|
|
ja: {},
|
|
ko: {},
|
|
};
|
|
|
|
// 获取租户支持的语言
|
|
export function getSupportLanOptions() {
|
|
const cookieSupportLans = uni.getStorageSync("language_info").value;
|
|
if (cookieSupportLans && cookieSupportLans.length > 0) {
|
|
const supportLanOptions = [];
|
|
for (let i = 0; i < supportLans.length; i++) {
|
|
if (cookieSupportLans.indexOf(supportLans[i].value) !== -1) {
|
|
supportLanOptions.push(supportLans[i]);
|
|
}
|
|
}
|
|
return supportLanOptions;
|
|
}
|
|
return supportLans;
|
|
}
|
|
|
|
// 获取默认语言
|
|
export function getDefaultLan() {
|
|
const cookieDefaultLan = uni.getStorageSync("language_info").value;
|
|
if (cookieDefaultLan) return cookieDefaultLan;
|
|
return "zh";
|
|
}
|
|
|
|
// 获取当前用户选择的语言
|
|
export function getLanguage() {
|
|
const chooseLanguage = uni.getStorageSync("language_info").value;
|
|
if (chooseLanguage) return chooseLanguage;
|
|
// if has not choose language
|
|
// const language = (navigator.language || navigator.browserLanguage).toLowerCase()
|
|
// const locales = Object.keys(messages)
|
|
// for (const locale of locales) {
|
|
// if (language.indexOf(locale) > -1) {
|
|
// return locale
|
|
// }
|
|
// }
|
|
return "zh";
|
|
}
|
|
|
|
// 重新加载国际化
|
|
export async function reloadMessage() {
|
|
const dict = await loadDict();
|
|
console.log("多语言接口", dict);
|
|
if (dict.status != 1) return;
|
|
|
|
console.log("lenmgth有的", data.length);
|
|
if (dict.data.length != 0) {
|
|
uni.setStorageSync("dict", dict.data);
|
|
}
|
|
regMessage(language_info.value, dict.data);
|
|
}
|
|
|
|
// 注册语言包
|
|
export async function regMessage(lan, lanDict) {
|
|
if (!lan || !lanDict) {
|
|
return;
|
|
}
|
|
if (lan === "zh") {
|
|
for (var key in lanDict) {
|
|
messages["zh"][key] = key;
|
|
}
|
|
// messages['zh'] = {
|
|
// ...lanDict
|
|
// }
|
|
} else if (lan === "en") {
|
|
messages["en"] = {
|
|
...lanDict,
|
|
};
|
|
} else if (lan === "ja") {
|
|
messages["ja"] = {
|
|
...lanDict,
|
|
};
|
|
} else if (lan === "ko") {
|
|
messages["ko"] = {
|
|
...lanDict,
|
|
};
|
|
} else if (lan === "ko") {
|
|
messages["ko"] = {
|
|
...lanDict,
|
|
};
|
|
} else if (lan === "zh_tw") {
|
|
messages["zh_tw"] = {
|
|
...lanDict,
|
|
};
|
|
}
|
|
}
|
|
|
|
// 网络加载国际化译文
|
|
export async function loadDict() {
|
|
uni.removeStorageSync("dict");
|
|
const url = `customer/lans`;
|
|
try {
|
|
return http.get(url).then((res) => res.data);
|
|
} catch (err) {
|
|
console.log(err);
|
|
return {};
|
|
}
|
|
}
|
|
reloadMessage();
|
|
const i18n = new VueI18n({
|
|
// set locale
|
|
// options: en | zh | ja
|
|
locale: getLanguage(),
|
|
// locale: 'zh',
|
|
// set locale messages
|
|
messages,
|
|
// 屏蔽console的warn
|
|
silentTranslationWarn: true,
|
|
});
|
|
|
|
export default i18n;
|