feat:国际化多语言适配
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
// i18n工具函数 - 用于在 Vue 3 setup 中安全获取 $t
|
||||
import { getCurrentInstance } from 'vue'
|
||||
|
||||
/**
|
||||
* 在 setup 中使用 i18n
|
||||
* @returns {{ t: Function, locale: string, i18n: object }}
|
||||
*/
|
||||
export function useI18n() {
|
||||
const instance = getCurrentInstance()
|
||||
|
||||
if (!instance || !instance.proxy) {
|
||||
return {
|
||||
t: (key) => key,
|
||||
locale: 'zh-CN',
|
||||
i18n: null
|
||||
}
|
||||
}
|
||||
|
||||
const proxy = instance.proxy
|
||||
|
||||
// 返回一个函数,每次调用时动态获取 $t(确保 $t 已经注入)
|
||||
return {
|
||||
t: (key, ...args) => {
|
||||
if (proxy.$t && typeof proxy.$t === 'function') {
|
||||
return proxy.$t(key, ...args)
|
||||
}
|
||||
return key
|
||||
},
|
||||
locale: proxy.$i18n?.locale || 'zh-CN',
|
||||
i18n: proxy.$i18n
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user