@@ -1,38 +1,38 | |||||
1 | import { MapOf } from "@implab/core-amd/interfaces"; |
|
1 | import { MapOf } from "@implab/core-amd/interfaces"; | |
2 | import { NlsBundle } from "./NlsBundle"; |
|
2 | import { NlsBundle } from "./NlsBundle"; | |
3 | import { isPromise } from "@implab/core-amd/safe"; |
|
3 | import { isPromise } from "@implab/core-amd/safe"; | |
4 |
|
4 | |||
5 | export interface OnLoad { |
|
5 | export interface OnLoad { | |
6 | (result?: any): void; |
|
6 | (result?: any): void; | |
7 | error(err: any): void; |
|
7 | error(err: any): void; | |
8 | } |
|
8 | } | |
9 |
|
9 | |||
10 | export function bundle<T extends object>(nls: T, locales?: MapOf<any>) { |
|
10 | export function bundle<T extends object>(nls: T, locales?: MapOf<any>) { | |
11 | const nlsBundle = new NlsBundle(nls, locales); |
|
11 | const nlsBundle = new NlsBundle(nls, locales); | |
12 |
|
12 | |||
13 | const fn = (locale?: string) => { |
|
13 | const fn = (locale?: string) => { | |
14 | const result = locale ? nlsBundle.getLocale(locale) : nlsBundle.default; |
|
14 | const result = locale ? nlsBundle.getLocale(locale) : nlsBundle.default; | |
15 |
|
15 | |||
16 | if (isPromise(result)) |
|
16 | if (isPromise(result)) | |
17 | throw new Error(`The bundle '${locale}' isn't loaded`); |
|
17 | throw new Error(`The bundle '${locale}' isn't loaded`); | |
18 | else |
|
18 | else | |
19 | return result; |
|
19 | return result; | |
20 | }; |
|
20 | }; | |
21 |
|
21 | |||
22 | fn.define = (pack: Partial<T>) => pack; |
|
22 | fn.define = (pack: Partial<T>) => pack; | |
23 | fn.default = nlsBundle.default; |
|
23 | fn.default = nlsBundle.default; | |
24 | fn.load = async (id: string, require: Require, cb: OnLoad, config: any) => { |
|
24 | fn.load = async (id: string, require: Require, cb: OnLoad, config: any) => { | |
25 | if (config.isBuild) { |
|
25 | if (config && config.isBuild) { | |
26 | cb(); |
|
26 | cb(); | |
27 | } else { |
|
27 | } else { | |
28 | try { |
|
28 | try { | |
29 | await nlsBundle.getLocale(id); |
|
29 | await nlsBundle.getLocale(id); | |
30 | cb(); |
|
30 | cb(); | |
31 | } catch (e) { |
|
31 | } catch (e) { | |
32 | cb.error(e); |
|
32 | cb.error(e); | |
33 | } |
|
33 | } | |
34 | } |
|
34 | } | |
35 | }; |
|
35 | }; | |
36 |
|
36 | |||
37 | return fn; |
|
37 | return fn; | |
38 | } |
|
38 | } |
General Comments 0
You need to be logged in to leave comments.
Login now