@@ -1,16 +1,14 | |||
|
1 | import { MapOf } from "@implab/core-amd/interfaces"; | |
|
2 | import { isPromise, mixin } from "@implab/core-amd/safe"; | |
|
3 | import { locale as sysLocale } from "dojo/_base/kernel"; | |
|
1 | import { MapOf, PromiseOrValue } from "@implab/core-amd/interfaces"; | |
|
2 | import { argumentNotEmptyString, isPromise, mixin } from "@implab/core-amd/safe"; | |
|
4 | 3 | import { id as mid } from "module"; |
|
5 | 4 | import { TraceSource } from "@implab/core-amd/log/TraceSource"; |
|
6 | 5 | |
|
7 | 6 | const trace = TraceSource.get(mid); |
|
8 | 7 | |
|
9 | type PromiseOrValue<T> = PromiseLike<T> | T; | |
|
8 | export type LocaleProvider<T> = () => PromiseOrValue<T | { default: T }>; | |
|
9 | ||
|
10 | 10 | type ResolveCallback<T> = () => PromiseOrValue<T>; |
|
11 | 11 | |
|
12 | trace.debug("Current sysLocale: {0}", sysLocale); | |
|
13 | ||
|
14 | 12 | function when<T, T2>(value: PromiseOrValue<T>, cb: (v: T) => PromiseOrValue<T2>): PromiseOrValue<T2> { |
|
15 | 13 | return isPromise(value) ? |
|
16 | 14 | value.then(cb) : |
@@ -35,20 +33,21 function chainObjects<T extends object>( | |||
|
35 | 33 | } |
|
36 | 34 | |
|
37 | 35 | export class NlsBundle<T extends object> { |
|
38 |
private _locales: MapOf< |
|
|
36 | private _locales: MapOf<LocaleProvider<Partial<T>>>; | |
|
39 | 37 | |
|
40 | 38 | private _default: T; |
|
41 | 39 | |
|
42 | 40 | private _cache: MapOf<PromiseOrValue<T>>; |
|
43 | 41 | |
|
44 |
constructor(defNls: T, locales?: MapOf< |
|
|
42 | constructor(defNls: T, locales?: MapOf<LocaleProvider<Partial<T>>>) { | |
|
45 | 43 | this._default = defNls; |
|
46 | 44 | this._locales = locales || {}; |
|
47 | 45 | this._cache = {}; |
|
48 | 46 | } |
|
49 | 47 | |
|
50 |
getLocale(locale |
|
|
51 | const _loc = locale ?? sysLocale; | |
|
48 | getLocale(locale: string) { | |
|
49 | argumentNotEmptyString(locale, "locale"); | |
|
50 | const _loc = locale; | |
|
52 | 51 | |
|
53 | 52 | // en-US => ["en", "en-US"] |
|
54 | 53 | const locales = _loc.split(/-|_/).map((x, i, a) => a.slice(0, i + 1).join("-")); |
@@ -1,6 +1,13 | |||
|
1 | import { id as mid} from "module"; | |
|
1 | 2 | import { MapOf } from "@implab/core-amd/interfaces"; |
|
2 | 3 | import { NlsBundle } from "./NlsBundle"; |
|
3 | 4 | import { isPromise } from "@implab/core-amd/safe"; |
|
5 | import { locale as sysLocale } from "dojo/_base/kernel"; | |
|
6 | import { TraceSource } from "@implab/core-amd/log/TraceSource"; | |
|
7 | ||
|
8 | const trace = TraceSource.get(mid); | |
|
9 | ||
|
10 | trace.debug("Current sysLocale: {0}", sysLocale); | |
|
4 | 11 | |
|
5 | 12 | export interface OnLoad { |
|
6 | 13 | (result?: any): void; |
@@ -10,7 +17,8 export interface OnLoad { | |||
|
10 | 17 | export function bundle<T extends object>(nls: T, locales?: MapOf<any>) { |
|
11 | 18 | const nlsBundle = new NlsBundle(nls, locales); |
|
12 | 19 | |
|
13 | const fn = (locale?: string) => { | |
|
20 | const fn = (_locale?: string) => { | |
|
21 | const locale = _locale || sysLocale; | |
|
14 | 22 | const result = nlsBundle.getLocale(locale); |
|
15 | 23 | |
|
16 | 24 | if (isPromise(result)) |
@@ -21,15 +29,20 export function bundle<T extends object> | |||
|
21 | 29 | |
|
22 | 30 | fn.define = (pack: Partial<T>) => pack; |
|
23 | 31 | fn.load = async (id: string, require: Require, cb: OnLoad, config: any) => { |
|
32 | const locale = id || sysLocale; | |
|
24 | 33 | if (config && config.isBuild) { |
|
25 | 34 | cb(); |
|
26 | 35 | } else { |
|
27 | 36 | try { |
|
28 |
await nlsBundle.getLocale( |
|
|
37 | await nlsBundle.getLocale(locale); | |
|
29 | 38 | cb(); |
|
30 | 39 | } catch (e) { |
|
31 | if(cb.error) | |
|
40 | if(cb.error) { | |
|
32 | 41 | cb.error(e); |
|
42 | } else { | |
|
43 | // in case the loader doesn't support error reporting | |
|
44 | trace.error("Error loading {0}: {1}", locale, e); | |
|
45 | } | |
|
33 | 46 | } |
|
34 | 47 | } |
|
35 | 48 | }; |
General Comments 0
You need to be logged in to leave comments.
Login now