Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wzj-java
uni-app
提交
2ec9088b
U
uni-app
项目概览
wzj-java
/
uni-app
与 Fork 源项目一致
Fork自
DCloud / uni-app
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
2ec9088b
编写于
9月 01, 2021
作者:
fxy060608
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
wip(i18n): sync locale
上级
513f4d1e
变更
15
隐藏空白更改
内联
并排
Showing
15 changed file
with
198 addition
and
201 deletion
+198
-201
packages/shims-vue-runtime.d.ts
packages/shims-vue-runtime.d.ts
+1
-0
packages/uni-api/src/service/ui/locale.ts
packages/uni-api/src/service/ui/locale.ts
+15
-6
packages/uni-app-plus/dist/uni-app-service.es.js
packages/uni-app-plus/dist/uni-app-service.es.js
+47
-49
packages/uni-app-plus/dist/uni-app-view.umd.js
packages/uni-app-plus/dist/uni-app-view.umd.js
+1
-1
packages/uni-app-plus/src/constants.ts
packages/uni-app-plus/src/constants.ts
+2
-0
packages/uni-app-plus/src/view/framework/subscriber/index.ts
packages/uni-app-plus/src/view/framework/subscriber/index.ts
+3
-1
packages/uni-core/src/i18n/useI18n.ts
packages/uni-core/src/i18n/useI18n.ts
+1
-26
packages/uni-core/src/service/init/index.ts
packages/uni-core/src/service/init/index.ts
+11
-1
packages/uni-h5/dist/uni-h5.cjs.js
packages/uni-h5/dist/uni-h5.cjs.js
+10
-3
packages/uni-h5/dist/uni-h5.es.js
packages/uni-h5/dist/uni-h5.es.js
+17
-8
packages/uni-i18n/dist/uni-i18n.cjs.js
packages/uni-i18n/dist/uni-i18n.cjs.js
+23
-30
packages/uni-i18n/dist/uni-i18n.d.ts
packages/uni-i18n/dist/uni-i18n.d.ts
+8
-10
packages/uni-i18n/dist/uni-i18n.es.js
packages/uni-i18n/dist/uni-i18n.es.js
+23
-30
packages/uni-i18n/src/I18n.ts
packages/uni-i18n/src/I18n.ts
+6
-11
packages/uni-i18n/src/vue-i18n.ts
packages/uni-i18n/src/vue-i18n.ts
+30
-25
未找到文件。
packages/shims-vue-runtime.d.ts
浏览文件 @
2ec9088b
...
...
@@ -12,6 +12,7 @@ declare module '@vue/runtime-core' {
getOpenerEventChannel
:
()
=>
EventChannel
$page
:
Page
.
PageInstance
[
'
$page
'
]
$mpType
?:
'
app
'
|
'
page
'
$locale
?:
string
__isTabBar
:
boolean
}
...
...
packages/uni-api/src/service/ui/locale.ts
浏览文件 @
2ec9088b
import
{
defineSyncApi
}
from
'
../../helpers/api
'
import
{
useI18n
}
from
'
@dcloudio/uni-core
'
import
{
BuiltInLocale
}
from
'
@dcloudio/uni-i18n
'
import
{
defineSyncApi
}
from
'
../../helpers/api
'
export
const
getLocale
=
defineSyncApi
<
typeof
uni
.
getLocale
>
(
'
getLocale
'
,
()
=>
{
const
i18n
=
useI18n
()
return
i18n
.
getLocale
()
// 优先使用 $locale
const
app
=
getApp
({
allowDefault
:
true
})
if
(
app
&&
app
.
$vm
)
{
return
app
.
$vm
.
$locale
}
return
useI18n
().
getLocale
()
}
)
export
const
setLocale
=
defineSyncApi
<
typeof
uni
.
setLocale
>
(
'
setLocale
'
,
(
locale
)
=>
{
const
i18n
=
useI18n
()
return
i18n
.
setLocale
(
locale
as
BuiltInLocale
)
getApp
().
$vm
.
$locale
=
locale
if
(
__PLATFORM__
===
'
app
'
)
{
const
pages
=
getCurrentPages
()
pages
.
forEach
((
page
)
=>
{
UniServiceJSBridge
.
publishHandler
(
'
setLocale
'
,
locale
,
page
.
$page
.
id
)
})
weex
.
requireModule
(
'
plus
'
).
setLanguage
(
locale
)
}
}
)
packages/uni-app-plus/dist/uni-app-service.es.js
浏览文件 @
2ec9088b
...
...
@@ -1483,42 +1483,36 @@ var serviceContext = (function (vue) {
}
const ignoreVueI18n = true;
function initLocaleWatcher(appVm, i18n) {
if (appVm.$i18n) {
const vm = appVm.$i18n.vm ? appVm.$i18n.vm : appVm;
vm.$watch(appVm.$i18n.vm ? 'locale' : () => appVm.$i18n.locale, (newLocale) => {
i18n.setLocale(newLocale);
}, {
immediate: true,
});
}
function watchAppLocale(appVm, i18n) {
appVm.$watch(() => appVm.$locale, (newLocale) => {
i18n.setLocale(newLocale);
});
}
// function getDefaultLocale() {
// if (typeof navigator !== 'undefined') {
// return (navigator as any).userLanguage || navigator.language
// }
// if (typeof plus !== 'undefined') {
// // TODO 待调整为最新的获取语言代码
// return plus.os.language
// }
// return uni.getSystemInfoSync().language
// }
const i18nInstances = [];
function initVueI18n(locale = LOCALE_EN, messages = {}, fallbackLocale = LOCALE_EN, watcher) {
function initVueI18n(locale, messages = {}, fallbackLocale, watcher) {
// 兼容旧版本入参
if (typeof locale !== 'string') {
[locale, messages] = [messages, locale];
[locale, messages] = [
messages,
locale,
];
}
if (typeof locale !== 'string') {
locale = fallbackLocale;
locale =
(typeof uni !== 'undefined' && uni.getLocale && uni.getLocale()) ||
LOCALE_EN;
}
if (typeof fallbackLocale !== 'string') {
fallbackLocale =
// @ts-expect-error
(typeof __uniConfig !== 'undefined' && __uniConfig.fallbackLocale) ||
LOCALE_EN;
}
const i18n = new I18n({
locale
: locale || fallbackLocale
,
locale,
fallbackLocale,
messages,
watcher,
});
i18nInstances.push(i18n);
let t = (key, values) => {
if (typeof getApp !== 'function') {
// app view
...
...
@@ -1529,17 +1523,19 @@ var serviceContext = (function (vue) {
}
else {
const appVm = getApp().$vm;
watchAppLocale(appVm, i18n);
if (!appVm.$t || !appVm.$i18n || ignoreVueI18n) {
// if (!locale) {
// i18n.setLocale(getDefaultLocale())
// }
/* eslint-disable no-func-assign */
t = function (key, values) {
// 触发响应式
appVm.$locale;
return i18n.t(key, values);
};
}
else {
initLocaleWatcher(appVm, i18n);
/* eslint-disable no-func-assign */
t = function (key, values) {
const $i18n = appVm.$i18n;
...
...
@@ -1574,10 +1570,7 @@ var serviceContext = (function (vue) {
return i18n.getLocale();
},
setLocale(newLocale) {
// 更新所有实例 locale
i18nInstances.forEach((ins) => {
ins.setLocale(newLocale);
});
return i18n.setLocale(newLocale);
},
};
}
...
...
@@ -1640,21 +1633,7 @@ var serviceContext = (function (vue) {
locale = plus.webview.currentWebview().getStyle().locale;
}
}
const SET_LOCALE_API = 'i18n.setLocale';
{
i18n = initVueI18n(locale, undefined, undefined, typeof getApp === 'function'
? (locale) => {
const pages = getCurrentPages();
pages.forEach((page) => {
UniServiceJSBridge.publishHandler(SET_LOCALE_API, locale, page.$page.id);
});
weex.requireModule('plus').setLanguage(locale);
}
: undefined);
}
if (typeof getApp !== 'function') {
UniViewJSBridge.subscribe(SET_LOCALE_API, i18n.setLocale);
}
i18n = initVueI18n(locale);
}
return i18n;
}
...
...
@@ -2225,6 +2204,15 @@ var serviceContext = (function (vue) {
function initAppVm(appVm) {
appVm.$vm = appVm;
appVm.$mpType = 'app';
const locale = vue.ref(useI18n().getLocale());
Object.defineProperty(appVm, '$locale', {
get() {
return locale.value;
},
set(v) {
locale.value = v;
},
});
}
function initPageVm(pageVm, page) {
pageVm.route = page.route;
...
...
@@ -4404,12 +4392,22 @@ var serviceContext = (function (vue) {
});
const getLocale = defineSyncApi('getLocale', () => {
const i18n = useI18n();
return i18n.getLocale();
// 优先使用 $locale
const app = getApp({ allowDefault: true });
if (app && app.$vm) {
return app.$vm.$locale;
}
return useI18n().getLocale();
});
const setLocale = defineSyncApi('setLocale', (locale) => {
const i18n = useI18n();
return i18n.setLocale(locale);
getApp().$vm.$locale = locale;
{
const pages = getCurrentPages();
pages.forEach((page) => {
UniServiceJSBridge.publishHandler('setLocale', locale, page.$page.id);
});
weex.requireModule('plus').setLanguage(locale);
}
});
const API_GET_SELECTED_TEXT_RANGE = 'getSelectedTextRange';
...
...
packages/uni-app-plus/dist/uni-app-view.umd.js
浏览文件 @
2ec9088b
因为 它太大了无法显示 source diff 。你可以改为
查看blob
。
packages/uni-app-plus/src/constants.ts
浏览文件 @
2ec9088b
...
...
@@ -20,3 +20,5 @@ export const WEBVIEW_REMOVED = 'webviewRemoved'
export
const
WEBVIEW_ID_PREFIX
=
'
webviewId
'
export
const
INIT_PAGE_SCROLL
=
'
initPageScroll
'
export
const
SET_LOCALE_API
=
'
setLocale
'
packages/uni-app-plus/src/view/framework/subscriber/index.ts
浏览文件 @
2ec9088b
import
{
VD_SYNC
}
from
'
../../../constants
'
import
{
useI18n
}
from
'
@dcloudio/uni-core
'
import
{
SET_LOCALE_API
,
VD_SYNC
}
from
'
../../../constants
'
import
{
onVdSync
}
from
'
../dom
'
export
function
initSubscribeHandlers
()
{
const
{
subscribe
}
=
UniViewJSBridge
subscribe
(
VD_SYNC
,
onVdSync
)
subscribe
(
SET_LOCALE_API
,
useI18n
().
setLocale
)
}
packages/uni-core/src/i18n/useI18n.ts
浏览文件 @
2ec9088b
...
...
@@ -84,32 +84,7 @@ export function useI18n() {
}
else
{
locale
=
uni
.
getSystemInfoSync
().
language
as
BuiltInLocale
}
const
SET_LOCALE_API
=
'
i18n.setLocale
'
if
(
__PLATFORM__
===
'
app
'
)
{
i18n
=
initVueI18n
(
locale
,
undefined
,
undefined
,
typeof
getApp
===
'
function
'
?
(
locale
)
=>
{
const
pages
=
getCurrentPages
()
pages
.
forEach
((
page
)
=>
{
UniServiceJSBridge
.
publishHandler
(
SET_LOCALE_API
,
locale
,
page
.
$page
.
id
)
})
weex
.
requireModule
(
'
plus
'
).
setLanguage
(
locale
)
}
:
undefined
)
}
else
{
i18n
=
initVueI18n
(
locale
)
}
if
(
__PLATFORM__
===
'
app
'
&&
typeof
getApp
!==
'
function
'
)
{
UniViewJSBridge
.
subscribe
(
SET_LOCALE_API
,
i18n
.
setLocale
)
}
i18n
=
initVueI18n
(
locale
)
}
return
i18n
}
packages/uni-core/src/service/init/index.ts
浏览文件 @
2ec9088b
import
{
ComponentPublicInstance
}
from
'
vue
'
import
{
ComponentPublicInstance
,
ref
}
from
'
vue
'
import
{
useI18n
}
from
'
../../i18n
'
import
{
initI18n
}
from
'
./i18n
'
import
{
initOn
}
from
'
./on
'
import
{
initSubscribe
}
from
'
./subscribe
'
...
...
@@ -14,6 +15,15 @@ export function initService() {
export
function
initAppVm
(
appVm
:
ComponentPublicInstance
)
{
appVm
.
$vm
=
appVm
appVm
.
$mpType
=
'
app
'
const
locale
=
ref
<
string
>
(
useI18n
().
getLocale
())
Object
.
defineProperty
(
appVm
,
'
$locale
'
,
{
get
()
{
return
locale
.
value
},
set
(
v
)
{
locale
.
value
=
v
},
})
}
export
function
initPageVm
(
...
...
packages/uni-h5/dist/uni-h5.cjs.js
浏览文件 @
2ec9088b
...
...
@@ -58,9 +58,7 @@ function useI18n() {
locale
=
uniShared
.
getEnvLocale
();
}
}
{
i18n
=
uniI18n
.
initVueI18n
(
locale
);
}
i18n
=
uniI18n
.
initVueI18n
(
locale
);
}
return
i18n
;
}
...
...
@@ -468,6 +466,15 @@ function initService() {
function
initAppVm
(
appVm2
)
{
appVm2
.
$vm
=
appVm2
;
appVm2
.
$mpType
=
"
app
"
;
const
locale
=
vue
.
ref
(
useI18n
().
getLocale
());
Object
.
defineProperty
(
appVm2
,
"
$locale
"
,
{
get
()
{
return
locale
.
value
;
},
set
(
v2
)
{
locale
.
value
=
v2
;
}
});
}
function
initPageVm
(
pageVm
,
page
)
{
pageVm
.
route
=
page
.
route
;
...
...
packages/uni-h5/dist/uni-h5.es.js
浏览文件 @
2ec9088b
import { withModifiers, createVNode, getCurrentInstance,
defineComponent, ref
, provide, computed, watch, onUnmounted, inject, onBeforeUnmount, mergeProps, injectHook, reactive, onActivated, onMounted, nextTick, onBeforeMount, withDirectives, vShow, shallowRef, watchEffect, isVNode, Fragment, markRaw, createTextVNode, onBeforeActivate, onBeforeDeactivate, openBlock, createBlock, renderList, onDeactivated, createApp, Transition, withCtx, KeepAlive, resolveDynamicComponent, createElementBlock, createElementVNode, normalizeStyle, renderSlot } from "vue";
import { withModifiers, createVNode, getCurrentInstance,
ref, defineComponent
, provide, computed, watch, onUnmounted, inject, onBeforeUnmount, mergeProps, injectHook, reactive, onActivated, onMounted, nextTick, onBeforeMount, withDirectives, vShow, shallowRef, watchEffect, isVNode, Fragment, markRaw, createTextVNode, onBeforeActivate, onBeforeDeactivate, openBlock, createBlock, renderList, onDeactivated, createApp, Transition, withCtx, KeepAlive, resolveDynamicComponent, createElementBlock, createElementVNode, normalizeStyle, renderSlot } from "vue";
import { isString, extend, stringifyStyle, parseStringStyle, isPlainObject, isFunction, isArray, hasOwn, isObject, capitalize, toRawType, makeMap as makeMap$1, isPromise, hyphenate, invokeArrayFns as invokeArrayFns$1 } from "@vue/shared";
import { I18N_JSON_DELIMITERS, once, passive, initCustomDataset, invokeArrayFns, resolveOwnerVm, resolveOwnerEl, ON_WXS_INVOKE_CALL_METHOD, normalizeTarget, ON_RESIZE, ON_APP_ENTER_FOREGROUND, ON_APP_ENTER_BACKGROUND, ON_SHOW, ON_HIDE, ON_PAGE_SCROLL, ON_REACH_BOTTOM, EventChannel, SCHEME_RE, DATA_RE, getCustomDataset, ON_ERROR, callOptions, ON_LAUNCH, PRIMARY_COLOR, removeLeadingSlash, getLen, debounce, UniLifecycleHooks, NAVBAR_HEIGHT, parseQuery, ON_UNLOAD, ON_REACH_BOTTOM_DISTANCE, decodedQuery, WEB_INVOKE_APPSERVICE, ON_WEB_INVOKE_APP_SERVICE, updateElementStyle, ON_BACK_PRESS, parseUrl, addFont, scrollTo, RESPONSIVE_MIN_WIDTH, formatDateTime, ON_PULL_DOWN_REFRESH } from "@dcloudio/uni-shared";
import { initVueI18n, isI18nStr, LOCALE_EN, LOCALE_ES, LOCALE_FR, LOCALE_ZH_HANS, LOCALE_ZH_HANT } from "@dcloudio/uni-i18n";
...
...
@@ -55,9 +55,7 @@ function useI18n() {
locale = __uniConfig.locale || navigator.language;
}
}
{
i18n = initVueI18n(locale);
}
i18n = initVueI18n(locale);
}
return i18n;
}
...
...
@@ -1345,6 +1343,15 @@ function initService() {
function initAppVm(appVm2) {
appVm2.$vm = appVm2;
appVm2.$mpType = "app";
const locale = ref(useI18n().getLocale());
Object.defineProperty(appVm2, "$locale", {
get() {
return locale.value;
},
set(v2) {
locale.value = v2;
}
});
}
function initPageVm(pageVm, page) {
pageVm.route = page.route;
...
...
@@ -4331,12 +4338,14 @@ const onWindowResize = /* @__PURE__ */ defineOnApi(API_ON_WINDOW_RESIZE, () => {
const offWindowResize = /* @__PURE__ */ defineOffApi(API_OFF_WINDOW_RESIZE, () => {
});
const getLocale = /* @__PURE__ */ defineSyncApi("getLocale", () => {
const i18n2 = useI18n();
return i18n2.getLocale();
const app = getApp({ allowDefault: true });
if (app && app.$vm) {
return app.$vm.$locale;
}
return useI18n().getLocale();
});
const setLocale = /* @__PURE__ */ defineSyncApi("setLocale", (locale) => {
const i18n2 = useI18n();
return i18n2.setLocale(locale);
getApp().$vm.$locale = locale;
});
const API_GET_SELECTED_TEXT_RANGE = "getSelectedTextRange";
const getSelectedTextRange$1 = /* @__PURE__ */ defineAsyncApi(API_GET_SELECTED_TEXT_RANGE, (_, { resolve, reject }) => {
...
...
packages/uni-i18n/dist/uni-i18n.cjs.js
浏览文件 @
2ec9088b
...
...
@@ -222,42 +222,36 @@ class I18n {
}
const
ignoreVueI18n
=
true
;
function
initLocaleWatcher
(
appVm
,
i18n
)
{
if
(
appVm
.
$i18n
)
{
const
vm
=
appVm
.
$i18n
.
vm
?
appVm
.
$i18n
.
vm
:
appVm
;
vm
.
$watch
(
appVm
.
$i18n
.
vm
?
'
locale
'
:
()
=>
appVm
.
$i18n
.
locale
,
(
newLocale
)
=>
{
i18n
.
setLocale
(
newLocale
);
},
{
immediate
:
true
,
});
}
function
watchAppLocale
(
appVm
,
i18n
)
{
appVm
.
$watch
(()
=>
appVm
.
$locale
,
(
newLocale
)
=>
{
i18n
.
setLocale
(
newLocale
);
});
}
// function getDefaultLocale() {
// if (typeof navigator !== 'undefined') {
// return (navigator as any).userLanguage || navigator.language
// }
// if (typeof plus !== 'undefined') {
// // TODO 待调整为最新的获取语言代码
// return plus.os.language
// }
// return uni.getSystemInfoSync().language
// }
const
i18nInstances
=
[];
function
initVueI18n
(
locale
=
LOCALE_EN
,
messages
=
{},
fallbackLocale
=
LOCALE_EN
,
watcher
)
{
function
initVueI18n
(
locale
,
messages
=
{},
fallbackLocale
,
watcher
)
{
// 兼容旧版本入参
if
(
typeof
locale
!==
'
string
'
)
{
[
locale
,
messages
]
=
[
messages
,
locale
];
[
locale
,
messages
]
=
[
messages
,
locale
,
];
}
if
(
typeof
locale
!==
'
string
'
)
{
locale
=
fallbackLocale
;
locale
=
(
typeof
uni
!==
'
undefined
'
&&
uni
.
getLocale
&&
uni
.
getLocale
())
||
LOCALE_EN
;
}
if
(
typeof
fallbackLocale
!==
'
string
'
)
{
fallbackLocale
=
// @ts-expect-error
(
typeof
__uniConfig
!==
'
undefined
'
&&
__uniConfig
.
fallbackLocale
)
||
LOCALE_EN
;
}
const
i18n
=
new
I18n
({
locale
:
locale
||
fallbackLocale
,
locale
,
fallbackLocale
,
messages
,
watcher
,
});
i18nInstances
.
push
(
i18n
);
let
t
=
(
key
,
values
)
=>
{
if
(
typeof
getApp
!==
'
function
'
)
{
// app view
...
...
@@ -268,17 +262,19 @@ function initVueI18n(locale = LOCALE_EN, messages = {}, fallbackLocale = LOCALE_
}
else
{
const
appVm
=
getApp
().
$vm
;
watchAppLocale
(
appVm
,
i18n
);
if
(
!
appVm
.
$t
||
!
appVm
.
$i18n
||
ignoreVueI18n
)
{
// if (!locale) {
// i18n.setLocale(getDefaultLocale())
// }
/* eslint-disable no-func-assign */
t
=
function
(
key
,
values
)
{
// 触发响应式
appVm
.
$locale
;
return
i18n
.
t
(
key
,
values
);
};
}
else
{
initLocaleWatcher
(
appVm
,
i18n
);
/* eslint-disable no-func-assign */
t
=
function
(
key
,
values
)
{
const
$i18n
=
appVm
.
$i18n
;
...
...
@@ -313,10 +309,7 @@ function initVueI18n(locale = LOCALE_EN, messages = {}, fallbackLocale = LOCALE_
return
i18n
.
getLocale
();
},
setLocale
(
newLocale
)
{
// 更新所有实例 locale
i18nInstances
.
forEach
((
ins
)
=>
{
ins
.
setLocale
(
newLocale
);
});
return
i18n
.
setLocale
(
newLocale
);
},
};
}
...
...
packages/uni-i18n/dist/uni-i18n.d.ts
浏览文件 @
2ec9088b
...
...
@@ -29,7 +29,7 @@ export declare class I18n {
private
formater
;
constructor
({
locale
,
fallbackLocale
,
messages
,
watcher
,
formater
,
}:
I18nOptions
);
setLocale
(
locale
:
string
):
void
;
getLocale
():
BuiltInLocale
;
getLocale
():
string
;
watchLocale
(
fn
:
LocaleWatcher
):
()
=>
void
;
add
(
locale
:
BuiltInLocale
,
message
:
Record
<
string
,
string
>
,
override
?:
boolean
):
void
;
f
(
message
:
string
,
values
?:
Record
<
string
,
unknown
>
|
Array
<
unknown
>
,
delimiters
?:
[
string
,
string
]):
string
;
...
...
@@ -38,21 +38,21 @@ export declare class I18n {
}
export
declare
interface
I18nOptions
{
locale
:
BuiltInLocale
;
fallbackLocale
?:
BuiltInLocale
;
locale
:
string
;
fallbackLocale
?:
string
;
messages
?:
LocaleMessages
;
formater
?:
Formatter_2
;
watcher
?:
LocaleWatcher
;
}
export
declare
function
initVueI18n
(
locale
?:
BuiltInLocale
,
messages
?:
LocaleMessages
,
fallbackLocale
?:
BuiltInLocale
,
watcher
?:
(
locale
:
BuiltInLocale
)
=>
void
):
{
export
declare
function
initVueI18n
(
locale
?:
string
,
messages
?:
LocaleMessages
,
fallbackLocale
?:
BuiltInLocale
,
watcher
?:
(
locale
:
string
)
=>
void
):
{
i18n
:
I18n
;
f
(
message
:
string
,
values
?:
Record
<
string
,
unknown
>
|
unknown
[]
|
undefined
,
delimiters
?:
[
string
,
string
]
|
undefined
):
string
;
t
(
key
:
string
,
values
?:
Record
<
string
,
unknown
>
|
unknown
[]
|
undefined
):
string
;
add
(
locale
:
BuiltInLocale
,
message
:
Record
<
string
,
string
>
,
override
?:
boolean
):
void
;
watch
(
fn
:
LocaleWatcher
):
()
=>
void
;
getLocale
():
BuiltInLocale
;
setLocale
(
newLocale
:
BuiltInLocale
):
void
;
getLocale
():
string
;
setLocale
(
newLocale
:
string
):
void
;
};
export
declare
function
isI18nStr
(
value
:
string
,
delimiters
:
[
string
,
string
]):
boolean
;
...
...
@@ -69,11 +69,9 @@ export declare const LOCALE_ZH_HANS = "zh-Hans";
export
declare
const
LOCALE_ZH_HANT
=
"
zh-Hant
"
;
export
declare
type
LocaleMessages
=
{
[
name
in
BuiltInLocale
]?:
Record
<
string
,
string
>
;
};
export
declare
type
LocaleMessages
=
Record
<
string
,
Record
<
string
,
string
>>
;
export
declare
type
LocaleWatcher
=
(
newLocale
:
BuiltInLocale
,
oldLocale
:
BuiltInLocale
)
=>
void
;
export
declare
type
LocaleWatcher
=
(
newLocale
:
string
,
oldLocale
:
string
)
=>
void
;
export
declare
function
parseI18nJson
(
jsonObj
:
unknown
,
values
:
Record
<
string
,
string
>
,
delimiters
:
[
string
,
string
]):
unknown
;
...
...
packages/uni-i18n/dist/uni-i18n.es.js
浏览文件 @
2ec9088b
...
...
@@ -218,42 +218,36 @@ class I18n {
}
const
ignoreVueI18n
=
true
;
function
initLocaleWatcher
(
appVm
,
i18n
)
{
if
(
appVm
.
$i18n
)
{
const
vm
=
appVm
.
$i18n
.
vm
?
appVm
.
$i18n
.
vm
:
appVm
;
vm
.
$watch
(
appVm
.
$i18n
.
vm
?
'
locale
'
:
()
=>
appVm
.
$i18n
.
locale
,
(
newLocale
)
=>
{
i18n
.
setLocale
(
newLocale
);
},
{
immediate
:
true
,
});
}
function
watchAppLocale
(
appVm
,
i18n
)
{
appVm
.
$watch
(()
=>
appVm
.
$locale
,
(
newLocale
)
=>
{
i18n
.
setLocale
(
newLocale
);
});
}
// function getDefaultLocale() {
// if (typeof navigator !== 'undefined') {
// return (navigator as any).userLanguage || navigator.language
// }
// if (typeof plus !== 'undefined') {
// // TODO 待调整为最新的获取语言代码
// return plus.os.language
// }
// return uni.getSystemInfoSync().language
// }
const
i18nInstances
=
[];
function
initVueI18n
(
locale
=
LOCALE_EN
,
messages
=
{},
fallbackLocale
=
LOCALE_EN
,
watcher
)
{
function
initVueI18n
(
locale
,
messages
=
{},
fallbackLocale
,
watcher
)
{
// 兼容旧版本入参
if
(
typeof
locale
!==
'
string
'
)
{
[
locale
,
messages
]
=
[
messages
,
locale
];
[
locale
,
messages
]
=
[
messages
,
locale
,
];
}
if
(
typeof
locale
!==
'
string
'
)
{
locale
=
fallbackLocale
;
locale
=
(
typeof
uni
!==
'
undefined
'
&&
uni
.
getLocale
&&
uni
.
getLocale
())
||
LOCALE_EN
;
}
if
(
typeof
fallbackLocale
!==
'
string
'
)
{
fallbackLocale
=
// @ts-expect-error
(
typeof
__uniConfig
!==
'
undefined
'
&&
__uniConfig
.
fallbackLocale
)
||
LOCALE_EN
;
}
const
i18n
=
new
I18n
({
locale
:
locale
||
fallbackLocale
,
locale
,
fallbackLocale
,
messages
,
watcher
,
});
i18nInstances
.
push
(
i18n
);
let
t
=
(
key
,
values
)
=>
{
if
(
typeof
getApp
!==
'
function
'
)
{
// app view
...
...
@@ -264,17 +258,19 @@ function initVueI18n(locale = LOCALE_EN, messages = {}, fallbackLocale = LOCALE_
}
else
{
const
appVm
=
getApp
().
$vm
;
watchAppLocale
(
appVm
,
i18n
);
if
(
!
appVm
.
$t
||
!
appVm
.
$i18n
||
ignoreVueI18n
)
{
// if (!locale) {
// i18n.setLocale(getDefaultLocale())
// }
/* eslint-disable no-func-assign */
t
=
function
(
key
,
values
)
{
// 触发响应式
appVm
.
$locale
;
return
i18n
.
t
(
key
,
values
);
};
}
else
{
initLocaleWatcher
(
appVm
,
i18n
);
/* eslint-disable no-func-assign */
t
=
function
(
key
,
values
)
{
const
$i18n
=
appVm
.
$i18n
;
...
...
@@ -309,10 +305,7 @@ function initVueI18n(locale = LOCALE_EN, messages = {}, fallbackLocale = LOCALE_
return
i18n
.
getLocale
();
},
setLocale
(
newLocale
)
{
// 更新所有实例 locale
i18nInstances
.
forEach
((
ins
)
=>
{
ins
.
setLocale
(
newLocale
);
});
return
i18n
.
setLocale
(
newLocale
);
},
};
}
...
...
packages/uni-i18n/src/I18n.ts
浏览文件 @
2ec9088b
...
...
@@ -14,9 +14,7 @@ export type BuiltInLocale =
|
typeof
LOCALE_FR
|
typeof
LOCALE_ES
export
type
LocaleMessages
=
{
[
name
in
BuiltInLocale
]?:
Record
<
string
,
string
>
}
export
type
LocaleMessages
=
Record
<
string
,
Record
<
string
,
string
>>
export
interface
Formatter
{
interpolate
:
(
...
...
@@ -26,14 +24,11 @@ export interface Formatter {
)
=>
Array
<
unknown
>
}
export
type
LocaleWatcher
=
(
newLocale
:
BuiltInLocale
,
oldLocale
:
BuiltInLocale
)
=>
void
export
type
LocaleWatcher
=
(
newLocale
:
string
,
oldLocale
:
string
)
=>
void
export
interface
I18nOptions
{
locale
:
BuiltInLocale
fallbackLocale
?:
BuiltInLocale
locale
:
string
fallbackLocale
?:
string
messages
?:
LocaleMessages
formater
?:
Formatter
watcher
?:
LocaleWatcher
...
...
@@ -83,8 +78,8 @@ function normalizeLocale(
}
export
class
I18n
{
private
locale
:
BuiltInLocale
=
LOCALE_EN
private
fallbackLocale
:
BuiltInLocale
=
LOCALE_EN
private
locale
:
string
=
LOCALE_EN
private
fallbackLocale
:
string
=
LOCALE_EN
private
message
:
Record
<
string
,
string
>
=
{}
private
messages
:
LocaleMessages
=
{}
private
watchers
:
LocaleWatcher
[]
=
[]
...
...
packages/uni-i18n/src/vue-i18n.ts
浏览文件 @
2ec9088b
...
...
@@ -13,19 +13,13 @@ type Interpolate = (
values
?:
Record
<
string
,
unknown
>
|
Array
<
unknown
>
)
=>
string
function
initLocaleWatcher
(
appVm
:
any
,
i18n
:
I18n
)
{
if
(
appVm
.
$i18n
)
{
const
vm
=
appVm
.
$i18n
.
vm
?
appVm
.
$i18n
.
vm
:
appVm
vm
.
$watch
(
appVm
.
$i18n
.
vm
?
'
locale
'
:
()
=>
appVm
.
$i18n
.
locale
,
(
newLocale
:
BuiltInLocale
)
=>
{
i18n
.
setLocale
(
newLocale
)
},
{
immediate
:
true
,
}
)
}
function
watchAppLocale
(
appVm
:
any
,
i18n
:
I18n
)
{
appVm
.
$watch
(
()
=>
appVm
.
$locale
,
(
newLocale
:
string
)
=>
{
i18n
.
setLocale
(
newLocale
)
}
)
}
// function getDefaultLocale() {
...
...
@@ -42,20 +36,32 @@ function initLocaleWatcher(appVm: any, i18n: I18n) {
const
i18nInstances
:
I18n
[]
=
[]
export
function
initVueI18n
(
locale
:
BuiltInLocale
=
LOCALE_EN
,
locale
?:
string
,
messages
:
LocaleMessages
=
{},
fallbackLocale
:
BuiltInLocale
=
LOCALE_EN
,
watcher
?:
(
locale
:
BuiltInLocale
)
=>
void
fallbackLocale
?:
BuiltInLocale
,
watcher
?:
(
locale
:
string
)
=>
void
)
{
// 兼容旧版本入参
if
(
typeof
locale
!==
'
string
'
)
{
;[
locale
,
messages
]
=
[
messages
as
BuiltInLocale
,
locale
as
LocaleMessages
]
;[
locale
,
messages
]
=
[
messages
as
unknown
as
string
,
locale
as
unknown
as
LocaleMessages
,
]
}
if
(
typeof
locale
!==
'
string
'
)
{
locale
=
fallbackLocale
locale
=
(
typeof
uni
!==
'
undefined
'
&&
uni
.
getLocale
&&
uni
.
getLocale
())
||
LOCALE_EN
}
if
(
typeof
fallbackLocale
!==
'
string
'
)
{
fallbackLocale
=
// @ts-expect-error
(
typeof
__uniConfig
!==
'
undefined
'
&&
__uniConfig
.
fallbackLocale
)
||
LOCALE_EN
}
const
i18n
=
new
I18n
({
locale
:
locale
||
fallbackLocale
,
locale
,
fallbackLocale
,
messages
,
watcher
,
...
...
@@ -72,16 +78,18 @@ export function initVueI18n(
}
}
else
{
const
appVm
=
getApp
().
$vm
watchAppLocale
(
appVm
,
i18n
)
if
(
!
appVm
.
$t
||
!
appVm
.
$i18n
||
ignoreVueI18n
)
{
// if (!locale) {
// i18n.setLocale(getDefaultLocale())
// }
/* eslint-disable no-func-assign */
t
=
function
(
key
,
values
)
{
// 触发响应式
appVm
.
$locale
return
i18n
.
t
(
key
,
values
)
}
}
else
{
initLocaleWatcher
(
appVm
,
i18n
)
/* eslint-disable no-func-assign */
t
=
function
(
key
,
values
)
{
const
$i18n
=
appVm
.
$i18n
...
...
@@ -123,11 +131,8 @@ export function initVueI18n(
getLocale
()
{
return
i18n
.
getLocale
()
},
setLocale
(
newLocale
:
BuiltInLocale
)
{
// 更新所有实例 locale
i18nInstances
.
forEach
((
ins
)
=>
{
ins
.
setLocale
(
newLocale
)
})
setLocale
(
newLocale
:
string
)
{
return
i18n
.
setLocale
(
newLocale
)
},
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录