修改国际版本

This commit is contained in:
2026-06-12 16:08:00 +08:00
parent af758a0ccc
commit 836cdaf2dc
38 changed files with 449 additions and 145 deletions
+54
View File
@@ -1,5 +1,59 @@
// i18n工具函数 - 用于在 Vue 3 setup 中安全获取 $t
import { getCurrentInstance } from 'vue'
import zhCN from '../locale/zh-CN.js'
import enUS from '../locale/en-US.js'
import idID from '../locale/id-ID.js'
const MESSAGE_MAP = {
'zh-CN': zhCN,
'en-US': enUS,
'id-ID': idID
}
const LANGUAGE_STORAGE_KEY = 'language'
export function getAppLocale() {
try {
const lang = uni.getStorageSync(LANGUAGE_STORAGE_KEY)
if (lang && MESSAGE_MAP[lang]) return lang
} catch (_) {}
return 'zh-CN'
}
function lookupMessage(locale, key) {
const segments = String(key).split('.')
let node = MESSAGE_MAP[locale]
for (const seg of segments) {
if (node == null || typeof node !== 'object') return null
node = node[seg]
}
return typeof node === 'string' ? node : null
}
/** 非 Vue 组件场景下的文案翻译 */
export function translate(key, values) {
const locale = getAppLocale()
let text = lookupMessage(locale, key) ?? lookupMessage('zh-CN', key) ?? key
if (values && typeof values === 'object') {
Object.entries(values).forEach(([k, v]) => {
text = text.split(`{${k}}`).join(String(v))
})
}
return text
}
/** uni.showModal 封装:默认使用多语言取消/确认按钮 */
export function showModalI18n(options = {}) {
const { cancelText, confirmText, showCancel = true, ...rest } = options
const modalOptions = {
...rest,
showCancel,
confirmText: confirmText ?? translate('common.confirm')
}
if (showCancel !== false) {
modalOptions.cancelText = cancelText ?? translate('common.cancel')
}
return uni.showModal(modalOptions)
}
/**
* 在 setup 中使用 i18n