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?: {
[T in keyof ED]?: Array<ED[T]['OpSchema']>;
}, 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配置
let i18n;
if (translations) {
const systemInfo = wx.getSystemInfoSync();
const { language } = systemInfo; // 系统语言
let defaultLocale;
if (language === 'zh_CN') {
defaultLocale = language;
}
(0, i18n_1.initI18nWechatMp)({
locales: {
const i18n = (0, i18n_1.getI18next)({
translations,
},
defaultLocale,
});
}
(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),

View File

@ -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<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
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: {
const i18n = (0, i18n_1.getI18next)({
translations,
},
defaultLocale,
});
}
(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),

View File

@ -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<string, any>): I18nWechatMpRuntimeBase;

View File

@ -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,
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,
},
defaultLocale,
});
};
},
attached() {
if (!OakI18n.i18nInstance) {
throw new Error('[i18n] ensure run initI18nWechatMp() in app.js before using I18nWechatMp library');
return i18n;
}
this.setData({
[exports.CURRENT_LOCALE_KEY]: OakI18n.i18nInstance.currentLocale,
[exports.CURRENT_LOCALE_DATA]: OakI18n.i18nInstance.translations,
});
},
detached() { },
},
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;
})());
exports.getI18next = getI18next;
// type Func = (...args: any[]) => any;
// export const I18nWechatMp = Behavior(
// (() => {
// const behaviorHooks: Record<
// string,
// Record<string, Func> | 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;
// })()
// );

View File

@ -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: {
const i18n = getI18next({
translations,
},
defaultLocale,
});
}
assign(global, {
OakPage: <

View File

@ -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: {
const i18n = getI18next({
translations,
},
defaultLocale,
});
}
assign(global, {
OakPage: <

View File

@ -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 const I18nWechatMp = Behavior(
(() => {
const behaviorHooks: Record<
string,
Record<string, Func> | CommonI18nInterface
> = {
lifetimes: {
created() {
(this as any)[LOCALE_CHANGE_HANDLER_NAME] = (
currentLocale: string
) => {
(this as any).setData({
[CURRENT_LOCALE_KEY]: currentLocale,
export function getI18next(options?: Record<string, any>) {
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,
});
};
},
attached() {
if (!OakI18n.i18nInstance) {
throw new Error(
'[i18n] ensure run initI18nWechatMp() in app.js before using I18nWechatMp library'
);
return i18n;
}
(this as any).setData({
[CURRENT_LOCALE_KEY]:
OakI18n.i18nInstance.currentLocale,
[CURRENT_LOCALE_DATA]:
OakI18n.i18nInstance.translations,
});
},
// type Func = (...args: any[]) => any;
detached() {},
},
// export const I18nWechatMp = Behavior(
// (() => {
// const behaviorHooks: Record<
// string,
// Record<string, Func> | CommonI18nInterface
// > = {
// lifetimes: {
// created() {
// (this as any)[LOCALE_CHANGE_HANDLER_NAME] = (
// currentLocale: string
// ) => {
// (this as any).setData({
// [CURRENT_LOCALE_KEY]: currentLocale,
// });
// };
// },
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);
},
// attached() {
// if (!OakI18n.i18nInstance) {
// throw new Error(
// '[i18n] ensure run initI18nWechatMp() in app.js before using I18nWechatMp library'
// );
// }
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);
},
// (this as any).setData({
// [CURRENT_LOCALE_KEY]:
// OakI18n.i18nInstance.currentLocale,
// [CURRENT_LOCALE_DATA]:
// OakI18n.i18nInstance.translations,
// });
// },
getLocale() {
if (!OakI18n.i18nInstance) {
throw new Error(
'[i18n] ensure run initI18nWechatMp() in app.js before using I18nWechatMp library'
);
}
return OakI18n.i18nInstance.getLocale();
},
} as CommonI18nInterface,
};
// 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;
// })()
// );