locales相关的一些改动
This commit is contained in:
parent
9fa3c6dc2b
commit
71f15163f9
|
|
@ -393,6 +393,12 @@ function createDebugStore(storageSchema, contextBuilder, triggers, checkers, wat
|
|||
console.log('使用初始化数据建立debugStore', initialData);
|
||||
}
|
||||
else {
|
||||
// 对static的对象,使用initialData,剩下的使用物化数据
|
||||
for (var entity in initialData) {
|
||||
if (storageSchema[entity].static) {
|
||||
data.data[entity] = initialData[entity];
|
||||
}
|
||||
}
|
||||
initDataInStore(store, data.data, data.stat);
|
||||
console.log('使用物化数据建立debugStore', data);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ function initialize(aspectWrapper, storageSchema, contextBuilder, store, actionC
|
|||
var navigator = new navigator_1.Navigator();
|
||||
var port = new port_1.Port(aspectWrapper);
|
||||
var style = new style_1.Style(colorDict);
|
||||
var locales = new locales_1.Locales(cache, localStorage, environment, 'zh_CN', makeBridgeUrlFn); // 临时性代码,应由上层传入
|
||||
var locales = new locales_1.Locales(cache, localStorage, environment, 'zh-CN', makeBridgeUrlFn); // 临时性代码,应由上层传入
|
||||
var contextMenuFactory = new contextMenuFactory_1.ContextMenuFactory(cache, relationAuth, actionCascadePathGraph);
|
||||
return {
|
||||
cache: cache,
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import { Environment } from './environment';
|
|||
import { SyncContext } from 'oak-domain/lib/store/SyncRowStore';
|
||||
import { Scope, TranslateOptions } from 'i18n-js';
|
||||
export declare class Locales<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>, FrontCxt extends SyncContext<ED>, AD extends CommonAspectDict<ED, Cxt>> extends Feature {
|
||||
static MINIMAL_LOADING_GAP: number;
|
||||
private cache;
|
||||
private localStorage;
|
||||
private environment;
|
||||
|
|
@ -16,6 +17,7 @@ export declare class Locales<ED extends EntityDict & BaseEntityDict, Cxt extends
|
|||
private language;
|
||||
private defaultLng;
|
||||
private i18n;
|
||||
private loadingRecord;
|
||||
constructor(cache: Cache<ED, Cxt, FrontCxt, AD>, localStorage: LocalStorage, environment: Environment, defaultLng: string, makeBridgeUrlFn?: (url: string, headers?: Record<string, string>) => string);
|
||||
private detectLanguange;
|
||||
private resetDataset;
|
||||
|
|
|
|||
|
|
@ -5,11 +5,13 @@ var tslib_1 = require("tslib");
|
|||
var Feature_1 = require("../types/Feature");
|
||||
var assert_1 = tslib_1.__importDefault(require("assert"));
|
||||
var i18n_js_1 = require("i18n-js");
|
||||
var lodash_1 = require("oak-domain/lib/utils/lodash");
|
||||
var LS_LNG_KEY = 'ofb-feature-locale-lng';
|
||||
var Locales = /** @class */ (function (_super) {
|
||||
tslib_1.__extends(Locales, _super);
|
||||
function Locales(cache, localStorage, environment, defaultLng, makeBridgeUrlFn) {
|
||||
var _this = _super.call(this) || this;
|
||||
_this.loadingRecord = {};
|
||||
_this.cache = cache;
|
||||
_this.localStorage = localStorage;
|
||||
_this.defaultLng = defaultLng;
|
||||
|
|
@ -81,12 +83,12 @@ var Locales = /** @class */ (function (_super) {
|
|||
*/
|
||||
Locales.prototype.loadData = function (key) {
|
||||
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
||||
var _a, ns, key2, currentI18ns, filters, newI18ns, dataset_1;
|
||||
var _a, ns, currentI18ns, filters, now, newI18ns, dataset_1;
|
||||
return tslib_1.__generator(this, function (_b) {
|
||||
switch (_b.label) {
|
||||
case 0:
|
||||
(0, assert_1.default)(typeof key === 'string');
|
||||
_a = tslib_1.__read(key.split(':'), 2), ns = _a[0], key2 = _a[1];
|
||||
_a = tslib_1.__read(key.split('.'), 1), ns = _a[0];
|
||||
currentI18ns = this.cache.get('i18n', {
|
||||
data: {
|
||||
id: 1,
|
||||
|
|
@ -99,20 +101,25 @@ var Locales = /** @class */ (function (_super) {
|
|||
},
|
||||
}
|
||||
});
|
||||
filters = [this.language, this.defaultLng].map(function (ele) {
|
||||
var current = currentI18ns.find(function (ele2) { return ele2.namespace === ele; });
|
||||
filters = (0, lodash_1.uniq)([this.language, this.defaultLng]).map(function (ele) {
|
||||
var current = currentI18ns.find(function (ele2) { return ele2.language === ele; });
|
||||
if (current) {
|
||||
return {
|
||||
namespace: ele,
|
||||
language: ele,
|
||||
$$updateAt$$: {
|
||||
$gt: current.$$updateAt$$,
|
||||
},
|
||||
};
|
||||
}
|
||||
return {
|
||||
namespace: ele,
|
||||
language: ele,
|
||||
};
|
||||
});
|
||||
now = Date.now();
|
||||
if (this.loadingRecord[ns] && now - this.loadingRecord[ns] < Locales.MINIMAL_LOADING_GAP) {
|
||||
return [2 /*return*/];
|
||||
}
|
||||
this.loadingRecord[ns] = now;
|
||||
return [4 /*yield*/, this.cache.refresh('i18n', {
|
||||
data: {
|
||||
id: 1,
|
||||
|
|
@ -123,6 +130,7 @@ var Locales = /** @class */ (function (_super) {
|
|||
$$updateAt$$: 1,
|
||||
},
|
||||
filter: {
|
||||
namespace: ns,
|
||||
$or: filters,
|
||||
}
|
||||
}, undefined, undefined, undefined, true)];
|
||||
|
|
@ -154,8 +162,8 @@ var Locales = /** @class */ (function (_super) {
|
|||
});
|
||||
};
|
||||
Locales.prototype.t = function (key, params) {
|
||||
return key;
|
||||
// return this.i18n.t(key, params);
|
||||
// return key as string;
|
||||
return this.i18n.t(key, params);
|
||||
};
|
||||
// 需要暴露给小程序
|
||||
Locales.prototype.getDataSet = function () {
|
||||
|
|
@ -180,6 +188,7 @@ var Locales = /** @class */ (function (_super) {
|
|||
console.warn('development模式下无法使用bridge,直接使用原始url', url);
|
||||
return url;
|
||||
};
|
||||
Locales.MINIMAL_LOADING_GAP = 600 * 10000; // 最小加载间歇
|
||||
return Locales;
|
||||
}(Feature_1.Feature));
|
||||
exports.Locales = Locales;
|
||||
|
|
|
|||
|
|
@ -655,6 +655,7 @@ function createComponent(option, features) {
|
|||
switch (_a.label) {
|
||||
case 0:
|
||||
this.registerPageScroll();
|
||||
this.subscribed.push(features.locales.subscribe(function () { return _this.reRender(); }));
|
||||
if (option.entity) {
|
||||
this.subscribed.push(features.cache.subscribe(function () { return _this.reRender(); }));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -276,6 +276,12 @@ export function createDebugStore<ED extends EntityDict & BaseEntityDict, Cxt ext
|
|||
console.log('使用初始化数据建立debugStore', initialData);
|
||||
}
|
||||
else {
|
||||
// 对static的对象,使用initialData,剩下的使用物化数据
|
||||
for (const entity in initialData) {
|
||||
if (storageSchema[entity].static) {
|
||||
data.data[entity] = initialData[entity];
|
||||
}
|
||||
}
|
||||
initDataInStore(store, data.data, data.stat);
|
||||
console.log('使用物化数据建立debugStore', data);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ export function initialize<ED extends EntityDict & BaseEntityDict, Cxt extends A
|
|||
const navigator = new Navigator();
|
||||
const port = new Port<ED, Cxt, AD & CommonAspectDict<ED, Cxt>>(aspectWrapper);
|
||||
const style = new Style<ED>(colorDict);
|
||||
const locales = new Locales(cache, localStorage, environment, 'zh_CN', makeBridgeUrlFn); // 临时性代码,应由上层传入
|
||||
const locales = new Locales(cache, localStorage, environment, 'zh-CN', makeBridgeUrlFn); // 临时性代码,应由上层传入
|
||||
const contextMenuFactory = new ContextMenuFactory<ED, Cxt, FrontCxt, AD & CommonAspectDict<ED, Cxt>>(cache, relationAuth, actionCascadePathGraph);
|
||||
return {
|
||||
cache,
|
||||
|
|
|
|||
|
|
@ -9,10 +9,12 @@ import { Environment } from './environment';
|
|||
import { SyncContext } from 'oak-domain/lib/store/SyncRowStore';
|
||||
import assert from 'assert';
|
||||
import { I18n, Scope, TranslateOptions } from 'i18n-js';
|
||||
import { uniq } from 'oak-domain/lib/utils/lodash';
|
||||
|
||||
const LS_LNG_KEY = 'ofb-feature-locale-lng';
|
||||
|
||||
export class Locales<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>, FrontCxt extends SyncContext<ED>, AD extends CommonAspectDict<ED, Cxt>> extends Feature {
|
||||
static MINIMAL_LOADING_GAP = 600 * 10000; // 最小加载间歇
|
||||
private cache: Cache<ED, Cxt, FrontCxt, AD>;
|
||||
private localStorage: LocalStorage;
|
||||
private environment: Environment;
|
||||
|
|
@ -20,6 +22,7 @@ export class Locales<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncCo
|
|||
private language: string;
|
||||
private defaultLng: string;
|
||||
private i18n: I18n;
|
||||
private loadingRecord: Record<string, number> = {};
|
||||
|
||||
constructor(
|
||||
cache: Cache<ED, Cxt, FrontCxt, AD>,
|
||||
|
|
@ -95,7 +98,7 @@ export class Locales<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncCo
|
|||
*/
|
||||
private async loadData(key: Scope) {
|
||||
assert(typeof key === 'string');
|
||||
const [ ns, key2 ] = key.split(':');
|
||||
const [ ns ] = key.split('.');
|
||||
const currentI18ns = this.cache.get('i18n', {
|
||||
data: {
|
||||
id: 1,
|
||||
|
|
@ -109,22 +112,28 @@ export class Locales<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncCo
|
|||
}
|
||||
});
|
||||
|
||||
const filters: NonNullable<EntityDict['i18n']['Selection']['filter']>[] = [this.language, this.defaultLng].map(
|
||||
const filters: NonNullable<EntityDict['i18n']['Selection']['filter']>[] = uniq([this.language, this.defaultLng]).map(
|
||||
ele => {
|
||||
const current = currentI18ns.find(ele2 => ele2.namespace === ele);
|
||||
const current = currentI18ns.find(ele2 => ele2.language === ele);
|
||||
if (current) {
|
||||
return {
|
||||
namespace: ele,
|
||||
language: ele,
|
||||
$$updateAt$$: {
|
||||
$gt: current.$$updateAt$$,
|
||||
},
|
||||
};
|
||||
}
|
||||
return {
|
||||
namespace: ele,
|
||||
language: ele,
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
const now = Date.now();
|
||||
if (this.loadingRecord[ns] && now - this.loadingRecord[ns] < Locales.MINIMAL_LOADING_GAP) {
|
||||
return;
|
||||
}
|
||||
this.loadingRecord[ns] = now;
|
||||
const { data: newI18ns } = await this.cache.refresh('i18n', {
|
||||
data: {
|
||||
id: 1,
|
||||
|
|
@ -135,6 +144,7 @@ export class Locales<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncCo
|
|||
$$updateAt$$: 1,
|
||||
},
|
||||
filter: {
|
||||
namespace: ns,
|
||||
$or: filters,
|
||||
}
|
||||
}, undefined, undefined, undefined, true);
|
||||
|
|
@ -162,8 +172,8 @@ export class Locales<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncCo
|
|||
}
|
||||
|
||||
t(key: Scope, params?: TranslateOptions) {
|
||||
return key as string;
|
||||
// return this.i18n.t(key, params);
|
||||
// return key as string;
|
||||
return this.i18n.t(key, params);
|
||||
}
|
||||
|
||||
// 需要暴露给小程序
|
||||
|
|
|
|||
|
|
@ -949,6 +949,9 @@ export function createComponent<
|
|||
|
||||
async componentDidMount() {
|
||||
this.registerPageScroll();
|
||||
this.subscribed.push(
|
||||
features.locales.subscribe(() => this.reRender())
|
||||
);
|
||||
if (option.entity) {
|
||||
this.subscribed.push(
|
||||
features.cache.subscribe(() => this.reRender())
|
||||
|
|
|
|||
Loading…
Reference in New Issue