build
This commit is contained in:
parent
3a2c4bc852
commit
9e6f5c9201
|
|
@ -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>, version?: string): {
|
||||
i18n: import("i18next").i18n | undefined;
|
||||
i18n: import("i18next").i18n;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -15,12 +15,9 @@ function initialize(storageSchema, createFeatures, contextBuilder, aspectDict, e
|
|||
});
|
||||
}
|
||||
// 初始化i8n配置
|
||||
let i18n;
|
||||
if (translations) {
|
||||
i18n = (0, i18n_1.getI18next)({
|
||||
version,
|
||||
});
|
||||
}
|
||||
const i18n = (0, i18n_1.getI18next)({
|
||||
version,
|
||||
});
|
||||
(0, lodash_1.assign)(global, {
|
||||
OakPage: (options) => (0, page_web_1.createPage)(options, features, exceptionRouterDict, context),
|
||||
OakComponent: (options) => (0, page_web_1.createComponent)(options, features, exceptionRouterDict, context),
|
||||
|
|
|
|||
|
|
@ -6,4 +6,6 @@ import { BasicFeatures } from './features';
|
|||
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>): void;
|
||||
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;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ function initialize(storageSchema, createFeatures, contextBuilder, exceptionRout
|
|||
});
|
||||
}
|
||||
// 初始化locales
|
||||
let i18n;
|
||||
if (translations) {
|
||||
const systemInfo = wx.getSystemInfoSync();
|
||||
const { language } = systemInfo; // 系统语言
|
||||
|
|
@ -34,5 +35,8 @@ function initialize(storageSchema, createFeatures, contextBuilder, exceptionRout
|
|||
OakPage: (options) => (0, page_mp_1.createPage)(options, features, exceptionRouterDict, context),
|
||||
OakComponent: (options) => (0, page_mp_1.createComponent)(options, features, exceptionRouterDict, context),
|
||||
});
|
||||
return {
|
||||
i18n,
|
||||
};
|
||||
}
|
||||
exports.initialize = initialize;
|
||||
|
|
|
|||
|
|
@ -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>, version?: string): {
|
||||
i18n: import("i18next").i18n | undefined;
|
||||
i18n: import("i18next").i18n;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -15,12 +15,9 @@ function initialize(storageSchema, createFeatures, contextBuilder, exceptionRout
|
|||
});
|
||||
}
|
||||
// 初始化i8n配置
|
||||
let i18n;
|
||||
if (translations) {
|
||||
i18n = (0, i18n_1.getI18next)({
|
||||
version,
|
||||
});
|
||||
}
|
||||
const i18n = (0, i18n_1.getI18next)({
|
||||
version,
|
||||
});
|
||||
(0, lodash_1.assign)(global, {
|
||||
OakPage: (options) => (0, page_web_1.createPage)(options, features, exceptionRouterDict, context),
|
||||
OakComponent: (options) => (0, page_web_1.createComponent)(options, features, exceptionRouterDict, context),
|
||||
|
|
|
|||
|
|
@ -14,8 +14,8 @@ const i18next_chained_backend_1 = __importDefault(require("i18next-chained-backe
|
|||
const i18next_localstorage_backend_1 = __importDefault(require("i18next-localstorage-backend")); // primary use cache
|
||||
const i18next_browser_languagedetector_1 = __importDefault(require("i18next-browser-languagedetector"));
|
||||
const react_i18next_1 = require("react-i18next");
|
||||
const i18next_keys_ondemand_1 = require("i18next-keys-ondemand");
|
||||
const lodash_1 = require("lodash");
|
||||
const keys_ondemand_1 = __importDefault(require("./keys-ondemand"));
|
||||
/**
|
||||
* I18N语言包设计思路:
|
||||
* 1)在本地缓存中查找
|
||||
|
|
@ -33,7 +33,7 @@ const LOCAL_STORE_PREFIX = 'i18next_res_';
|
|||
* @param namespace
|
||||
* @returns {Promise}
|
||||
*/
|
||||
async function translationGetter(keys, language, namespace) {
|
||||
async function translationService(keys, language, namespace) {
|
||||
const url = `${process.env.PUBLIC_URL}/locales/${language}/${namespace}.json`;
|
||||
const response = await fetch(url);
|
||||
const json = await response.json();
|
||||
|
|
@ -104,7 +104,9 @@ function getI18nextInitOptions(options) {
|
|||
function getI18next(options) {
|
||||
const i18nextInitOptions = getI18nextInitOptions(options);
|
||||
i18next_1.default
|
||||
.use(new i18next_keys_ondemand_1.I18nextKeysOnDemand({ translationGetter }))
|
||||
.use(new keys_ondemand_1.default({
|
||||
translationGetter: translationService,
|
||||
}))
|
||||
.use(i18next_chained_backend_1.default)
|
||||
.use(i18next_browser_languagedetector_1.default)
|
||||
.use(react_i18next_1.initReactI18next) // if not using I18nextProvider
|
||||
|
|
|
|||
|
|
@ -0,0 +1,43 @@
|
|||
import { InitOptions } from 'i18next';
|
||||
export interface i18nInitOptions extends InitOptions {
|
||||
on(event: 'missingKey', callback: (lngs: string[], namespace: string, key: string, res: string) => void): void;
|
||||
/**
|
||||
* Adds multiple key/values.
|
||||
*/
|
||||
addResources(lng: string, ns: string, resources: any): void;
|
||||
options: InitOptions;
|
||||
}
|
||||
export interface TranslationMap {
|
||||
[key: string]: string;
|
||||
}
|
||||
export declare type TranslationGetter = (keys: string[], language: string, namespace: string) => Promise<TranslationMap>;
|
||||
export interface Options {
|
||||
/**
|
||||
* The resource key translator
|
||||
*
|
||||
* @type {TranslationGetter}
|
||||
* @memberof Options
|
||||
*/
|
||||
translationGetter: TranslationGetter;
|
||||
/**
|
||||
* Value to return for missing keys (default: empty string)
|
||||
*
|
||||
* @type {string}
|
||||
* @memberof Options
|
||||
*/
|
||||
missingKeyValue?: string;
|
||||
/**
|
||||
* Delay in ms used to debounce the translation requests (default: 100ms)
|
||||
*
|
||||
* @type {number}
|
||||
* @memberof Options
|
||||
*/
|
||||
debounceDelay?: number;
|
||||
}
|
||||
declare class I18nextKeysOnDemand {
|
||||
type: string;
|
||||
options: Options;
|
||||
constructor(options: Options);
|
||||
init(instance: i18nInitOptions): void;
|
||||
}
|
||||
export default I18nextKeysOnDemand;
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const debounce_1 = __importDefault(require("debounce"));
|
||||
class I18nextKeysOnDemand {
|
||||
type;
|
||||
options;
|
||||
constructor(options) {
|
||||
this.type = '3rdParty';
|
||||
this.options = { debounceDelay: 100, missingKeyValue: '', ...options };
|
||||
}
|
||||
init(instance) {
|
||||
const missingKeysQueue = {};
|
||||
const options = this.options;
|
||||
function requestResources(lng, ns) {
|
||||
const path = `${lng}.${ns}`;
|
||||
options
|
||||
.translationGetter(Object.keys(missingKeysQueue[path]), lng, ns)
|
||||
.then((result) => {
|
||||
missingKeysQueue[path] = {};
|
||||
instance.addResources(lng, ns, result);
|
||||
});
|
||||
}
|
||||
const debouncedRequestResources = {};
|
||||
function requestKey(key, lng, ns) {
|
||||
const path = `${lng}.${ns}`;
|
||||
missingKeysQueue[path] = missingKeysQueue[path] || {};
|
||||
missingKeysQueue[path][key] = true;
|
||||
debouncedRequestResources[path] =
|
||||
debouncedRequestResources[path] ||
|
||||
(0, debounce_1.default)(() => requestResources(lng, ns), options.debounceDelay);
|
||||
debouncedRequestResources[path]();
|
||||
}
|
||||
instance.on('missingKey', (lngs, ns, key, res) => {
|
||||
instance.options.parseMissingKeyHandler = () => {
|
||||
return options.missingKeyValue;
|
||||
};
|
||||
const languages = typeof lngs === 'string' ? [lngs] : lngs;
|
||||
languages.map((l) => requestKey(key, l, ns));
|
||||
});
|
||||
}
|
||||
}
|
||||
exports.default = I18nextKeysOnDemand;
|
||||
|
|
@ -69,12 +69,9 @@ export function initialize<
|
|||
}
|
||||
|
||||
// 初始化i8n配置
|
||||
let i18n;
|
||||
if (translations) {
|
||||
i18n = getI18next({
|
||||
version,
|
||||
});
|
||||
}
|
||||
const i18n = getI18next({
|
||||
version,
|
||||
});
|
||||
|
||||
assign(global, {
|
||||
OakPage: <
|
||||
|
|
|
|||
|
|
@ -60,12 +60,9 @@ export function initialize<
|
|||
}
|
||||
|
||||
// 初始化i8n配置
|
||||
let i18n;
|
||||
if (translations) {
|
||||
i18n = getI18next({
|
||||
version,
|
||||
});
|
||||
}
|
||||
const i18n = getI18next({
|
||||
version,
|
||||
});
|
||||
|
||||
assign(global, {
|
||||
OakPage: <
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import * as debounce from 'debounce';
|
||||
import debounce from 'debounce';
|
||||
import { InitOptions } from 'i18next';
|
||||
|
||||
export interface i18nInitOptions extends InitOptions {
|
||||
|
|
|
|||
Loading…
Reference in New Issue