i18n.ts
38 lines
| 1.0 KiB
| video/mp2t
|
TypeScriptLexer
cin
|
r0 | import { MapOf } from "@implab/core-amd/interfaces"; | ||
import { NlsBundle } from "./NlsBundle"; | ||||
import { isPromise } from "@implab/core-amd/safe"; | ||||
cin
|
r36 | interface OnLoad { | ||
(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); | ||||
const fn = (locale?: string) => { | ||||
cin
|
r2 | const result = locale ? nlsBundle.getLocale(locale) : nlsBundle.default; | ||
cin
|
r0 | if (isPromise(result)) | ||
throw new Error(`The bundle '${locale}' isn't loaded`); | ||||
else | ||||
return result; | ||||
}; | ||||
fn.define = (pack: Partial<T>) => pack; | ||||
fn.default = nlsBundle.default; | ||||
cin
|
r36 | fn.load = async (id: string, require: Require, cb: OnLoad, config: any) => { | ||
if (config.isBuild) { | ||||
cin
|
r0 | cb(); | ||
cin
|
r36 | } else { | ||
try { | ||||
await nlsBundle.getLocale(id); | ||||
cb(); | ||||
} catch (e) { | ||||
cb.error(e); | ||||
} | ||||
cin
|
r0 | } | ||
}; | ||||
return fn; | ||||
} | ||||