i18n.ts
51 lines
| 1.6 KiB
| video/mp2t
|
TypeScriptLexer
|
|
r65 | import { id as mid} from "module"; | ||
| import { MapOf } from "@implab/core-amd/interfaces"; | ||||
|
|
r109 | import { LocaleProvider, NlsBundle } from "./NlsBundle"; | ||
|
|
r65 | 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 { | ||||
|
|
r109 | (result?: unknown): void; | ||
| error(err: unknown): void; | ||||
|
|
r65 | } | ||
|
|
r109 | export function bundle<T extends object>(nls: T, locales?: MapOf<LocaleProvider<object>>) { | ||
|
|
r65 | 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.define = (pack: Partial<T>) => pack; | ||||
|
|
r109 | fn.load = async (id: string, require: Require, cb: OnLoad, config: {isBuild?: boolean}) => { | ||
|
|
r65 | 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; | ||||
| } | ||||
