diff --git a/lib/initialize.dev.mp.d.ts b/lib/initialize.dev.mp.d.ts index 24497885..9bb6ce13 100644 --- a/lib/initialize.dev.mp.d.ts +++ b/lib/initialize.dev.mp.d.ts @@ -9,5 +9,5 @@ import { ExceptionRouters } from './types/ExceptionRoute'; export declare function initialize, AD extends Record>, FD extends Record>>>(storageSchema: StorageSchema, createFeatures: (aspectWrapper: AspectWrapper, basicFeatures: BasicFeatures>, context: Cxt) => FD, contextBuilder: (cxtString?: string) => (store: RowStore) => Cxt, aspectDict: AD, exceptionRouters?: ExceptionRouters, triggers?: Array>, checkers?: Array>, watchers?: Array>, initialData?: { [T in keyof ED]?: Array; }, actionDict?: ActionDictOfEntityDict, translations?: Record): { - i18n: undefined; + i18n: import("./platforms/wechatMp/i18n").I18nWechatMpRuntimeBase; }; diff --git a/lib/initialize.dev.mp.js b/lib/initialize.dev.mp.js index 589901af..6ac26ebb 100644 --- a/lib/initialize.dev.mp.js +++ b/lib/initialize.dev.mp.js @@ -15,21 +15,9 @@ function initialize(storageSchema, createFeatures, contextBuilder, aspectDict, e }); } // 初始化i8n配置 - let i18n; - if (translations) { - const systemInfo = wx.getSystemInfoSync(); - const { language } = systemInfo; // 系统语言 - let defaultLocale; - if (language === 'zh_CN') { - defaultLocale = language; - } - (0, i18n_1.initI18nWechatMp)({ - locales: { - translations, - }, - defaultLocale, - }); - } + const i18n = (0, i18n_1.getI18next)({ + translations, + }); (0, lodash_1.assign)(global, { OakPage: (options) => (0, page_mp_1.createPage)(options, features, exceptionRouterDict, context), OakComponent: (options) => (0, page_mp_1.createComponent)(options, features, exceptionRouterDict, context), diff --git a/lib/initialize.prod.mp.d.ts b/lib/initialize.prod.mp.d.ts index 201a2f54..0d00e6b2 100644 --- a/lib/initialize.prod.mp.d.ts +++ b/lib/initialize.prod.mp.d.ts @@ -7,5 +7,5 @@ import { ActionDictOfEntityDict } from 'oak-domain/lib/types/Action'; import { CommonAspectDict } from 'oak-common-aspect'; import { ExceptionRouters } from './types/ExceptionRoute'; export declare function initialize, AD extends Record>, FD extends Record>>>(storageSchema: StorageSchema, createFeatures: (aspectWrapper: AspectWrapper, basicFeatures: BasicFeatures>, context: Cxt) => FD, contextBuilder: (cxtString?: string) => (store: RowStore) => Cxt, exceptionRouters: ExceptionRouters | undefined, connector: Connector, checkers?: Array>, actionDict?: ActionDictOfEntityDict, translations?: Record): { - i18n: undefined; + i18n: import("./platforms/wechatMp/i18n").I18nWechatMpRuntimeBase; }; diff --git a/lib/initialize.prod.mp.js b/lib/initialize.prod.mp.js index 9e70c2fa..9718e37d 100644 --- a/lib/initialize.prod.mp.js +++ b/lib/initialize.prod.mp.js @@ -15,22 +15,9 @@ function initialize(storageSchema, createFeatures, contextBuilder, exceptionRout }); } // 初始化locales - let i18n; - if (translations) { - const systemInfo = wx.getSystemInfoSync(); - const { language } = systemInfo; // 系统语言 - let defaultLocale; - if (language === 'zh_CN') { - defaultLocale = language; - } - //初始化i18n - (0, i18n_1.initI18nWechatMp)({ - locales: { - translations, - }, - defaultLocale, - }); - } + const i18n = (0, i18n_1.getI18next)({ + translations, + }); (0, lodash_1.assign)(global, { OakPage: (options) => (0, page_mp_1.createPage)(options, features, exceptionRouterDict, context), OakComponent: (options) => (0, page_mp_1.createComponent)(options, features, exceptionRouterDict, context), diff --git a/lib/platforms/wechatMp/i18n/index.d.ts b/lib/platforms/wechatMp/i18n/index.d.ts index 7c488300..34124db6 100644 --- a/lib/platforms/wechatMp/i18n/index.d.ts +++ b/lib/platforms/wechatMp/i18n/index.d.ts @@ -35,8 +35,8 @@ export declare function initI18nWechatMp(options: { defaultLocale?: string; fallbackLocale?: string; }): I18nWechatMpRuntimeBase; -export declare function getI18nInstanceWechatMp(): any; +export declare function getI18nInstanceWechatMp(): I18nWechatMpRuntimeBase | null; export declare const CURRENT_LOCALE_KEY = "$_locale"; export declare const LOCALE_CHANGE_HANDLER_NAME = "$_localeChange"; export declare const CURRENT_LOCALE_DATA = "$_translations"; -export declare const I18nWechatMp: string; +export declare function getI18next(options?: Record): I18nWechatMpRuntimeBase; diff --git a/lib/platforms/wechatMp/i18n/index.js b/lib/platforms/wechatMp/i18n/index.js index 71f491bc..f6c6bd36 100644 --- a/lib/platforms/wechatMp/i18n/index.js +++ b/lib/platforms/wechatMp/i18n/index.js @@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.I18nWechatMp = exports.CURRENT_LOCALE_DATA = exports.LOCALE_CHANGE_HANDLER_NAME = exports.CURRENT_LOCALE_KEY = exports.getI18nInstanceWechatMp = exports.initI18nWechatMp = exports.I18nWechatMpRuntimeBase = void 0; +exports.getI18next = exports.CURRENT_LOCALE_DATA = exports.LOCALE_CHANGE_HANDLER_NAME = exports.CURRENT_LOCALE_KEY = exports.getI18nInstanceWechatMp = exports.initI18nWechatMp = exports.I18nWechatMpRuntimeBase = void 0; const interpreter_1 = require("./interpreter"); const common_1 = require("./common"); const translation_parser_1 = require("./compile/translation-parser"); @@ -88,54 +88,89 @@ function initI18nWechatMp(options) { } exports.initI18nWechatMp = initI18nWechatMp; function getI18nInstanceWechatMp() { - //@ts-ignore - return global.OakI18n?.i18nInstance; + return OakI18n?.i18nInstance; } exports.getI18nInstanceWechatMp = getI18nInstanceWechatMp; exports.CURRENT_LOCALE_KEY = '$_locale'; exports.LOCALE_CHANGE_HANDLER_NAME = '$_localeChange'; exports.CURRENT_LOCALE_DATA = '$_translations'; -exports.I18nWechatMp = Behavior((() => { - const behaviorHooks = { - lifetimes: { - created() { - this[exports.LOCALE_CHANGE_HANDLER_NAME] = (currentLocale) => { - this.setData({ - [exports.CURRENT_LOCALE_KEY]: currentLocale, - }); - }; - }, - attached() { - if (!OakI18n.i18nInstance) { - throw new Error('[i18n] ensure run initI18nWechatMp() in app.js before using I18nWechatMp library'); - } - this.setData({ - [exports.CURRENT_LOCALE_KEY]: OakI18n.i18nInstance.currentLocale, - [exports.CURRENT_LOCALE_DATA]: OakI18n.i18nInstance.translations, - }); - }, - detached() { }, +function getI18next(options) { + const systemInfo = wx.getSystemInfoSync(); + const { language } = systemInfo; // 系统语言 + const { translations } = options || {}; + let defaultLocale = 'zh_CN'; + if (language) { + defaultLocale = language; + } + //初始化i18n + const i18n = initI18nWechatMp({ + locales: { + translations, }, - methods: { - t(key, params) { - if (!OakI18n.i18nInstance) { - throw new Error('[i18n] ensure run initI18nWechatMp() in app.js before using I18nWechatMp library'); - } - return OakI18n.i18nInstance.getString(key, params); - }, - setLocale(locale) { - if (!OakI18n.i18nInstance) { - throw new Error('[i18n] ensure run initI18nWechatMp() in app.js before using I18nWechatMp library'); - } - return OakI18n.i18nInstance.setLocale(locale); - }, - getLocale() { - if (!OakI18n.i18nInstance) { - throw new Error('[i18n] ensure run initI18nWechatMp() in app.js before using I18nWechatMp library'); - } - return OakI18n.i18nInstance.getLocale(); - }, - }, - }; - return behaviorHooks; -})()); + defaultLocale, + }); + return i18n; +} +exports.getI18next = getI18next; +// type Func = (...args: any[]) => any; +// export const I18nWechatMp = Behavior( +// (() => { +// const behaviorHooks: Record< +// string, +// Record | CommonI18nInterface +// > = { +// lifetimes: { +// created() { +// (this as any)[LOCALE_CHANGE_HANDLER_NAME] = ( +// currentLocale: string +// ) => { +// (this as any).setData({ +// [CURRENT_LOCALE_KEY]: currentLocale, +// }); +// }; +// }, +// attached() { +// if (!OakI18n.i18nInstance) { +// throw new Error( +// '[i18n] ensure run initI18nWechatMp() in app.js before using I18nWechatMp library' +// ); +// } +// (this as any).setData({ +// [CURRENT_LOCALE_KEY]: +// OakI18n.i18nInstance.currentLocale, +// [CURRENT_LOCALE_DATA]: +// OakI18n.i18nInstance.translations, +// }); +// }, +// detached() {}, +// }, +// methods: { +// t(key: string, params: object) { +// if (!OakI18n.i18nInstance) { +// throw new Error( +// '[i18n] ensure run initI18nWechatMp() in app.js before using I18nWechatMp library' +// ); +// } +// return OakI18n.i18nInstance.getString(key, params); +// }, +// setLocale(locale: string) { +// if (!OakI18n.i18nInstance) { +// throw new Error( +// '[i18n] ensure run initI18nWechatMp() in app.js before using I18nWechatMp library' +// ); +// } +// return OakI18n.i18nInstance.setLocale(locale); +// }, +// getLocale() { +// if (!OakI18n.i18nInstance) { +// throw new Error( +// '[i18n] ensure run initI18nWechatMp() in app.js before using I18nWechatMp library' +// ); +// } +// return OakI18n.i18nInstance.getLocale(); +// }, +// } as CommonI18nInterface, +// }; +// return behaviorHooks; +// })() +// ); diff --git a/src/initialize.dev.mp.ts b/src/initialize.dev.mp.ts index 55ee31b5..09697b9c 100644 --- a/src/initialize.dev.mp.ts +++ b/src/initialize.dev.mp.ts @@ -22,7 +22,7 @@ import { ExceptionHandler, ExceptionRouters } from './types/ExceptionRoute'; import { OakComponentOption, OakPageOption } from './types/Page'; import { createComponent, createPage } from './page.mp'; import { initialize as initDev } from './initialize-dev'; -import { initI18nWechatMp } from './platforms/wechatMp/i18n'; +import { getI18next } from './platforms/wechatMp/i18n'; export function initialize< ED extends EntityDict, @@ -67,21 +67,9 @@ export function initialize< }); } // 初始化i8n配置 - let i18n; - if (translations) { - const systemInfo = wx.getSystemInfoSync(); - const { language } = systemInfo; // 系统语言 - let defaultLocale; - if (language === 'zh_CN') { - defaultLocale = language; - } - initI18nWechatMp({ - locales: { - translations, - }, - defaultLocale, - }); - } + const i18n = getI18next({ + translations, + }); assign(global, { OakPage: < diff --git a/src/initialize.prod.mp.ts b/src/initialize.prod.mp.ts index a26eb434..8ca0fc19 100644 --- a/src/initialize.prod.mp.ts +++ b/src/initialize.prod.mp.ts @@ -21,7 +21,7 @@ import { ExceptionHandler, ExceptionRouters } from './types/ExceptionRoute'; import { OakComponentOption, OakPageOption } from './types/Page'; import { createComponent, createPage } from './page.mp'; import { initialize as initProd } from './initialize-prod'; -import { initI18nWechatMp } from './platforms/wechatMp/i18n'; +import { getI18next } from './platforms/wechatMp/i18n'; export function initialize< ED extends EntityDict, @@ -58,22 +58,9 @@ export function initialize< }); } // 初始化locales - let i18n; - if (translations) { - const systemInfo = wx.getSystemInfoSync(); - const { language } = systemInfo; // 系统语言 - let defaultLocale; - if (language === 'zh_CN') { - defaultLocale = language; - } - //初始化i18n - initI18nWechatMp({ - locales: { - translations, - }, - defaultLocale, - }); - } + const i18n = getI18next({ + translations, + }); assign(global, { OakPage: < diff --git a/src/platforms/wechatMp/i18n/index.ts b/src/platforms/wechatMp/i18n/index.ts index c782e19b..4c018707 100644 --- a/src/platforms/wechatMp/i18n/index.ts +++ b/src/platforms/wechatMp/i18n/index.ts @@ -126,81 +126,98 @@ export function initI18nWechatMp(options: { } export function getI18nInstanceWechatMp() { - //@ts-ignore - return global.OakI18n?.i18nInstance; + return OakI18n?.i18nInstance; } export const CURRENT_LOCALE_KEY = '$_locale'; export const LOCALE_CHANGE_HANDLER_NAME = '$_localeChange'; export const CURRENT_LOCALE_DATA = '$_translations'; -type Func = (...args: any[]) => any; +export function getI18next(options?: Record) { + const systemInfo = wx.getSystemInfoSync(); + const { language } = systemInfo; // 系统语言 + const { translations } = options || {}; + let defaultLocale = 'zh_CN'; + if (language) { + defaultLocale = language; + } + //初始化i18n + const i18n = initI18nWechatMp({ + locales: { + translations, + }, + defaultLocale, + }); + return i18n; +} -export const I18nWechatMp = Behavior( - (() => { - const behaviorHooks: Record< - string, - Record | CommonI18nInterface - > = { - lifetimes: { - created() { - (this as any)[LOCALE_CHANGE_HANDLER_NAME] = ( - currentLocale: string - ) => { - (this as any).setData({ - [CURRENT_LOCALE_KEY]: currentLocale, - }); - }; - }, +// type Func = (...args: any[]) => any; - attached() { - if (!OakI18n.i18nInstance) { - throw new Error( - '[i18n] ensure run initI18nWechatMp() in app.js before using I18nWechatMp library' - ); - } +// export const I18nWechatMp = Behavior( +// (() => { +// const behaviorHooks: Record< +// string, +// Record | CommonI18nInterface +// > = { +// lifetimes: { +// created() { +// (this as any)[LOCALE_CHANGE_HANDLER_NAME] = ( +// currentLocale: string +// ) => { +// (this as any).setData({ +// [CURRENT_LOCALE_KEY]: currentLocale, +// }); +// }; +// }, - (this as any).setData({ - [CURRENT_LOCALE_KEY]: - OakI18n.i18nInstance.currentLocale, - [CURRENT_LOCALE_DATA]: - OakI18n.i18nInstance.translations, - }); - }, +// attached() { +// if (!OakI18n.i18nInstance) { +// throw new Error( +// '[i18n] ensure run initI18nWechatMp() in app.js before using I18nWechatMp library' +// ); +// } - detached() {}, - }, +// (this as any).setData({ +// [CURRENT_LOCALE_KEY]: +// OakI18n.i18nInstance.currentLocale, +// [CURRENT_LOCALE_DATA]: +// OakI18n.i18nInstance.translations, +// }); +// }, - methods: { - t(key: string, params: object) { - if (!OakI18n.i18nInstance) { - throw new Error( - '[i18n] ensure run initI18nWechatMp() in app.js before using I18nWechatMp library' - ); - } - return OakI18n.i18nInstance.getString(key, params); - }, +// detached() {}, +// }, - setLocale(locale: string) { - if (!OakI18n.i18nInstance) { - throw new Error( - '[i18n] ensure run initI18nWechatMp() in app.js before using I18nWechatMp library' - ); - } - return OakI18n.i18nInstance.setLocale(locale); - }, +// methods: { +// t(key: string, params: object) { +// if (!OakI18n.i18nInstance) { +// throw new Error( +// '[i18n] ensure run initI18nWechatMp() in app.js before using I18nWechatMp library' +// ); +// } +// return OakI18n.i18nInstance.getString(key, params); +// }, - getLocale() { - if (!OakI18n.i18nInstance) { - throw new Error( - '[i18n] ensure run initI18nWechatMp() in app.js before using I18nWechatMp library' - ); - } - return OakI18n.i18nInstance.getLocale(); - }, - } as CommonI18nInterface, - }; +// setLocale(locale: string) { +// if (!OakI18n.i18nInstance) { +// throw new Error( +// '[i18n] ensure run initI18nWechatMp() in app.js before using I18nWechatMp library' +// ); +// } +// return OakI18n.i18nInstance.setLocale(locale); +// }, - return behaviorHooks; - })() -); +// getLocale() { +// if (!OakI18n.i18nInstance) { +// throw new Error( +// '[i18n] ensure run initI18nWechatMp() in app.js before using I18nWechatMp library' +// ); +// } +// return OakI18n.i18nInstance.getLocale(); +// }, +// } as CommonI18nInterface, +// }; + +// return behaviorHooks; +// })() +// );