unibest/src/locale/index.ts

38 lines
940 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { createI18n } from 'vue-i18n'
import en from './en.json'
import zhHans from './zh-Hans.json' // 简体中文
const messages = {
en,
'zh-Hans': zhHans, // key 不能乱写,查看截图 screenshots/i18n.png
}
const i18n = createI18n({
locale: uni.getLocale(), // 获取已设置的语言fallback 语言需要再 manifest.config.ts 中设置
messages,
})
console.log(uni.getLocale())
console.log(i18n.global.locale)
/**
* 非 vue 文件使用这个方法
* @param { string } localeKey 多语言的keyeg: "app.name"
*/
export const translate = (localeKey: string) => {
if (!localeKey) {
console.error(`[i18n] Function translate(), localeKey param is required`)
return ''
}
const locale = uni.getLocale()
console.log('locale:', locale)
const message = messages[locale]
if (Object.keys(message).includes(localeKey)) {
return message[localeKey]
}
return localeKey
}
export default i18n