1
1
import "./shims/jquery-shim.js" ;
2
2
import ResourceBundle from "@ui5/webcomponents-core/dist/sap/base/i18n/ResourceBundle.js" ;
3
+ import formatMessage from "@ui5/webcomponents-core/dist/sap/base/strings/formatMessage.js" ;
3
4
import { getLanguage } from "./LocaleProvider.js" ;
4
5
import { registerModuleContent } from "./ResourceLoaderOverrides.js" ;
5
6
import { fetchJsonOnce } from "./util/FetchHelper.js" ;
6
7
7
8
const bundleURLs = new Map ( ) ;
8
- const singletonPromises = new Map ( ) ;
9
-
10
- /**
11
- * Creates a new promise for the specified key (or returns a new one and stores it for next calls).
12
- * The same promise is always returned for multiple calls to this method for the same key.
13
- * This promise can also be resolved so that all usages that await its resolution can continue.
14
- * @param {key } key the unique key identifying the promise
15
- * @private
16
- */
17
- const _getSingletonPromise = key => {
18
- const prevPromise = singletonPromises . get ( key ) ;
19
- if ( prevPromise ) {
20
- return prevPromise ;
21
- }
22
-
23
- let resolveFn ;
24
- const newPromise = new Promise ( resolve => {
25
- resolveFn = resolve ;
26
- } ) ;
27
- // private usage for making a deferred-like API to avoid storing resolve functions in a second map
28
- newPromise . _deferredResolve = resolveFn ;
29
-
30
- singletonPromises . set ( key , newPromise ) ;
31
- return newPromise ;
32
- } ;
33
9
34
10
/**
35
11
* This method preforms the asyncronous task of fething the actual text resources. It will fetch
@@ -41,10 +17,14 @@ const _getSingletonPromise = key => {
41
17
* @public
42
18
*/
43
19
const fetchResourceBundle = async packageId => {
44
- // depending on the module resolution order, the fetch might run before the bundle URLs are registered - sync them here
45
- await _getSingletonPromise ( packageId ) ;
46
20
const bundlesForPackage = bundleURLs . get ( packageId ) ;
47
21
22
+ if ( ! bundlesForPackage ) {
23
+ console . warn ( `Message bundle assets are not configured. Falling back to english texts.` , /* eslint-disable-line */
24
+ ` You need to import @ui5/webcomponents/dist/MessageBundleAssets.js with a build tool that supports JSON imports.` ) ; /* eslint-disable-line */
25
+ return ;
26
+ }
27
+
48
28
const language = getLanguage ( ) ;
49
29
50
30
let localeId = ResourceBundle . __normalize ( language ) ;
@@ -72,13 +52,34 @@ const fetchResourceBundle = async packageId => {
72
52
*/
73
53
const registerMessageBundles = ( packageId , bundlesMap ) => {
74
54
bundleURLs . set ( packageId , bundlesMap ) ;
75
- _getSingletonPromise ( packageId ) . _deferredResolve ( ) ;
76
55
} ;
77
56
78
- const getResourceBundle = library => {
79
- return ResourceBundle . create ( {
80
- url : `${ library } .properties` ,
81
- } ) ;
57
+ class ResourceBundleFallback {
58
+ getText ( textObj , ...params ) {
59
+ return formatMessage ( textObj . defaultText , params ) ;
60
+ }
61
+ }
62
+
63
+ class ResourceBundleWrapper {
64
+ constructor ( resouceBundle ) {
65
+ this . _resourceBundle = resouceBundle ;
66
+ }
67
+
68
+ getText ( textObj , ...params ) {
69
+ return this . _resourceBundle . getText ( textObj . key , ...params ) ;
70
+ }
71
+ }
72
+
73
+ const getResourceBundle = packageId => {
74
+ const bundleLoaded = bundleURLs . has ( packageId ) ;
75
+
76
+ if ( bundleLoaded ) {
77
+ return new ResourceBundleWrapper ( ResourceBundle . create ( {
78
+ url : `${ packageId } .properties` ,
79
+ } ) ) ;
80
+ }
81
+
82
+ return new ResourceBundleFallback ( ) ;
82
83
} ;
83
84
84
85
export { fetchResourceBundle , registerMessageBundles , getResourceBundle } ;
0 commit comments