getI18next

This commit is contained in:
Wang Kejun 2022-07-18 18:32:33 +08:00
parent 9e6f5c9201
commit 540f780bae
9 changed files with 180 additions and 178 deletions

View File

@ -9,5 +9,5 @@ import { ExceptionRouters } from './types/ExceptionRoute';
export declare function initialize<ED extends EntityDict, Cxt extends Context<ED>, AD extends Record<string, Aspect<ED, Cxt>>, FD extends Record<string, Feature<ED, Cxt, AD & CommonAspectDict<ED, Cxt>>>>(storageSchema: StorageSchema<ED>, createFeatures: (aspectWrapper: AspectWrapper<ED, Cxt, AD>, basicFeatures: BasicFeatures<ED, Cxt, AD & CommonAspectDict<ED, Cxt>>, context: Cxt) => FD, contextBuilder: (cxtString?: string) => (store: RowStore<ED, Cxt>) => Cxt, aspectDict: AD, exceptionRouters?: ExceptionRouters, triggers?: Array<Trigger<ED, keyof ED, Cxt>>, checkers?: Array<Checker<ED, keyof ED, Cxt>>, watchers?: Array<Watcher<ED, keyof ED, Cxt>>, initialData?: { export declare function initialize<ED extends EntityDict, Cxt extends Context<ED>, AD extends Record<string, Aspect<ED, Cxt>>, FD extends Record<string, Feature<ED, Cxt, AD & CommonAspectDict<ED, Cxt>>>>(storageSchema: StorageSchema<ED>, createFeatures: (aspectWrapper: AspectWrapper<ED, Cxt, AD>, basicFeatures: BasicFeatures<ED, Cxt, AD & CommonAspectDict<ED, Cxt>>, context: Cxt) => FD, contextBuilder: (cxtString?: string) => (store: RowStore<ED, Cxt>) => Cxt, aspectDict: AD, exceptionRouters?: ExceptionRouters, triggers?: Array<Trigger<ED, keyof ED, Cxt>>, checkers?: Array<Checker<ED, keyof ED, Cxt>>, watchers?: Array<Watcher<ED, keyof ED, Cxt>>, initialData?: {
[T in keyof ED]?: Array<ED[T]['OpSchema']>; [T in keyof ED]?: Array<ED[T]['OpSchema']>;
}, actionDict?: ActionDictOfEntityDict<ED>, translations?: Record<string, any>): { }, actionDict?: ActionDictOfEntityDict<ED>, translations?: Record<string, any>): {
i18n: undefined; i18n: import("./platforms/wechatMp/i18n").I18nWechatMpRuntimeBase;
}; };

View File

