Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wychl
uni-app
提交
312eb5a7
U
uni-app
项目概览
wychl
/
uni-app
与 Fork 源项目一致
Fork自
DCloud / uni-app
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
312eb5a7
编写于
5月 31, 2022
作者:
D
DCloud_LXH
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(mp): 小程序端 getLocale 规范化
上级
b2fa9cd6
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
61 addition
and
13 deletion
+61
-13
src/core/helpers/i18n/index.js
src/core/helpers/i18n/index.js
+53
-9
src/core/runtime/locale.js
src/core/runtime/locale.js
+4
-2
src/platforms/mp-weixin/runtime/wrapper/app-base-parser.js
src/platforms/mp-weixin/runtime/wrapper/app-base-parser.js
+4
-2
未找到文件。
src/core/helpers/i18n/index.js
浏览文件 @
312eb5a7
...
@@ -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])
// )
// )
// }
// }
// }
// }
src/core/runtime/locale.js
浏览文件 @
312eb5a7
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
}
}
src/platforms/mp-weixin/runtime/wrapper/app-base-parser.js
浏览文件 @
312eb5a7
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录