NlsBundle.ts
81 lines
| 2.4 KiB
| video/mp2t
|
TypeScriptLexer
cin
|
r55 | import { MapOf, PromiseOrValue } from "@implab/core-amd/interfaces"; | ||
import { argumentNotEmptyString, isPromise, mixin } from "@implab/core-amd/safe"; | ||||
cin
|
r0 | import { id as mid } from "module"; | ||
import { TraceSource } from "@implab/core-amd/log/TraceSource"; | ||||
const trace = TraceSource.get(mid); | ||||
cin
|
r55 | export type LocaleProvider<T> = () => PromiseOrValue<T | { default: T }>; | ||
cin
|
r0 | type ResolveCallback<T> = () => PromiseOrValue<T>; | ||
function when<T, T2>(value: PromiseOrValue<T>, cb: (v: T) => PromiseOrValue<T2>): PromiseOrValue<T2> { | ||||
return isPromise(value) ? | ||||
value.then(cb) : | ||||
cb(value); | ||||
} | ||||
function isCallback<T>(v: ResolveCallback<T> | PromiseOrValue<T>): v is ResolveCallback<T> { | ||||
return typeof v === "function"; | ||||
} | ||||
cin
|
r53 | function defaultResolver(module: string) { | ||
return import(module).then(x => x && x.default ? x.default : x); | ||||
} | ||||
cin
|
r0 | function chainObjects<T extends object>(o1: T, o2: T) { | ||
if (!o1) | ||||
return o2; | ||||
if (!o2) | ||||
return o1; | ||||
return mixin(Object.create(o1) as T, o2); | ||||
} | ||||
export class NlsBundle<T extends object> { | ||||
cin
|
r55 | private _locales: MapOf<LocaleProvider<Partial<T>>>; | ||
cin
|
r0 | |||
cin
|
r53 | private _default: T; | ||
private _cache: MapOf<PromiseOrValue<T>>; | ||||
cin
|
r0 | |||
cin
|
r55 | constructor(defNls: T, locales?: MapOf<LocaleProvider<Partial<T>>>) { | ||
cin
|
r53 | this._default = defNls; | ||
cin
|
r0 | this._locales = locales || {}; | ||
this._cache = {}; | ||||
} | ||||
cin
|
r55 | getLocale(locale: string) { | ||
argumentNotEmptyString(locale, "locale"); | ||||
const _loc = locale; | ||||
cin
|
r0 | |||
cin
|
r53 | // en-US => ["en", "en-US"] | ||
const locales = _loc.split(/-|_/).map((x, i, a) => a.slice(0, i + 1).join("-")); | ||||
cin
|
r0 | return this._resolveLocale(locales); | ||
} | ||||
_resolveLocale(locales: string[]): PromiseOrValue<T> { | ||||
if (!locales.length) | ||||
cin
|
r53 | return this._default; | ||
cin
|
r0 | |||
cin
|
r53 | const locale = locales.pop(); | ||
cin
|
r1 | if (!locale) | ||
cin
|
r53 | throw new Error("The locale can't be empty"); | ||
cin
|
r1 | |||
cin
|
r0 | if (this._cache[locale]) | ||
return this._cache[locale]; | ||||
cin
|
r53 | const data = this._loadPackage(this._locales[locale]); | ||
cin
|
r1 | const parent = this._resolveLocale(locales); | ||
cin
|
r0 | |||
return this._cache[locale] = when(data, x => { | ||||
return when(parent, y => this._cache[locale] = chainObjects(y, x)); | ||||
}); | ||||
} | ||||
cin
|
r53 | |||
_loadPackage(localeData: any) { | ||||
if (isCallback(localeData)) | ||||
return when(localeData(), data => data && "default" in data ? data.default : data); | ||||
return localeData; | ||||
} | ||||
cin
|
r0 | } | ||