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