@ -15,21 +15,9 @@ function initialize(storageSchema, createFeatures, contextBuilder, aspectDict, e
}); });
} }
// 初始化i8n配置 // 初始化i8n配置
let i18n; const i18n = (0, i18n_1.getI18next)({
if (translations) {
const systemInfo = wx.getSystemInfoSync();
const { language } = systemInfo; // 系统语言
let defaultLocale;
if (language === 'zh_CN') {
defaultLocale = language;
}
(0, i18n_1.initI18nWechatMp)({
locales: {
translations, translations,
},
defaultLocale,
}); });
}
(0, lodash_1.assign)(global, { (0, lodash_1.assign)(global, {
OakPage: (options) => (0, page_mp_1.createPage)(options, features, exceptionRouterDict, context), OakPage: (options) => (0, page_mp_1.createPage)(options, features, exceptionRouterDict, context),
OakComponent: (options) => (0, page_mp_1.createComponent)(options, features, exceptionRouterDict, context), OakComponent: (options) => (0, page_mp_1.createComponent)(options, features, exceptionRouterDict, context),

View File

@ -7,5 +7,5 @@ import { ActionDictOfEntityDict } from 'oak-domain/lib/types/Action';
import { CommonAspectDict } from 'oak-common-aspect'; import { CommonAspectDict } from 'oak-common-aspect';
import { ExceptionRouters } from './types/ExceptionRoute'; import { ExceptionRouters } from './types/ExceptionRoute';
export declare function initialize<ED extends EntityDict, Cxt extends Context<ED>, AD extends Record<string, Aspect<ED, Cxt>>, FD extends Record<string, Feature<ED, Cxt, AD & CommonAspectDict<ED, Cxt>>>>(storageSchema: StorageSchema<ED>, createFeatures: (aspectWrapper: AspectWrapper<ED, Cxt, AD>, basicFeatures: BasicFeatures<ED, Cxt, AD & CommonAspectDict<ED, Cxt>>, context: Cxt) => FD, contextBuilder: (cxtString?: string) => (store: RowStore<ED, Cxt>) => Cxt, exceptionRouters: ExceptionRouters | undefined, connector: Connector<ED, Cxt>, checkers?: Array<Checker<ED, keyof ED, Cxt>>, actionDict?: ActionDictOfEntityDict<ED>, translations?: Record<string, any>): { export declare function initialize<ED extends EntityDict, Cxt extends Context<ED>, AD extends Record<string, Aspect<ED, Cxt>>, FD extends Record<string, Feature<ED, Cxt, AD & CommonAspectDict<ED, Cxt>>>>(storageSchema: StorageSchema<ED>, createFeatures: (aspectWrapper: AspectWrapper<ED, Cxt, AD>, basicFeatures: BasicFeatures<ED, Cxt, AD & CommonAspectDict<ED, Cxt>>, context: Cxt) => FD, contextBuilder: (cxtString?: string) => (store: RowStore<ED, Cxt>) => Cxt, exceptionRouters: ExceptionRouters | undefined, connector: Connector<ED, Cxt>, checkers?: Array<Checker<ED, keyof ED, Cxt>>, actionDict?: ActionDictOfEntityDict<ED>, translations?: Record<string, any>): {
i18n: undefined; i18n: import("./platforms/wechatMp/i18n").I18nWechatMpRuntimeBase;
}; };

View File

@ -15,22 +15,9 @@ function initialize(storageSchema, createFeatures, contextBuilder, exceptionRout
}); });
} }
// 初始化locales // 初始化locales
let i18n; const i18n = (0, i18n_1.getI18next)({
if (translations) {
const systemInfo = wx.getSystemInfoSync();
const { language } = systemInfo; // 系统语言
let defaultLocale;
if (language === 'zh_CN') {
defaultLocale = language;
}
//初始化i18n
(0, i18n_1.initI18nWechatMp)({
locales: {
translations, translations,
},
defaultLocale,
}); });
}
(0, lodash_1.assign)(global, { (0, lodash_1.assign)(global, {
OakPage: (options) => (0, page_mp_1.createPage)(options, features, exceptionRouterDict, context), OakPage: (options) => (0, page_mp_1.createPage)(options, features, exceptionRouterDict, context),
OakComponent: (options) => (0, page_mp_1.createComponent)(options, features, exceptionRouterDict, context), OakComponent: (options) => (0, page_mp_1.createComponent)(options, features, exceptionRouterDict, context),

View File

@ -35,8 +35,8 @@ export declare function initI18nWechatMp(options: {
defaultLocale?: string; defaultLocale?: string;
fallbackLocale?: string; fallbackLocale?: string;
}): I18nWechatMpRuntimeBase; }): I18nWechatMpRuntimeBase;
export declare function getI18nInstanceWechatMp(): any; export declare function getI18nInstanceWechatMp(): I18nWechatMpRuntimeBase | null;
export declare const CURRENT_LOCALE_KEY = "$_locale"; export declare const CURRENT_LOCALE_KEY = "$_locale";
export declare const LOCALE_CHANGE_HANDLER_NAME = "$_localeChange"; export declare const LOCALE_CHANGE_HANDLER_NAME = "$_localeChange";
export declare const CURRENT_LOCALE_DATA = "$_translations"; export declare const CURRENT_LOCALE_DATA = "$_translations";
export declare const I18nWechatMp: string; export declare function getI18next(options?: Record<string, any>): I18nWechatMpRuntimeBase;

View File

@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod }; return (mod && mod.__esModule) ? mod : { "default": mod };
}; };
Object.defineProperty(exports, "__esModule", { value: true }); 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 interpreter_1 = require("./interpreter");
const common_1 = require("./common"); const common_1 = require("./common");
const translation_parser_1 = require("./compile/translation-parser"); const translation_parser_1 = require("./compile/translation-parser");
@ -88,54 +88,89 @@ function initI18nWechatMp(options) {
} }
exports.initI18nWechatMp = initI18nWechatMp; exports.initI18nWechatMp = initI18nWechatMp;
function getI18nInstanceWechatMp() { function getI18nInstanceWechatMp() {
//@ts-ignore return OakI18n?.i18nInstance;
return global.OakI18n?.i18nInstance;
} }
exports.getI18nInstanceWechatMp = getI18nInstanceWechatMp; exports.getI18nInstanceWechatMp = getI18nInstanceWechatMp;
exports.CURRENT_LOCALE_KEY = '$_locale'; exports.CURRENT_LOCALE_KEY = '$_locale';
exports.LOCALE_CHANGE_HANDLER_NAME = '$_localeChange'; exports.LOCALE_CHANGE_HANDLER_NAME = '$_localeChange';
exports.CURRENT_LOCALE_DATA = '$_translations'; exports.CURRENT_LOCALE_DATA = '$_translations';
exports.I18nWechatMp = Behavior((() => { function getI18next(options) {
const behaviorHooks = { const systemInfo = wx.getSystemInfoSync();
lifetimes: { const { language } = systemInfo; // 系统语言
created() { const { translations } = options || {};
this[exports.LOCALE_CHANGE_HANDLER_NAME] = (currentLocale) => { let defaultLocale = 'zh_CN';
this.setData({ if (language) {
[exports.CURRENT_LOCALE_KEY]: currentLocale, defaultLocale = language;
}
//初始化i18n
const i18n = initI18nWechatMp({
locales: {
translations,
},
defaultLocale,
}); });
}; return i18n;
}, }
attached() { exports.getI18next = getI18next;
if (!OakI18n.i18nInstance) { // type Func = (...args: any[]) => any;
throw new Error('[i18n] ensure run initI18nWechatMp() in app.js before using I18nWechatMp library'); // export const I18nWechatMp = Behavior(
} // (() => {
this.setData({ // const behaviorHooks: Record<
[exports.CURRENT_LOCALE_KEY]: OakI18n.i18nInstance.currentLocale, // string,
[exports.CURRENT_LOCALE_DATA]: OakI18n.i18nInstance.translations, // Record<string, Func> | CommonI18nInterface
}); // > = {
}, // lifetimes: {
detached() { }, // created() {
}, // (this as any)[LOCALE_CHANGE_HANDLER_NAME] = (
methods: { // currentLocale: string
t(key, params) { // ) => {
if (!OakI18n.i18nInstance) { // (this as any).setData({
throw new Error('[i18n] ensure run initI18nWechatMp() in app.js before using I18nWechatMp library'); // [CURRENT_LOCALE_KEY]: currentLocale,
} // });
return OakI18n.i18nInstance.getString(key, params); // };
}, // },
setLocale(locale) { // attached() {
if (!OakI18n.i18nInstance) { // if (!OakI18n.i18nInstance) {
throw new Error('[i18n] ensure run initI18nWechatMp() in app.js before using I18nWechatMp library'); // throw new Error(
} // '[i18n] ensure run initI18nWechatMp() in app.js before using I18nWechatMp library'
return OakI18n.i18nInstance.setLocale(locale); // );
}, // }
getLocale() { // (this as any).setData({
if (!OakI18n.i18nInstance) { // [CURRENT_LOCALE_KEY]:
throw new Error('[i18n] ensure run initI18nWechatMp() in app.js before using I18nWechatMp library'); // OakI18n.i18nInstance.currentLocale,
} // [CURRENT_LOCALE_DATA]:
return OakI18n.i18nInstance.getLocale(); // OakI18n.i18nInstance.translations,
}, // });
}, // },
}; // detached() {},
return behaviorHooks; // },
})()); // 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;
// })()
// );

