i18n.ts
51 lines
| 1.5 KiB
| video/mp2t
|
TypeScriptLexer
cin
|
r55 | import { id as mid} from "module"; | ||
cin
|
r0 | import { MapOf } from "@implab/core-amd/interfaces"; | ||
import { NlsBundle } from "./NlsBundle"; | ||||
import { isPromise } from "@implab/core-amd/safe"; | ||||
cin
|
r55 | 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); | ||||
cin
|
r0 | |||
cin
|
r44 | export interface OnLoad { | ||
cin
|
r36 | (result?: any): void; | ||
error(err: any): void; | ||||
} | ||||
cin
|
r0 | export function bundle<T extends object>(nls: T, locales?: MapOf<any>) { | ||
const nlsBundle = new NlsBundle(nls, locales); | ||||
cin
|
r55 | const fn = (_locale?: string) => { | ||
const locale = _locale || sysLocale; | ||||
cin
|
r53 | const result = nlsBundle.getLocale(locale); | ||
cin
|
r2 | |||
cin
|
r0 | if (isPromise(result)) | ||
throw new Error(`The bundle '${locale}' isn't loaded`); | ||||
else | ||||
return result; | ||||
}; | ||||
fn.define = (pack: Partial<T>) => pack; | ||||
cin
|
r36 | fn.load = async (id: string, require: Require, cb: OnLoad, config: any) => { | ||
cin
|
r55 | const locale = id || sysLocale; | ||
cin
|
r46 | if (config && config.isBuild) { | ||
cin
|
r0 | cb(); | ||
cin
|
r36 | } else { | ||
try { | ||||
cin
|
r55 | await nlsBundle.getLocale(locale); | ||
cin
|
r36 | cb(); | ||
} catch (e) { | ||||
cin
|
r55 | if(cb.error) { | ||
cin
|
r53 | cb.error(e); | ||
cin
|
r55 | } else { | ||
// in case the loader doesn't support error reporting | ||||
trace.error("Error loading {0}: {1}", locale, e); | ||||
} | ||||
cin
|
r36 | } | ||
cin
|
r0 | } | ||
}; | ||||
return fn; | ||||
} | ||||