提交 312eb5a7 编写于 作者: D DCloud_LXH

feat(mp): 小程序端 getLocale 规范化

上级 b2fa9cd6
...@@ -16,15 +16,21 @@ import fr from './fr.json' ...@@ -16,15 +16,21 @@ import fr from './fr.json'
import zhHans from './zh-Hans.json' import zhHans from './zh-Hans.json'
import zhHant from './zh-Hant.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 = {} const messages = {}
if (__PLATFORM__ === 'h5' || __PLATFORM__ === 'app-plus') { if (__PLATFORM__ === 'h5' || __PLATFORM__ === 'app-plus') {
Object.assign(messages, { Object.assign(messages, {
en, [LOCALE_EN]: en,
es, [LOCALE_ES]: es,
fr, [LOCALE_FR]: fr,
'zh-Hans': zhHans, [LOCALE_ZH_HANS]: zhHans,
'zh-Hant': zhHant [LOCALE_ZH_HANT]: zhHant
}) })
} }
...@@ -39,7 +45,7 @@ if (__PLATFORM__ === 'h5') { ...@@ -39,7 +45,7 @@ if (__PLATFORM__ === 'h5') {
locale = '' locale = ''
} }
} else { } else {
locale = __GLOBAL__.getSystemInfoSync().language locale = normalizeLocale(__GLOBAL__.getSystemInfoSync().language) || LOCALE_EN
} }
function initI18nMessages () { function initI18nMessages () {
...@@ -95,7 +101,7 @@ export function initAppLocale (Vue, appVm, locale) { ...@@ -95,7 +101,7 @@ export function initAppLocale (Vue, appVm, locale) {
} }
Object.defineProperty(appVm, '$locale', { Object.defineProperty(appVm, '$locale', {
get () { get () {
return state.locale return normalizeLocale(state.locale)
}, },
set (v) { set (v) {
state.locale = v state.locale = v
...@@ -110,7 +116,7 @@ function getLocaleMessage () { ...@@ -110,7 +116,7 @@ function getLocaleMessage () {
const locale = uni.getLocale() const locale = uni.getLocale()
const locales = __uniConfig.locales const locales = __uniConfig.locales
return ( return (
locales[locale] || locales[__uniConfig.fallbackLocale] || locales.en || {} locales[locale] || locales[__uniConfig.fallbackLocale] || locales[LOCALE_EN] || {}
) )
} }
...@@ -190,6 +196,44 @@ export function initTabBarI18n (tabBar) { ...@@ -190,6 +196,44 @@ export function initTabBarI18n (tabBar) {
return 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() { // export function initI18n() {
// const localeKeys = Object.keys(__uniConfig.locales || {}) // const localeKeys = Object.keys(__uniConfig.locales || {})
// if (localeKeys.length) { // if (localeKeys.length) {
...@@ -197,4 +241,4 @@ export function initTabBarI18n (tabBar) { ...@@ -197,4 +241,4 @@ export function initTabBarI18n (tabBar) {
// i18n.add(locale, __uniConfig.locales[locale]) // i18n.add(locale, __uniConfig.locales[locale])
// ) // )
// } // }
// } // }
import { normalizeLocale, LOCALE_EN } from '../helpers/i18n/index'
export function getLocale () { export function getLocale () {
// 优先使用 $locale // 优先使用 $locale
const app = getApp({ const app = getApp({
...@@ -6,7 +8,7 @@ export function getLocale () { ...@@ -6,7 +8,7 @@ export function getLocale () {
if (app && app.$vm) { if (app && app.$vm) {
return app.$vm.$locale return app.$vm.$locale
} }
return __GLOBAL__.getSystemInfoSync().language || 'zh-Hans' return normalizeLocale(__GLOBAL__.getSystemInfoSync().language) || LOCALE_EN
} }
export function setLocale (locale) { export function setLocale (locale) {
...@@ -34,4 +36,4 @@ export function onLocaleChange (fn) { ...@@ -34,4 +36,4 @@ export function onLocaleChange (fn) {
if (typeof global !== 'undefined') { if (typeof global !== 'undefined') {
global.getLocale = getLocale global.getLocale = getLocale
} }
...@@ -6,7 +6,9 @@ import { ...@@ -6,7 +6,9 @@ import {
} from 'uni-wrapper/util' } from 'uni-wrapper/util'
import { import {
initAppLocale initAppLocale,
normalizeLocale,
LOCALE_EN
} from 'uni-helpers/i18n/index' } from 'uni-helpers/i18n/index'
import EventChannel from 'uni-helpers/EventChannel' import EventChannel from 'uni-helpers/EventChannel'
...@@ -185,7 +187,7 @@ export default function parseBaseApp (vm, { ...@@ -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) initHooks(appOptions, hooks)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册