From 312eb5a7c31e24198d3b317f92e04919204914d0 Mon Sep 17 00:00:00 2001 From: DCloud_LXH <283700113@qq.com> Date: Tue, 31 May 2022 15:06:27 +0800 Subject: [PATCH] =?UTF-8?q?feat(mp):=20=E5=B0=8F=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=E7=AB=AF=20getLocale=20=E8=A7=84=E8=8C=83=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/helpers/i18n/index.js | 62 ++++++++++++++++--- src/core/runtime/locale.js | 6 +- .../runtime/wrapper/app-base-parser.js | 6 +- 3 files changed, 61 insertions(+), 13 deletions(-) diff --git a/src/core/helpers/i18n/index.js b/src/core/helpers/i18n/index.js index 2a3cb9607..c37cdaa88 100644 --- a/src/core/helpers/i18n/index.js +++ b/src/core/helpers/i18n/index.js @@ -16,15 +16,21 @@ import fr from './fr.json' import zhHans from './zh-Hans.json' import zhHant from './zh-Hant.json' +export const LOCALE_ZH_HANS = 'zh-Hans' +export const LOCALE_ZH_HANT = 'zh-Hant' +export const LOCALE_EN = 'en' +export const LOCALE_FR = 'fr' +export const LOCALE_ES = 'es' + const messages = {} if (__PLATFORM__ === 'h5' || __PLATFORM__ === 'app-plus') { Object.assign(messages, { - en, - es, - fr, - 'zh-Hans': zhHans, - 'zh-Hant': zhHant + [LOCALE_EN]: en, + [LOCALE_ES]: es, + [LOCALE_FR]: fr, + [LOCALE_ZH_HANS]: zhHans, + [LOCALE_ZH_HANT]: zhHant }) } @@ -39,7 +45,7 @@ if (__PLATFORM__ === 'h5') { locale = '' } } else { - locale = __GLOBAL__.getSystemInfoSync().language + locale = normalizeLocale(__GLOBAL__.getSystemInfoSync().language) || LOCALE_EN } function initI18nMessages () { @@ -95,7 +101,7 @@ export function initAppLocale (Vue, appVm, locale) { } Object.defineProperty(appVm, '$locale', { get () { - return state.locale + return normalizeLocale(state.locale) }, set (v) { state.locale = v @@ -110,7 +116,7 @@ function getLocaleMessage () { const locale = uni.getLocale() const locales = __uniConfig.locales return ( - locales[locale] || locales[__uniConfig.fallbackLocale] || locales.en || {} + locales[locale] || locales[__uniConfig.fallbackLocale] || locales[LOCALE_EN] || {} ) } @@ -190,6 +196,44 @@ export function initTabBarI18n (tabBar) { return tabBar } +function include (str, parts) { + return !!parts.find((part) => str.indexOf(part) !== -1) +} + +function startsWith (str, parts) { + return parts.find((part) => str.indexOf(part) === 0) +} + +export function normalizeLocale (locale, messages) { + if (!locale) { + return + } + locale = locale.trim().replace(/_/g, '-') + if (messages && messages[locale]) { + return locale + } + locale = locale.toLowerCase() + if (locale === 'chinese') { + // 支付宝 + return LOCALE_ZH_HANS + } + if (locale.indexOf('zh') === 0) { + if (locale.indexOf('-hans') > -1) { + return LOCALE_ZH_HANS + } + if (locale.indexOf('-hant') > -1) { + return LOCALE_ZH_HANT + } + if (include(locale, ['-tw', '-hk', '-mo', '-cht'])) { + return LOCALE_ZH_HANT + } + return LOCALE_ZH_HANS + } + const lang = startsWith(locale, [LOCALE_EN, LOCALE_FR, LOCALE_ES]) + if (lang) { + return lang + } +} // export function initI18n() { // const localeKeys = Object.keys(__uniConfig.locales || {}) // if (localeKeys.length) { @@ -197,4 +241,4 @@ export function initTabBarI18n (tabBar) { // i18n.add(locale, __uniConfig.locales[locale]) // ) // } -// } +// } diff --git a/src/core/runtime/locale.js b/src/core/runtime/locale.js index ae6794ce9..2b358f323 100644 --- a/src/core/runtime/locale.js +++ b/src/core/runtime/locale.js @@ -1,3 +1,5 @@ +import { normalizeLocale, LOCALE_EN } from '../helpers/i18n/index' + export function getLocale () { // 优先使用 $locale const app = getApp({ @@ -6,7 +8,7 @@ export function getLocale () { if (app && app.$vm) { return app.$vm.$locale } - return __GLOBAL__.getSystemInfoSync().language || 'zh-Hans' + return normalizeLocale(__GLOBAL__.getSystemInfoSync().language) || LOCALE_EN } export function setLocale (locale) { @@ -34,4 +36,4 @@ export function onLocaleChange (fn) { if (typeof global !== 'undefined') { global.getLocale = getLocale -} +} diff --git a/src/platforms/mp-weixin/runtime/wrapper/app-base-parser.js b/src/platforms/mp-weixin/runtime/wrapper/app-base-parser.js index 718c6906d..d8659b99c 100644 --- a/src/platforms/mp-weixin/runtime/wrapper/app-base-parser.js +++ b/src/platforms/mp-weixin/runtime/wrapper/app-base-parser.js @@ -6,7 +6,9 @@ import { } from 'uni-wrapper/util' import { - initAppLocale + initAppLocale, + normalizeLocale, + LOCALE_EN } from 'uni-helpers/i18n/index' import EventChannel from 'uni-helpers/EventChannel' @@ -185,7 +187,7 @@ export default function parseBaseApp (vm, { }) } - initAppLocale(Vue, vm, __GLOBAL__.getSystemInfoSync().language || 'zh-Hans') + initAppLocale(Vue, vm, normalizeLocale(__GLOBAL__.getSystemInfoSync().language) || LOCALE_EN) initHooks(appOptions, hooks) -- GitLab