View File

@ -22,7 +22,7 @@ import { ExceptionHandler, ExceptionRouters } from './types/ExceptionRoute';
import { OakComponentOption, OakPageOption } from './types/Page'; import { OakComponentOption, OakPageOption } from './types/Page';
import { createComponent, createPage } from './page.mp'; import { createComponent, createPage } from './page.mp';
import { initialize as initDev } from './initialize-dev'; import { initialize as initDev } from './initialize-dev';
import { initI18nWechatMp } from './platforms/wechatMp/i18n'; import { getI18next } from './platforms/wechatMp/i18n';
export function initialize< export function initialize<
ED extends EntityDict, ED extends EntityDict,
@ -67,21 +67,9 @@ export function initialize<
}); });
} }
// 初始化i8n配置 // 初始化i8n配置
let i18n; const i18n = getI18next({
if (translations) {
const systemInfo = wx.getSystemInfoSync();
const { language } = systemInfo; // 系统语言
let defaultLocale;
if (language === 'zh_CN') {
defaultLocale = language;
}
initI18nWechatMp({
locales: {
translations, translations,
},
defaultLocale,
}); });
}
assign(global, { assign(global, {
OakPage: < OakPage: <

View File

@ -21,7 +21,7 @@ import { ExceptionHandler, ExceptionRouters } from './types/ExceptionRoute';
import { OakComponentOption, OakPageOption } from './types/Page'; import { OakComponentOption, OakPageOption } from './types/Page';
import { createComponent, createPage } from './page.mp'; import { createComponent, createPage } from './page.mp';
import { initialize as initProd } from './initialize-prod'; import { initialize as initProd } from './initialize-prod';
import { initI18nWechatMp } from './platforms/wechatMp/i18n'; import { getI18next } from './platforms/wechatMp/i18n';
export function initialize< export function initialize<
ED extends EntityDict, ED extends EntityDict,
@ -58,22 +58,9 @@ export function initialize<
}); });
} }
// 初始化locales // 初始化locales
let i18n; const i18n = getI18next({
if (translations) {
const systemInfo = wx.getSystemInfoSync();
const { language } = systemInfo; // 系统语言
let defaultLocale;
if (language === 'zh_CN') {
defaultLocale = language;
}
//初始化i18n
initI18nWechatMp({
locales: {
translations, translations,
},
defaultLocale,
}); });
}
assign(global, { assign(global, {
OakPage: < OakPage: <

View File

@ -126,81 +126,98 @@ export function initI18nWechatMp(options: {
} }
export function getI18nInstanceWechatMp() { export function getI18nInstanceWechatMp() {
//@ts-ignore return OakI18n?.i18nInstance;
return global.OakI18n?.i18nInstance;
} }
export const CURRENT_LOCALE_KEY = '$_locale'; export const CURRENT_LOCALE_KEY = '$_locale';
export const LOCALE_CHANGE_HANDLER_NAME = '$_localeChange'; export const LOCALE_CHANGE_HANDLER_NAME = '$_localeChange';
export const CURRENT_LOCALE_DATA = '$_translations'; export const CURRENT_LOCALE_DATA = '$_translations';
type Func = (...args: any[]) => any; export function getI18next(options?: Record<string, any>) {
const systemInfo = wx.getSystemInfoSync();
export const I18nWechatMp = Behavior( const { language } = systemInfo; // 系统语言
(() => { const { translations } = options || {};
const behaviorHooks: Record< let defaultLocale = 'zh_CN';
string, if (language) {
Record<string, Func> | CommonI18nInterface defaultLocale = language;
> = { }
lifetimes: { //初始化i18n
created() { const i18n = initI18nWechatMp({
(this as any)[LOCALE_CHANGE_HANDLER_NAME] = ( locales: {
currentLocale: string translations,
) => { },
(this as any).setData({ defaultLocale,
[CURRENT_LOCALE_KEY]: currentLocale,
}); });
}; return i18n;
}, }
attached() { // type Func = (...args: any[]) => any;
if (!OakI18n.i18nInstance) {
throw new Error(
'[i18n] ensure run initI18nWechatMp() in app.js before using I18nWechatMp library'
);
}
(this as any).setData({ // export const I18nWechatMp = Behavior(
[CURRENT_LOCALE_KEY]: // (() => {
OakI18n.i18nInstance.currentLocale, // const behaviorHooks: Record<
[CURRENT_LOCALE_DATA]: // string,
OakI18n.i18nInstance.translations, // Record<string, Func> | CommonI18nInterface
}); // > = {
}, // lifetimes: {
// created() {
// (this as any)[LOCALE_CHANGE_HANDLER_NAME] = (
// currentLocale: string
// ) => {
// (this as any).setData({
// [CURRENT_LOCALE_KEY]: currentLocale,
// });
// };
// },
detached() {}, // attached() {
}, // if (!OakI18n.i18nInstance) {
// throw new Error(
// '[i18n] ensure run initI18nWechatMp() in app.js before using I18nWechatMp library'
// );
// }
methods: { // (this as any).setData({
t(key: string, params: object) { // [CURRENT_LOCALE_KEY]:
if (!OakI18n.i18nInstance) { // OakI18n.i18nInstance.currentLocale,
throw new Error( // [CURRENT_LOCALE_DATA]:
'[i18n] ensure run initI18nWechatMp() in app.js before using I18nWechatMp library' // OakI18n.i18nInstance.translations,
); // });
} // },
return OakI18n.i18nInstance.getString(key, params);
},
setLocale(locale: string) { // detached() {},
if (!OakI18n.i18nInstance) { // },
throw new Error(
'[i18n] ensure run initI18nWechatMp() in app.js before using I18nWechatMp library'
);
}
return OakI18n.i18nInstance.setLocale(locale);
},
getLocale() { // methods: {
if (!OakI18n.i18nInstance) { // t(key: string, params: object) {
throw new Error( // if (!OakI18n.i18nInstance) {
'[i18n] ensure run initI18nWechatMp() in app.js before using I18nWechatMp library' // throw new Error(
); // '[i18n] ensure run initI18nWechatMp() in app.js before using I18nWechatMp library'
} // );
return OakI18n.i18nInstance.getLocale(); // }
}, // return OakI18n.i18nInstance.getString(key, params);
} as CommonI18nInterface, // },
};
return behaviorHooks; // 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;
// })()
// );