|
|
import { createI18n } from 'vue-i18n'
|
|
|
import zhCN from './zh-CN'
|
|
|
import enUS from './en-US'
|
|
|
|
|
|
const LOCALE_STORAGE_KEY = 'app_locale'
|
|
|
const LOCALE_CHANGE_EVENT = 'app-locale-changed'
|
|
|
const DEFAULT_LOCALE = 'zh-CN'
|
|
|
|
|
|
const messages = {
|
|
|
'zh-CN': zhCN,
|
|
|
'en-US': enUS
|
|
|
}
|
|
|
|
|
|
function unwrapMessageParam(value) {
|
|
|
if (value && typeof value === 'object' && 'value' in value) {
|
|
|
return value.value
|
|
|
}
|
|
|
return value
|
|
|
}
|
|
|
|
|
|
function normalizeMessageParams(params = {}) {
|
|
|
return Object.keys(params).reduce((result, key) => {
|
|
|
result[key] = unwrapMessageParam(params[key])
|
|
|
return result
|
|
|
}, {})
|
|
|
}
|
|
|
|
|
|
function replaceMessageParams(message, params = {}) {
|
|
|
if (typeof message !== 'string') return message
|
|
|
return Object.keys(params).reduce((result, key) => {
|
|
|
const pattern = new RegExp(`\\{\\s*${key}\\s*\\}`, 'g')
|
|
|
const value = params[key]
|
|
|
return result.replace(pattern, value == null ? '' : String(value))
|
|
|
}, message)
|
|
|
}
|
|
|
|
|
|
function normalizeLocale(locale) {
|
|
|
if (locale === 'zh' || locale === 'zh_CN' || locale === 'zh-Hans') return 'zh-CN'
|
|
|
if (locale === 'en' || locale === 'en_US') return 'en-US'
|
|
|
return locale === 'en-US' ? 'en-US' : 'zh-CN'
|
|
|
}
|
|
|
|
|
|
function getSavedLocale() {
|
|
|
try {
|
|
|
const locale = uni.getStorageSync(LOCALE_STORAGE_KEY)
|
|
|
if (locale) {
|
|
|
return normalizeLocale(locale)
|
|
|
}
|
|
|
} catch (error) {
|
|
|
}
|
|
|
const systemLocale = typeof uni.getLocale === 'function' ? uni.getLocale() : DEFAULT_LOCALE
|
|
|
return normalizeLocale(systemLocale)
|
|
|
}
|
|
|
|
|
|
const i18n = createI18n({
|
|
|
legacy: false,
|
|
|
locale: getSavedLocale(),
|
|
|
fallbackLocale: DEFAULT_LOCALE,
|
|
|
globalInjection: true,
|
|
|
messages,
|
|
|
missingWarn: false,
|
|
|
fallbackWarn: false,
|
|
|
missing: (locale, key) => key
|
|
|
})
|
|
|
|
|
|
const rawGlobalTranslate = i18n.global.t.bind(i18n.global)
|
|
|
|
|
|
i18n.global.t = (...args) => {
|
|
|
const translated = rawGlobalTranslate(...args)
|
|
|
const paramsArg = args.find((arg) => arg && typeof arg === 'object' && !Array.isArray(arg))
|
|
|
if (!paramsArg) {
|
|
|
return translated
|
|
|
}
|
|
|
return replaceMessageParams(translated, normalizeMessageParams(paramsArg))
|
|
|
}
|
|
|
|
|
|
const literalMap = {
|
|
|
'首页': 'nav.home',
|
|
|
'报表': 'tab.report',
|
|
|
'管理': 'tab.work',
|
|
|
'我的': 'tab.mine',
|
|
|
'返回': 'dashboard.back',
|
|
|
'查询': 'functionCommon.search',
|
|
|
'取消': 'functionCommon.cancel',
|
|
|
'保存': 'functionCommon.save',
|
|
|
'加载中...': 'functionCommon.loading',
|
|
|
'正在加载更多...': 'functionCommon.loadingMore',
|
|
|
'没有更多数据了': 'functionCommon.noMoreData',
|
|
|
'缺少ID,无法查看详情': 'functionCommon.noIdView',
|
|
|
'缺少ID,无法编辑': 'functionCommon.noIdEdit',
|
|
|
'缺少ID,无法删除': 'functionCommon.noIdDelete',
|
|
|
'加载失败': 'functionCommon.loadFailed',
|
|
|
'删除成功': 'functionCommon.deleteSuccess',
|
|
|
'删除失败': 'functionCommon.deleteFailed',
|
|
|
'保存失败': 'functionCommon.saveFailed',
|
|
|
'新增成功': 'functionCommon.createSuccess',
|
|
|
'更新成功': 'functionCommon.updateSuccess',
|
|
|
'确认删除': 'functionCommon.confirmDelete',
|
|
|
'上传中': 'functionCommon.uploading',
|
|
|
'图片上传失败': 'functionCommon.uploadImageFailed',
|
|
|
'是': 'functionCommon.yes',
|
|
|
'否': 'functionCommon.no',
|
|
|
'暂无待办任务': 'dashboard.noTodo',
|
|
|
'模具出库': 'moldGet.moduleName',
|
|
|
'模具出库详情': 'moldGet.detailTitle',
|
|
|
'模具入库': 'moldReturn.moduleName',
|
|
|
'模具入库详情': 'moldReturn.detailTitle',
|
|
|
'上下模': 'moldOperate.moduleName',
|
|
|
'上下模详情': 'moldOperate.detailTitle',
|
|
|
'点检项库': 'moldInspectionItems.moduleName',
|
|
|
'点检项库详情': 'moldInspectionItems.detailTitle',
|
|
|
'点检模板': 'moldInspectionPlan.moduleName',
|
|
|
'点检模板详情': 'moldInspectionPlan.detailTitle',
|
|
|
'点检任务': 'moldTaskConfig.moduleName',
|
|
|
'点检任务详情': 'moldTaskConfig.detailTitle',
|
|
|
'设备点检任务': 'equipmentInspectionTasks.moduleName',
|
|
|
'设备点检任务详情': 'equipmentInspectionTasks.detailTitle',
|
|
|
'设备点检记录': 'equipmentInspectionRecord.moduleName',
|
|
|
'设备点检记录详情': 'equipmentInspectionRecord.detailTitle',
|
|
|
'点检记录': 'moldWorkOrder.moduleName',
|
|
|
'点检记录详情': 'moldWorkOrder.detailTitle'
|
|
|
}
|
|
|
|
|
|
export function getCurrentLocale() {
|
|
|
return i18n.global.locale.value
|
|
|
}
|
|
|
|
|
|
export function setLocale(locale) {
|
|
|
const nextLocale = normalizeLocale(locale)
|
|
|
i18n.global.locale.value = nextLocale
|
|
|
uni.setStorageSync(LOCALE_STORAGE_KEY, nextLocale)
|
|
|
uni.$emit(LOCALE_CHANGE_EVENT, nextLocale)
|
|
|
return nextLocale
|
|
|
}
|
|
|
|
|
|
export function translateLiteral(text) {
|
|
|
if (typeof text !== 'string') return text
|
|
|
const key = literalMap[text]
|
|
|
if (key) return i18n.global.t(key)
|
|
|
return text
|
|
|
}
|
|
|
|
|
|
export function translateWithParams(key, params = {}) {
|
|
|
const normalizedParams = normalizeMessageParams(params)
|
|
|
const translated = i18n.global.t(key, normalizedParams)
|
|
|
return replaceMessageParams(translated, normalizedParams)
|
|
|
}
|
|
|
|
|
|
export function initializeLocale() {
|
|
|
setLocale(getSavedLocale())
|
|
|
}
|
|
|
|
|
|
export function setNavigationTitle(key) {
|
|
|
uni.setNavigationBarTitle({
|
|
|
title: i18n.global.t(key)
|
|
|
})
|
|
|
}
|
|
|
|
|
|
export function onLocaleChange(callback) {
|
|
|
uni.$on(LOCALE_CHANGE_EVENT, callback)
|
|
|
}
|
|
|
|
|
|
export function offLocaleChange(callback) {
|
|
|
uni.$off(LOCALE_CHANGE_EVENT, callback)
|
|
|
}
|
|
|
|
|
|
export default i18n
|