feat: 非vue文件支持多语言

This commit is contained in:
菲鸽 2024-01-29 16:59:12 +08:00
parent 33061591a4
commit 6376816d0b
3 changed files with 31 additions and 1 deletions

View File

@ -9,9 +9,31 @@ const messages = {
} }
console.log(uni.getLocale()) 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({ const i18n = createI18n({
locale: 'zh', locale: getLocale(), //
messages, messages,
}) })
/**
* vue 使
* @param { string } localeKey keyeg: "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 export default i18n

View File

@ -39,6 +39,8 @@
<script lang="ts" setup> <script lang="ts" setup>
// //
import '@/utils/http'
const { safeAreaInsets } = uni.getSystemInfoSync() const { safeAreaInsets } = uni.getSystemInfoSync()
const author = ref('菲鸽') const author = ref('菲鸽')

View File

@ -1,6 +1,7 @@
/* eslint-disable no-param-reassign */ /* eslint-disable no-param-reassign */
import { useUserStore } from '@/store' import { useUserStore } from '@/store'
import { UserInfo } from '@/typings' import { UserInfo } from '@/typings'
import { translate as t } from '@/locale/index'
type Data<T> = { type Data<T> = {
code: number code: number
@ -8,6 +9,11 @@ type Data<T> = {
result: T result: T
} }
uni.showModal({
title: '菲鸽',
content: t('app.name'),
})
// 请求基地址 // 请求基地址
const baseURL = import.meta.env.VITE_SERVER_BASEURL const baseURL = import.meta.env.VITE_SERVER_BASEURL
// console.log(import.meta.env) // console.log(import.meta.env)