提交 2ec9088b 编写于 作者: fxy060608's avatar fxy060608

wip(i18n): sync locale

上级 513f4d1e
......@@ -12,6 +12,7 @@ declare module '@vue/runtime-core' {
getOpenerEventChannel: () => EventChannel
$page: Page.PageInstance['$page']
$mpType?: 'app' | 'page'
$locale?: string
__isTabBar: boolean
}
......
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)
}
}
)
......@@ -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';
......
因为 它太大了无法显示 source diff 。你可以改为 查看blob
......@@ -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'
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)
}
......@@ -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
}
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(
......
......@@ -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;
......
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 }) => {
......
......@@ -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);
},
};
}
......
......@@ -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;
......
......@@ -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);
},
};
}
......
......@@ -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[] = []
......
......@@ -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.
先完成此消息的编辑!
想要评论请 注册