feat: 非vue文件支持多语言
This commit is contained in:
parent
33061591a4
commit
6376816d0b
@ -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 多语言的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
|
export default i18n
|
||||||
|
@ -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('菲鸽')
|
||||||
|
@ -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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user