@discord/intl 0.10.0 → 0.10.2-canary.6656457
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 +11 -9
- package/dist/message-loader.js +8 -4
- package/dist/types.d.ts +2 -4
- package/package.json +7 -13
- package/src/message-loader.ts +12 -13
- package/src/types.d.ts +2 -4
package/dist/message-loader.d.ts
CHANGED
|
@@ -2,17 +2,12 @@ import { AstNode, FullFormatJsNode } from '@discord/intl-ast';
|
|
|
2
2
|
import { InternalIntlMessage } from './message';
|
|
3
3
|
/**
|
|
4
4
|
* Type representing the serialized content of a translations file, which is a record of hashed
|
|
5
|
-
* message keys to their AST structure. This type represents both compressed, keyless AstNodes
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* message keys to their AST structure. This type represents both compressed, keyless AstNodes and
|
|
6
|
+
* fully-typed, object FullFormatJsNodes as the message content, since either can be given depending
|
|
7
|
+
* on the configuration of the bundler/compiler.
|
|
8
8
|
*/
|
|
9
9
|
type MessagesData = Record<string, AstNode[] | FullFormatJsNode[]>;
|
|
10
|
-
export
|
|
11
|
-
onChange(callback: () => void): () => void;
|
|
12
|
-
}
|
|
13
|
-
export interface IntlMessageGetter extends IntlMessageGetterAdditions {
|
|
14
|
-
(locale: LocaleId): InternalIntlMessage;
|
|
15
|
-
}
|
|
10
|
+
export type IntlMessageGetter = (this: MessageLoader, locale: LocaleId) => InternalIntlMessage;
|
|
16
11
|
export type LocaleId = string;
|
|
17
12
|
export type LocaleImportMap = Record<LocaleId, () => Promise<{
|
|
18
13
|
default: MessagesData;
|
|
@@ -103,6 +98,13 @@ export declare class MessageLoader {
|
|
|
103
98
|
* for `get` with the that key as the first argument, allowing consumers to
|
|
104
99
|
* just call the function with a locale to retrieve the translated message
|
|
105
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.
|
|
106
108
|
*/
|
|
107
109
|
getBinds(): Record<string, IntlMessageGetter>;
|
|
108
110
|
_loadLocale(locale: LocaleId): Promise<void>;
|
package/dist/message-loader.js
CHANGED
|
@@ -105,14 +105,18 @@ class MessageLoader {
|
|
|
105
105
|
* for `get` with the that key as the first argument, allowing consumers to
|
|
106
106
|
* just call the function with a locale to retrieve the translated message
|
|
107
107
|
* for that key.
|
|
108
|
+
*
|
|
109
|
+
* This method is provided as a way to generate binds _at runtime_, but for
|
|
110
|
+
* very-large messages files (e.g., thousands of messages), this can be a
|
|
111
|
+
* non-negligible cost. Where feasible, consider generating these binds
|
|
112
|
+
* at build/bundle time, especially in cases where they can be substantially
|
|
113
|
+
* minified (i.e., Hermes bytecode). This is provided automatically as an
|
|
114
|
+
* option when using one of `@discord/intl`'s transformers.
|
|
108
115
|
*/
|
|
109
116
|
getBinds() {
|
|
110
|
-
const onChange = this.onChange.bind(this);
|
|
111
117
|
const result = {};
|
|
112
118
|
for (const key of this.messageKeys) {
|
|
113
|
-
|
|
114
|
-
bound.onChange = onChange;
|
|
115
|
-
result[key] = bound;
|
|
119
|
+
result[key] = this.get.bind(this, key);
|
|
116
120
|
}
|
|
117
121
|
return result;
|
|
118
122
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { InternalIntlMessage } from './message';
|
|
2
|
-
import type { IntlMessageGetterAdditions } from './message-loader';
|
|
3
2
|
import { FormatBuilderConstructor } from './format';
|
|
4
3
|
|
|
5
4
|
/**
|
|
@@ -42,7 +41,7 @@ export type IntlTime = string | number | Date | null | undefined;
|
|
|
42
41
|
export type PlainIntlMessage = string;
|
|
43
42
|
export type AnyIntlMessage = PlainIntlMessage | InternalIntlMessage;
|
|
44
43
|
|
|
45
|
-
export interface IntlMessageGetter
|
|
44
|
+
export interface IntlMessageGetter {
|
|
46
45
|
(locale: string): InternalIntlMessage | PlainIntlMessage;
|
|
47
46
|
}
|
|
48
47
|
|
|
@@ -52,8 +51,7 @@ export interface IntlMessageGetter extends IntlMessageGetterAdditions {
|
|
|
52
51
|
* value types that are required to format the message, or `{}` to represent
|
|
53
52
|
* that there are no values required and the message is a plain string.
|
|
54
53
|
*/
|
|
55
|
-
export interface TypedIntlMessageGetter<FormatValues extends object>
|
|
56
|
-
extends IntlMessageGetterAdditions {
|
|
54
|
+
export interface TypedIntlMessageGetter<FormatValues extends object> {
|
|
57
55
|
// TODO: This is lossy and unfortunate that typing can't be propagated
|
|
58
56
|
// to the returned message type, but doing so causes problems with
|
|
59
57
|
// contravariance of the type. When returning a message getter from a
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@discord/intl",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.2-canary.6656457",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Client runtime for managing messages and translations in a React project.",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -14,12 +14,16 @@
|
|
|
14
14
|
"dist",
|
|
15
15
|
"src"
|
|
16
16
|
],
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "https://github.com/discord/discord-intl"
|
|
20
|
+
},
|
|
17
21
|
"dependencies": {
|
|
18
22
|
"@formatjs/icu-skeleton-parser": "1.8.2",
|
|
19
23
|
"@formatjs/intl": "^2.10.1",
|
|
20
24
|
"@intrnl/xxhash64": "^0.1.2",
|
|
21
25
|
"intl-messageformat": "^10.5.11",
|
|
22
|
-
"@discord/intl-ast": "0.10.
|
|
26
|
+
"@discord/intl-ast": "0.10.2"
|
|
23
27
|
},
|
|
24
28
|
"devDependencies": {
|
|
25
29
|
"@swc/cli": "^0.3.12",
|
|
@@ -27,17 +31,7 @@
|
|
|
27
31
|
"typescript": "*"
|
|
28
32
|
},
|
|
29
33
|
"peerDependencies": {
|
|
30
|
-
"
|
|
31
|
-
"react": "> 16",
|
|
32
|
-
"webpack": "^5.0.0"
|
|
33
|
-
},
|
|
34
|
-
"peerDependenciesMeta": {
|
|
35
|
-
"@rspack/core": {
|
|
36
|
-
"optional": true
|
|
37
|
-
},
|
|
38
|
-
"webpack": {
|
|
39
|
-
"optional": true
|
|
40
|
-
}
|
|
34
|
+
"react": "> 16"
|
|
41
35
|
},
|
|
42
36
|
"scripts": {
|
|
43
37
|
"build": "tsc",
|
package/src/message-loader.ts
CHANGED
|
@@ -3,18 +3,13 @@ import { InternalIntlMessage } from './message';
|
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Type representing the serialized content of a translations file, which is a record of hashed
|
|
6
|
-
* message keys to their AST structure. This type represents both compressed, keyless AstNodes
|
|
7
|
-
*
|
|
8
|
-
*
|
|
6
|
+
* message keys to their AST structure. This type represents both compressed, keyless AstNodes and
|
|
7
|
+
* fully-typed, object FullFormatJsNodes as the message content, since either can be given depending
|
|
8
|
+
* on the configuration of the bundler/compiler.
|
|
9
9
|
*/
|
|
10
10
|
type MessagesData = Record<string, AstNode[] | FullFormatJsNode[]>;
|
|
11
|
-
export interface IntlMessageGetterAdditions {
|
|
12
|
-
onChange(callback: () => void): () => void;
|
|
13
|
-
}
|
|
14
11
|
|
|
15
|
-
export
|
|
16
|
-
(locale: LocaleId): InternalIntlMessage;
|
|
17
|
-
}
|
|
12
|
+
export type IntlMessageGetter = (this: MessageLoader, locale: LocaleId) => InternalIntlMessage;
|
|
18
13
|
|
|
19
14
|
export type LocaleId = string;
|
|
20
15
|
|
|
@@ -191,14 +186,18 @@ export class MessageLoader {
|
|
|
191
186
|
* for `get` with the that key as the first argument, allowing consumers to
|
|
192
187
|
* just call the function with a locale to retrieve the translated message
|
|
193
188
|
* for that key.
|
|
189
|
+
*
|
|
190
|
+
* This method is provided as a way to generate binds _at runtime_, but for
|
|
191
|
+
* very-large messages files (e.g., thousands of messages), this can be a
|
|
192
|
+
* non-negligible cost. Where feasible, consider generating these binds
|
|
193
|
+
* at build/bundle time, especially in cases where they can be substantially
|
|
194
|
+
* minified (i.e., Hermes bytecode). This is provided automatically as an
|
|
195
|
+
* option when using one of `@discord/intl`'s transformers.
|
|
194
196
|
*/
|
|
195
197
|
getBinds(): Record<string, IntlMessageGetter> {
|
|
196
|
-
const onChange = this.onChange.bind(this);
|
|
197
198
|
const result: Record<string, IntlMessageGetter> = {};
|
|
198
199
|
for (const key of this.messageKeys) {
|
|
199
|
-
|
|
200
|
-
bound.onChange = onChange;
|
|
201
|
-
result[key] = bound;
|
|
200
|
+
result[key] = this.get.bind(this, key);
|
|
202
201
|
}
|
|
203
202
|
|
|
204
203
|
return result;
|
package/src/types.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { InternalIntlMessage } from './message';
|
|
2
|
-
import type { IntlMessageGetterAdditions } from './message-loader';
|
|
3
2
|
import { FormatBuilderConstructor } from './format';
|
|
4
3
|
|
|
5
4
|
/**
|
|
@@ -42,7 +41,7 @@ export type IntlTime = string | number | Date | null | undefined;
|
|
|
42
41
|
export type PlainIntlMessage = string;
|
|
43
42
|
export type AnyIntlMessage = PlainIntlMessage | InternalIntlMessage;
|
|
44
43
|
|
|
45
|
-
export interface IntlMessageGetter
|
|
44
|
+
export interface IntlMessageGetter {
|
|
46
45
|
(locale: string): InternalIntlMessage | PlainIntlMessage;
|
|
47
46
|
}
|
|
48
47
|
|
|
@@ -52,8 +51,7 @@ export interface IntlMessageGetter extends IntlMessageGetterAdditions {
|
|
|
52
51
|
* value types that are required to format the message, or `{}` to represent
|
|
53
52
|
* that there are no values required and the message is a plain string.
|
|
54
53
|
*/
|
|
55
|
-
export interface TypedIntlMessageGetter<FormatValues extends object>
|
|
56
|
-
extends IntlMessageGetterAdditions {
|
|
54
|
+
export interface TypedIntlMessageGetter<FormatValues extends object> {
|
|
57
55
|
// TODO: This is lossy and unfortunate that typing can't be propagated
|
|
58
56
|
// to the returned message type, but doing so causes problems with
|
|
59
57
|
// contravariance of the type. When returning a message getter from a
|