@discord/intl 0.18.0 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/message-loader.d.ts +8 -19
- package/dist/message-loader.js +19 -30
- package/package.json +2 -2
- package/src/message-loader.ts +19 -36
package/dist/message-loader.d.ts
CHANGED
|
@@ -73,7 +73,7 @@ export declare class MessageLoader {
|
|
|
73
73
|
* @private
|
|
74
74
|
*/
|
|
75
75
|
_localeFileMap?: Record<string, string>;
|
|
76
|
-
constructor(
|
|
76
|
+
constructor(localeImportMap: LocaleImportMap, defaultLocale: LocaleId);
|
|
77
77
|
/**
|
|
78
78
|
* Provide additional debug information to use during development, providing additional context
|
|
79
79
|
* for console error messages.
|
|
@@ -93,20 +93,6 @@ export declare class MessageLoader {
|
|
|
93
93
|
* returning.
|
|
94
94
|
*/
|
|
95
95
|
getMessageValue(key: string, locale: LocaleId): InternalIntlMessage | undefined;
|
|
96
|
-
/**
|
|
97
|
-
* Returns a record mapping the keys this object manages to bound functions
|
|
98
|
-
* for `get` with the that key as the first argument, allowing consumers to
|
|
99
|
-
* just call the function with a locale to retrieve the translated message
|
|
100
|
-
* for that key.
|
|
101
|
-
*
|
|
102
|
-
* This method is provided as a way to generate binds _at runtime_, but for
|
|
103
|
-
* very-large messages files (e.g., thousands of messages), this can be a
|
|
104
|
-
* non-negligible cost. Where feasible, consider generating these binds
|
|
105
|
-
* at build/bundle time, especially in cases where they can be substantially
|
|
106
|
-
* minified (i.e., Hermes bytecode). This is provided automatically as an
|
|
107
|
-
* option when using one of `@discord/intl`'s transformers.
|
|
108
|
-
*/
|
|
109
|
-
getBinds(): Record<string, IntlMessageGetter>;
|
|
110
96
|
_loadLocale(locale: LocaleId): Promise<void>;
|
|
111
97
|
/**
|
|
112
98
|
* Inform subscribers that the loader state has changed and they should
|
|
@@ -145,9 +131,12 @@ export declare function loadAllMessagesInLocale(locale: LocaleId): Promise<void>
|
|
|
145
131
|
*/
|
|
146
132
|
export declare function waitForAllDefaultIntlMessagesLoaded(): Promise<void>;
|
|
147
133
|
/**
|
|
148
|
-
* Create a new MessageLoader, which handles lazily loading messages for
|
|
149
|
-
*
|
|
150
|
-
*
|
|
134
|
+
* Create a new MessageLoader, which handles lazily loading messages for different locales and
|
|
135
|
+
* sanity checks as needed to provide accessors for each message contained by the import map.
|
|
136
|
+
*
|
|
137
|
+
* Notably, this does _not_ verify whether the locale objects managed by this loader actually
|
|
138
|
+
* contain a given key. It is the responsibility of the caller to verify this (easily enforced with
|
|
139
|
+
* typescript, eslint rules, and others).
|
|
151
140
|
*/
|
|
152
|
-
export declare function createLoader(
|
|
141
|
+
export declare function createLoader(localeImportMap: LocaleImportMap, defaultLocale: LocaleId): MessageLoader;
|
|
153
142
|
export {};
|
package/dist/message-loader.js
CHANGED
|
@@ -15,8 +15,7 @@ exports.waitForAllDefaultIntlMessagesLoaded = waitForAllDefaultIntlMessagesLoade
|
|
|
15
15
|
exports.createLoader = createLoader;
|
|
16
16
|
const message_1 = require("./message");
|
|
17
17
|
class MessageLoader {
|
|
18
|
-
constructor(
|
|
19
|
-
this.messageKeys = messageKeys;
|
|
18
|
+
constructor(localeImportMap, defaultLocale) {
|
|
20
19
|
this.messages = {};
|
|
21
20
|
this.localeImportMap = localeImportMap;
|
|
22
21
|
this.supportedLocales = Object.keys(localeImportMap);
|
|
@@ -99,9 +98,16 @@ class MessageLoader {
|
|
|
99
98
|
}
|
|
100
99
|
return undefined;
|
|
101
100
|
}
|
|
102
|
-
// Then try to return the loaded message.
|
|
103
|
-
|
|
104
|
-
|
|
101
|
+
// Then try to return the loaded message. Previously, this used a `key in ...` check to quickly
|
|
102
|
+
// verify whether the locale object contains the requested message, but in the case where the
|
|
103
|
+
// target is a Proxy or uses some other internalized method for providing gettable values, `in`
|
|
104
|
+
// queries aren't guaranteed to work. Instead, since this is realistically amortized to just be
|
|
105
|
+
// a property access on every call, we can just the returned value as the determining factor. If
|
|
106
|
+
// the locale object returns `undefined`, then it "does not contain" the message, whether it's
|
|
107
|
+
// still loading or otherwise. Future calls will then be able to pick up any changed value,
|
|
108
|
+
// since it won't be cached in the `_parseCache`.
|
|
109
|
+
const content = this.messages[locale][key];
|
|
110
|
+
if (content != null) {
|
|
105
111
|
const message = new message_1.InternalIntlMessage(content, locale);
|
|
106
112
|
((_b = (_c = this._parseCache)[locale]) !== null && _b !== void 0 ? _b : (_c[locale] = {}))[key] = message;
|
|
107
113
|
return message;
|
|
@@ -109,26 +115,6 @@ class MessageLoader {
|
|
|
109
115
|
// Otherwise just assume it doesn't exist.
|
|
110
116
|
return undefined;
|
|
111
117
|
}
|
|
112
|
-
/**
|
|
113
|
-
* Returns a record mapping the keys this object manages to bound functions
|
|
114
|
-
* for `get` with the that key as the first argument, allowing consumers to
|
|
115
|
-
* just call the function with a locale to retrieve the translated message
|
|
116
|
-
* for that key.
|
|
117
|
-
*
|
|
118
|
-
* This method is provided as a way to generate binds _at runtime_, but for
|
|
119
|
-
* very-large messages files (e.g., thousands of messages), this can be a
|
|
120
|
-
* non-negligible cost. Where feasible, consider generating these binds
|
|
121
|
-
* at build/bundle time, especially in cases where they can be substantially
|
|
122
|
-
* minified (i.e., Hermes bytecode). This is provided automatically as an
|
|
123
|
-
* option when using one of `@discord/intl`'s transformers.
|
|
124
|
-
*/
|
|
125
|
-
getBinds() {
|
|
126
|
-
const result = {};
|
|
127
|
-
for (const key of this.messageKeys) {
|
|
128
|
-
result[key] = this.get.bind(this, key);
|
|
129
|
-
}
|
|
130
|
-
return result;
|
|
131
|
-
}
|
|
132
118
|
_loadLocale(locale) {
|
|
133
119
|
return __awaiter(this, void 0, void 0, function* () {
|
|
134
120
|
var _a, _b, _c, _d;
|
|
@@ -248,12 +234,15 @@ function waitForAllDefaultIntlMessagesLoaded() {
|
|
|
248
234
|
});
|
|
249
235
|
}
|
|
250
236
|
/**
|
|
251
|
-
* Create a new MessageLoader, which handles lazily loading messages for
|
|
252
|
-
*
|
|
253
|
-
*
|
|
237
|
+
* Create a new MessageLoader, which handles lazily loading messages for different locales and
|
|
238
|
+
* sanity checks as needed to provide accessors for each message contained by the import map.
|
|
239
|
+
*
|
|
240
|
+
* Notably, this does _not_ verify whether the locale objects managed by this loader actually
|
|
241
|
+
* contain a given key. It is the responsibility of the caller to verify this (easily enforced with
|
|
242
|
+
* typescript, eslint rules, and others).
|
|
254
243
|
*/
|
|
255
|
-
function createLoader(
|
|
256
|
-
const loader = new MessageLoader(
|
|
244
|
+
function createLoader(localeImportMap, defaultLocale) {
|
|
245
|
+
const loader = new MessageLoader(localeImportMap, defaultLocale);
|
|
257
246
|
LOADER_REGISTRY.push(loader);
|
|
258
247
|
return loader;
|
|
259
248
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@discord/intl",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.19.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Client runtime for managing messages and translations in a React project.",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"@formatjs/intl": "^2.10.1",
|
|
24
24
|
"@intrnl/xxhash64": "^0.1.2",
|
|
25
25
|
"intl-messageformat": "^10.5.11",
|
|
26
|
-
"@discord/intl-ast": "0.
|
|
26
|
+
"@discord/intl-ast": "0.19.0"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@formatjs/intl-durationformat": "^0.7.3",
|
package/src/message-loader.ts
CHANGED
|
@@ -85,8 +85,7 @@ export class MessageLoader {
|
|
|
85
85
|
*/
|
|
86
86
|
_localeFileMap?: Record<string, string>;
|
|
87
87
|
|
|
88
|
-
constructor(
|
|
89
|
-
this.messageKeys = messageKeys;
|
|
88
|
+
constructor(localeImportMap: LocaleImportMap, defaultLocale: LocaleId) {
|
|
90
89
|
this.messages = {};
|
|
91
90
|
this.localeImportMap = localeImportMap;
|
|
92
91
|
this.supportedLocales = Object.keys(localeImportMap);
|
|
@@ -178,9 +177,16 @@ export class MessageLoader {
|
|
|
178
177
|
return undefined;
|
|
179
178
|
}
|
|
180
179
|
|
|
181
|
-
// Then try to return the loaded message.
|
|
182
|
-
|
|
183
|
-
|
|
180
|
+
// Then try to return the loaded message. Previously, this used a `key in ...` check to quickly
|
|
181
|
+
// verify whether the locale object contains the requested message, but in the case where the
|
|
182
|
+
// target is a Proxy or uses some other internalized method for providing gettable values, `in`
|
|
183
|
+
// queries aren't guaranteed to work. Instead, since this is realistically amortized to just be
|
|
184
|
+
// a property access on every call, we can just the returned value as the determining factor. If
|
|
185
|
+
// the locale object returns `undefined`, then it "does not contain" the message, whether it's
|
|
186
|
+
// still loading or otherwise. Future calls will then be able to pick up any changed value,
|
|
187
|
+
// since it won't be cached in the `_parseCache`.
|
|
188
|
+
const content = this.messages[locale][key];
|
|
189
|
+
if (content != null) {
|
|
184
190
|
const message = new InternalIntlMessage(content, locale);
|
|
185
191
|
(this._parseCache[locale] ??= {})[key] = message;
|
|
186
192
|
return message;
|
|
@@ -190,28 +196,6 @@ export class MessageLoader {
|
|
|
190
196
|
return undefined;
|
|
191
197
|
}
|
|
192
198
|
|
|
193
|
-
/**
|
|
194
|
-
* Returns a record mapping the keys this object manages to bound functions
|
|
195
|
-
* for `get` with the that key as the first argument, allowing consumers to
|
|
196
|
-
* just call the function with a locale to retrieve the translated message
|
|
197
|
-
* for that key.
|
|
198
|
-
*
|
|
199
|
-
* This method is provided as a way to generate binds _at runtime_, but for
|
|
200
|
-
* very-large messages files (e.g., thousands of messages), this can be a
|
|
201
|
-
* non-negligible cost. Where feasible, consider generating these binds
|
|
202
|
-
* at build/bundle time, especially in cases where they can be substantially
|
|
203
|
-
* minified (i.e., Hermes bytecode). This is provided automatically as an
|
|
204
|
-
* option when using one of `@discord/intl`'s transformers.
|
|
205
|
-
*/
|
|
206
|
-
getBinds(): Record<string, IntlMessageGetter> {
|
|
207
|
-
const result: Record<string, IntlMessageGetter> = {};
|
|
208
|
-
for (const key of this.messageKeys) {
|
|
209
|
-
result[key] = this.get.bind(this, key);
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
return result;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
199
|
async _loadLocale(locale: LocaleId) {
|
|
216
200
|
// If the locale is already set in `messages`, then it doesn't need to be loaded again.
|
|
217
201
|
if (this.messages[locale] != null) return;
|
|
@@ -328,16 +312,15 @@ export async function waitForAllDefaultIntlMessagesLoaded(): Promise<void> {
|
|
|
328
312
|
}
|
|
329
313
|
|
|
330
314
|
/**
|
|
331
|
-
* Create a new MessageLoader, which handles lazily loading messages for
|
|
332
|
-
*
|
|
333
|
-
*
|
|
315
|
+
* Create a new MessageLoader, which handles lazily loading messages for different locales and
|
|
316
|
+
* sanity checks as needed to provide accessors for each message contained by the import map.
|
|
317
|
+
*
|
|
318
|
+
* Notably, this does _not_ verify whether the locale objects managed by this loader actually
|
|
319
|
+
* contain a given key. It is the responsibility of the caller to verify this (easily enforced with
|
|
320
|
+
* typescript, eslint rules, and others).
|
|
334
321
|
*/
|
|
335
|
-
export function createLoader(
|
|
336
|
-
|
|
337
|
-
localeImportMap: LocaleImportMap,
|
|
338
|
-
defaultLocale: LocaleId,
|
|
339
|
-
) {
|
|
340
|
-
const loader = new MessageLoader(messageKeys, localeImportMap, defaultLocale);
|
|
322
|
+
export function createLoader(localeImportMap: LocaleImportMap, defaultLocale: LocaleId) {
|
|
323
|
+
const loader = new MessageLoader(localeImportMap, defaultLocale);
|
|
341
324
|
LOADER_REGISTRY.push(loader);
|
|
342
325
|
return loader;
|
|
343
326
|
}
|