##// END OF EJS Templates
WIP observables
WIP observables

File last commit:

r122:fb2ea4d6aaba v1.6.3 default
r157:f9518367061a default
Show More
i18n.ts
57 lines | 1.8 KiB | video/mp2t | TypeScriptLexer
import { id as mid} from "module";
import { PromiseOrValue } from "@implab/core-amd/interfaces";
import { NlsBundle } from "./NlsBundle";
import { isPromise } from "@implab/core-amd/safe";
import { locale as sysLocale } from "dojo/_base/kernel";
import { TraceSource } from "@implab/core-amd/log/TraceSource";
const trace = TraceSource.get(mid);
trace.debug("Current sysLocale: {0}", sysLocale);
export interface OnLoad {
(result?: unknown): void;
error(err: unknown): void;
}
export const bundle = <T extends object>(nls: T, locales?: Record<string, () => PromiseOrValue<object>>) : {
i18n: (this: void, locale?: string) => T;
define: (this: void, pack: Partial<T>) => object;
load: (this: void, id: string, require: Require, cb: OnLoad, config: {isBuild?: boolean}) => void;
} => {
const nlsBundle = new NlsBundle(nls, locales);
const fn = (_locale?: string) => {
const locale = _locale || sysLocale;
const result = nlsBundle.getLocale(locale);
if (isPromise(result))
throw new Error(`The bundle '${locale}' isn't loaded`);
else
return result;
};
fn.i18n = fn;
fn.nls = fn;
fn.define = (pack: Partial<T>): object => pack;
fn.load = async (id: string, require: Require, cb: OnLoad, config: {isBuild?: boolean}) => {
const locale = id || sysLocale;
if (config && config.isBuild) {
cb();
} else {
try {
await nlsBundle.getLocale(locale);
cb();
} catch (e) {
if(cb.error) {
cb.error(e);
} else {
// in case the loader doesn't support error reporting
trace.error("Error loading {0}: {1}", locale, e);
}
}
}
};
return fn;
};