You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
besure_app/src/locales/index.js

151 lines
4.3 KiB
JavaScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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 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 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',
'点检记录': 'moldWorkOrder.moduleName',
'点检记录详情': 'moldWorkOrder.detailTitle'
}
function applyTabBarLanguage() {
try {
const pages = getCurrentPages()
if (!pages || pages.length === 0) return
const currentPage = pages[pages.length - 1]
if (!currentPage) return
const route = currentPage.route || ''
const tabBarPages = ['pages/index', 'pages/report', 'pages/work', 'pages/mine']
if (!tabBarPages.includes(route)) return
const labels = [
i18n.global.t('tab.home'),
i18n.global.t('tab.report'),
i18n.global.t('tab.work'),
i18n.global.t('tab.mine')
]
labels.forEach((text, index) => {
uni.setTabBarItem({
index,
text
})
})
} catch (e) {
}
}
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)
applyTabBarLanguage()
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 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