# HG changeset patch # User cin # Date 2021-02-18 11:23:19 # Node ID dd0d589acfbbe938a5630547e83ad934ab597b64 # Parent 3a881e2e1ef5670469191c3cd0327975f32dbec0 i18n corrected default locale selection, more verbose error reporting diff --git a/src/main/ts/NlsBundle.ts b/src/main/ts/NlsBundle.ts --- a/src/main/ts/NlsBundle.ts +++ b/src/main/ts/NlsBundle.ts @@ -1,16 +1,14 @@ -import { MapOf } from "@implab/core-amd/interfaces"; -import { isPromise, mixin } from "@implab/core-amd/safe"; -import { locale as sysLocale } from "dojo/_base/kernel"; +import { MapOf, PromiseOrValue } from "@implab/core-amd/interfaces"; +import { argumentNotEmptyString, isPromise, mixin } from "@implab/core-amd/safe"; import { id as mid } from "module"; import { TraceSource } from "@implab/core-amd/log/TraceSource"; const trace = TraceSource.get(mid); -type PromiseOrValue = PromiseLike | T; +export type LocaleProvider = () => PromiseOrValue; + type ResolveCallback = () => PromiseOrValue; -trace.debug("Current sysLocale: {0}", sysLocale); - function when(value: PromiseOrValue, cb: (v: T) => PromiseOrValue): PromiseOrValue { return isPromise(value) ? value.then(cb) : @@ -35,20 +33,21 @@ function chainObjects( } export class NlsBundle { - private _locales: MapOf | PromiseOrValue>; + private _locales: MapOf>>; private _default: T; private _cache: MapOf>; - constructor(defNls: T, locales?: MapOf) { + constructor(defNls: T, locales?: MapOf>>) { this._default = defNls; this._locales = locales || {}; this._cache = {}; } - getLocale(locale?: string) { - const _loc = locale ?? sysLocale; + getLocale(locale: string) { + argumentNotEmptyString(locale, "locale"); + const _loc = locale; // en-US => ["en", "en-US"] const locales = _loc.split(/-|_/).map((x, i, a) => a.slice(0, i + 1).join("-")); diff --git a/src/main/ts/i18n.ts b/src/main/ts/i18n.ts --- a/src/main/ts/i18n.ts +++ b/src/main/ts/i18n.ts @@ -1,6 +1,13 @@ +import { id as mid} from "module"; import { MapOf } from "@implab/core-amd/interfaces"; import { NlsBundle } from "./NlsBundle"; 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 { (result?: any): void; @@ -10,7 +17,8 @@ export interface OnLoad { export function bundle(nls: T, locales?: MapOf) { const nlsBundle = new NlsBundle(nls, locales); - const fn = (locale?: string) => { + const fn = (_locale?: string) => { + const locale = _locale || sysLocale; const result = nlsBundle.getLocale(locale); if (isPromise(result)) @@ -21,15 +29,20 @@ export function bundle fn.define = (pack: Partial) => pack; fn.load = async (id: string, require: Require, cb: OnLoad, config: any) => { + const locale = id || sysLocale; if (config && config.isBuild) { cb(); } else { try { - await nlsBundle.getLocale(id); + await nlsBundle.getLocale(locale); cb(); } catch (e) { - if(cb.error) + if(cb.error) { cb.error(e); + } else { + // in case the loader doesn't support error reporting + trace.error("Error loading {0}: {1}", locale, e); + } } } }; diff --git a/src/test/ts/i18n/foo.ts b/src/test/ts/i18n/foo.ts --- a/src/test/ts/i18n/foo.ts +++ b/src/test/ts/i18n/foo.ts @@ -6,3 +6,4 @@ export default bundle({ }, { ru: () => import("./ru/foo") }); +