##// END OF EJS Templates
Added tag v1.0.10 for changeset d4f0cdae9577
Added tag v1.0.10 for changeset d4f0cdae9577

File last commit:

r55:dd0d589acfbb v1.0.8 default
r61:a5ead1578dfc default
Show More
NlsBundle.ts
81 lines | 2.4 KiB | video/mp2t | TypeScriptLexer
cin
i18n corrected default locale selection, more verbose error reporting
r55 import { MapOf, PromiseOrValue } from "@implab/core-amd/interfaces";
import { argumentNotEmptyString, isPromise, mixin } from "@implab/core-amd/safe";
cin
Initial commit, copied files related to .tsx scripts support.
r0 import { id as mid } from "module";
import { TraceSource } from "@implab/core-amd/log/TraceSource";
const trace = TraceSource.get(mid);
cin
i18n corrected default locale selection, more verbose error reporting
r55 export type LocaleProvider<T> = () => PromiseOrValue<T | { default: T }>;
cin
Initial commit, copied files related to .tsx scripts support.
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
corrected i18n, css modules to support requirejs optimizer...
r53 function defaultResolver(module: string) {
return import(module).then(x => x && x.default ? x.default : x);
}
cin
Initial commit, copied files related to .tsx scripts support.
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
i18n corrected default locale selection, more verbose error reporting
r55 private _locales: MapOf<LocaleProvider<Partial<T>>>;
cin
Initial commit, copied files related to .tsx scripts support.
r0
cin
corrected i18n, css modules to support requirejs optimizer...
r53 private _default: T;
private _cache: MapOf<PromiseOrValue<T>>;
cin
Initial commit, copied files related to .tsx scripts support.
r0
cin
i18n corrected default locale selection, more verbose error reporting
r55 constructor(defNls: T, locales?: MapOf<LocaleProvider<Partial<T>>>) {
cin
corrected i18n, css modules to support requirejs optimizer...
r53 this._default = defNls;
cin
Initial commit, copied files related to .tsx scripts support.
r0 this._locales = locales || {};
this._cache = {};
}
cin
i18n corrected default locale selection, more verbose error reporting
r55 getLocale(locale: string) {
argumentNotEmptyString(locale, "locale");
const _loc = locale;
cin
Initial commit, copied files related to .tsx scripts support.
r0
cin
corrected i18n, css modules to support requirejs optimizer...
r53 // en-US => ["en", "en-US"]
const locales = _loc.split(/-|_/).map((x, i, a) => a.slice(0, i + 1).join("-"));
cin
Initial commit, copied files related to .tsx scripts support.
r0 return this._resolveLocale(locales);
}
_resolveLocale(locales: string[]): PromiseOrValue<T> {
if (!locales.length)
cin
corrected i18n, css modules to support requirejs optimizer...
r53 return this._default;
cin
Initial commit, copied files related to .tsx scripts support.
r0
cin
corrected i18n, css modules to support requirejs optimizer...
r53 const locale = locales.pop();
cin
typescript strict mode...
r1 if (!locale)
cin
corrected i18n, css modules to support requirejs optimizer...
r53 throw new Error("The locale can't be empty");
cin
typescript strict mode...
r1
cin
Initial commit, copied files related to .tsx scripts support.
r0 if (this._cache[locale])
return this._cache[locale];
cin
corrected i18n, css modules to support requirejs optimizer...
r53 const data = this._loadPackage(this._locales[locale]);
cin
typescript strict mode...
r1 const parent = this._resolveLocale(locales);
cin
Initial commit, copied files related to .tsx scripts support.
r0
return this._cache[locale] = when(data, x => {
return when(parent, y => this._cache[locale] = chainObjects(y, x));
});
}
cin
corrected i18n, css modules to support requirejs optimizer...
r53
_loadPackage(localeData: any) {
if (isCallback(localeData))
return when(localeData(), data => data && "default" in data ? data.default : data);
return localeData;
}
cin
Initial commit, copied files related to .tsx scripts support.
r0 }