UNPKG

dm-vue3-ui

Version:

This Components Library will help get you started developing in Vue 3.

43 lines (42 loc) 974 B
import { createI18n } from "vue-i18n"; import { store } from "../utils"; import mZhLocale from "./lang/zh_CN"; import mEnLocale from "./lang/en_US"; const messages = { en_US: { ...mEnLocale }, zh_CN: { ...mZhLocale } }; let currentLang = store("lang") || store("currentLang") || "zh_CN"; const lang = currentLang === "zh_CN" ? mZhLocale : mEnLocale; const i18n = createI18n({ legacy: false, // 使用composition API locale: currentLang, //调用localstorage中存储的数据,或者默认赋值为”zh_CN“ globalInjection: true, // 表明使用全局t函数 messages }); let value = i18n.global.t; const $t = (path) => { const array = path.split("."); let current = lang; for (let i = 0, j = array.length; i < j; i++) { const property = array[i]; value = current[property]; if (i === j - 1) return value; if (!value) return ""; current = value; } return ""; }; export { $t, i18n as default };