From 6376816d0bd093989e33ac09ae8f835ae679fd4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8F=B2=E9=B8=BD?= <1020103647@qq.com> Date: Mon, 29 Jan 2024 16:59:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=9D=9Evue=E6=96=87=E4=BB=B6=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=A4=9A=E8=AF=AD=E8=A8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/locale/index.ts | 24 +++++++++++++++++++++++- src/pages/index/index.vue | 2 ++ src/utils/http.ts | 6 ++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/locale/index.ts b/src/locale/index.ts index 0a85193..1e97ddf 100644 --- a/src/locale/index.ts +++ b/src/locale/index.ts @@ -9,9 +9,31 @@ const messages = { } console.log(uni.getLocale()) +const getLocale = () => { + const browserLang = uni.getLocale() + if (Object.keys(messages).includes(browserLang)) { + return browserLang + } + return 'zh' // fallback language, 可以配置,必须是 message 的key +} + +console.log(getLocale()) + const i18n = createI18n({ - locale: 'zh', + locale: getLocale(), // messages, }) +/** + * 非 vue 文件使用这个方法 + * @param { string } localeKey 多语言的key,eg: "app.name" + */ +export const translate = (localeKey: string) => { + const locale = getLocale() + const message = messages[locale] + if (Object.keys(message).includes(localeKey)) { + return message[localeKey] + } + return localeKey +} export default i18n diff --git a/src/pages/index/index.vue b/src/pages/index/index.vue index 004264d..9b5859e 100644 --- a/src/pages/index/index.vue +++ b/src/pages/index/index.vue @@ -39,6 +39,8 @@