From da5e0bfbab4957b522d5e368b160a1c87c2fa545 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=9D=E5=85=88=E7=91=9E?= <1490493387@qq.com> Date: Thu, 24 Feb 2022 00:18:41 +0800 Subject: [PATCH] =?UTF-8?q?feat(lang/index.ts):=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=92=8C=E5=88=9B=E5=BB=BAi18n=E5=AE=9E=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lang/index.ts | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/lang/index.ts diff --git a/src/lang/index.ts b/src/lang/index.ts new file mode 100644 index 00000000..834384ab --- /dev/null +++ b/src/lang/index.ts @@ -0,0 +1,44 @@ +import {createI18n} from 'vue-i18n' +import {localStorage} from '@/utils/storage' + +// Element Plus 国际化配置 +import elementPlusLocaleZhCn from 'element-plus/es/locale/lang/zh-cn' +import elementPlusLocaleEn from 'element-plus/es/locale/lang/en' + +// 自定义国际化配置 +import enLocale from './en' +import zhCnLocale from './zh-cn' +import {Locale, Path} from "@intlify/core-base"; + +const messages = { + 'zh-cn': { + ...zhCnLocale, + ...elementPlusLocaleZhCn + }, + en: { + ...enLocale, + ...elementPlusLocaleEn + } +} + +export const getLanguage = () => { + let language = localStorage.get('language') + if (language) { + return language + } + language = navigator.language.toLowerCase() // 浏览器使用的语言 + const locales = Object.keys(messages) + for (const locale of locales) { + if (language.indexOf(locale) > -1) { + return locale + } + } + return 'zh-cn' +} + +const i18n = createI18n({ + locale: getLanguage(), + messages: messages +}) + +export default i18n \ No newline at end of file