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