@discord/intl 0.10.0 → 0.10.2

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.
@@ -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 as
6
- * well as fully-typed, object FullFormatJsNodes as the message content, since either can be given
7
- * depending on the configuration of the bundler/compiler.
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 interface IntlMessageGetterAdditions {
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>;
@@ -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
- const bound = (locale) => this.get(key, locale);
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 extends IntlMessageGetterAdditions {
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.0",
3
+ "version": "0.10.2",
4
4
  "license": "MIT",
5
5
  "description": "Client runtime for managing messages and translations in a React project.",
6
6
  "main": "./dist/index.js",
@@ -19,7 +19,7 @@
19
19
  "@formatjs/intl": "^2.10.1",
20
20
  "@intrnl/xxhash64": "^0.1.2",
21
21
  "intl-messageformat": "^10.5.11",
22
- "@discord/intl-ast": "0.10.0"
22
+ "@discord/intl-ast": "0.10.2"
23
23
  },
24
24
  "devDependencies": {
25
25
  "@swc/cli": "^0.3.12",
@@ -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 as
7
- * well as fully-typed, object FullFormatJsNodes as the message content, since either can be given
8
- * depending on the configuration of the bundler/compiler.
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 interface IntlMessageGetter extends IntlMessageGetterAdditions {
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
- const bound = (locale: LocaleId) => this.get(key, locale);
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 extends IntlMessageGetterAdditions {
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