From 9fa1655bf8fc001af062648f0af46c83d8fe791c Mon Sep 17 00:00:00 2001 From: handongxun Date: Fri, 15 Oct 2021 17:56:55 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20H5=20uni.setLocale=20=E6=8C=81=E4=B9=85?= =?UTF-8?q?=E5=8C=96=E5=AD=98=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/helpers/constants.js | 1 + src/core/helpers/i18n/index.js | 8 ++++++-- src/core/service/api/ui/locale.js | 6 ++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/core/helpers/constants.js b/src/core/helpers/constants.js index bec72ef81..24a14dc03 100644 --- a/src/core/helpers/constants.js +++ b/src/core/helpers/constants.js @@ -2,3 +2,4 @@ export const NAVBAR_HEIGHT = 44 export const TABBAR_HEIGHT = 50 // 576:landscape phones,768:tablets,992:desktops,1200:large desktops export const RESPONSIVE_MIN_WIDTH = 768 +export const DC_LOCALE = '__DC_LOCALE' diff --git a/src/core/helpers/i18n/index.js b/src/core/helpers/i18n/index.js index 778fbc2c2..311253148 100644 --- a/src/core/helpers/i18n/index.js +++ b/src/core/helpers/i18n/index.js @@ -4,7 +4,11 @@ import { } from '@dcloudio/uni-i18n' import { isStr -} from 'uni-shared' +} from 'uni-shared' + +import { + DC_LOCALE +} from '../constants' import en from './en.json' import es from './es.json' @@ -23,7 +27,7 @@ const messages = { let locale if (__PLATFORM__ === 'h5') { - locale = __uniConfig.locale || navigator.language + locale = (window.localStorage && localStorage[DC_LOCALE]) || __uniConfig.locale || navigator.language } else if (__PLATFORM__ === 'app-plus') { if (typeof weex === 'object') { locale = weex.requireModule('plus').getLanguage() diff --git a/src/core/service/api/ui/locale.js b/src/core/service/api/ui/locale.js index 0713493b0..6d360290a 100644 --- a/src/core/service/api/ui/locale.js +++ b/src/core/service/api/ui/locale.js @@ -4,6 +4,9 @@ import { import { i18n } from 'uni-helpers/i18n' +import { + DC_LOCALE +} from 'uni-helpers/constants' export function getLocale () { // 优先使用 $locale @@ -30,6 +33,9 @@ export function setLocale (locale) { ) }) weex.requireModule('plus').setLanguage(locale) + } + if (__PLATFORM__ === 'h5') { + window.localStorage && (localStorage[DC_LOCALE] = locale) } callbacks.forEach(callbackId => { invoke(callbackId, { locale }) -- GitLab