chore: initial import

This commit is contained in:
liangjian
2026-06-29 09:42:53 +08:00
commit 9fea127de1
241 changed files with 82767 additions and 0 deletions

130
utils/i18n.js Normal file
View File

@@ -0,0 +1,130 @@
import store from '@/store'
import globalMessages from '@/locales/messages'
export const ZH_LOCALE = 'zh-Hans'
export const EN_LOCALE = 'en'
export const DEFAULT_LOCALE = ZH_LOCALE
function safeReadStorageLocale() {
try {
const lifeData = uni.getStorageSync('lifeData') || {}
return lifeData.vuex_locale || ''
} catch (error) {
return ''
}
}
export function normalizeLocale(locale) {
const normalized = String(locale || '').trim().toLowerCase()
if (!normalized) {
return DEFAULT_LOCALE
}
if (normalized === 'en' || normalized === 'en-us' || normalized === 'en_us') {
return EN_LOCALE
}
if (normalized === 'zh' || normalized === 'zh-cn' || normalized === 'zh_cn' || normalized === 'zh-hans') {
return ZH_LOCALE
}
return normalized.startsWith('en') ? EN_LOCALE : ZH_LOCALE
}
export function getLocaleShort(locale) {
return normalizeLocale(locale) === EN_LOCALE ? 'en' : 'zh'
}
export function getCurrentLocale() {
const stateLocale = store && store.state ? store.state.vuex_locale : ''
return normalizeLocale(stateLocale || safeReadStorageLocale())
}
export function getCurrentLanguage() {
return getLocaleShort(getCurrentLocale())
}
export function getNextLocale(locale) {
return normalizeLocale(locale) === EN_LOCALE ? ZH_LOCALE : EN_LOCALE
}
export function resolveLanguageHeader(locale) {
return getLocaleShort(locale) === 'en' ? 'en-US' : 'zh-CN'
}
export function formatI18nText(template, params = {}) {
return String(template || '').replace(/\{(\w+)\}/g, (match, key) => {
return Object.prototype.hasOwnProperty.call(params, key) ? params[key] : match
})
}
function getMessageText(messages = {}, locale, key) {
const shortLocale = getLocaleShort(locale)
const fallbackMessages = messages.zh || messages[ZH_LOCALE] || {}
const scopedMessages = messages[shortLocale] || messages[normalizeLocale(locale)] || fallbackMessages
if (scopedMessages[key] !== undefined) {
return scopedMessages[key]
}
return fallbackMessages[key]
}
export function translateMessages(messages = {}, locale, key, params = {}) {
const text = getMessageText(messages, locale, key)
return formatI18nText(text !== undefined ? text : key, params)
}
export function getMessageScope(messages = {}, namespace = '') {
if (!namespace) {
return messages
}
return String(namespace)
.split('.')
.filter(Boolean)
.reduce((current, segment) => {
if (!current || typeof current !== 'object') {
return undefined
}
return current[segment]
}, messages)
}
export function getGlobalMessages(namespace = '') {
if (!namespace) {
return globalMessages
}
const zhScope = getMessageScope(globalMessages.zh || globalMessages[ZH_LOCALE] || {}, namespace)
const enScope = getMessageScope(globalMessages.en || globalMessages[EN_LOCALE] || {}, namespace)
if (!zhScope && !enScope) {
return undefined
}
return {
zh: zhScope || {},
en: enScope || {}
}
}
export function translateGlobalMessages(namespace, locale, key, params = {}) {
const scopedMessages = getGlobalMessages(namespace)
if (!scopedMessages || typeof scopedMessages !== 'object') {
return key
}
return translateMessages(scopedMessages, locale, key, params)
}
export function getGlobalMessage(namespace, locale, key, params = {}) {
const scopedMessages = getGlobalMessages(namespace)
if (!scopedMessages || typeof scopedMessages !== 'object') {
return ''
}
const text = getMessageText(scopedMessages, locale, key)
return text === undefined ? '' : formatI18nText(text, params)
}
export function pickLocalizedText(options = {}) {
const locale = getLocaleShort(options.locale)
const zhText = options.zh !== undefined ? options.zh : ''
const enText = options.en !== undefined ? options.en : zhText
if (locale === 'en') {
return enText || options.fallback || zhText || ''
}
return zhText || options.fallback || enText || ''
}