@discord/intl 0.0.2-rc.1 → 0.0.3-canary.dae5431

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.
@@ -3,7 +3,6 @@ import type { MessageFormatElement } from '@formatjs/icu-messageformat-parser';
3
3
  type AnyRawMessage = string | MessageFormatElement[];
4
4
  type MessagesData = Record<string, AnyRawMessage>;
5
5
  export interface IntlMessageGetterAdditions {
6
- isLocaleLoaded(locale: LocaleId): boolean;
7
6
  onChange(callback: () => void): () => void;
8
7
  }
9
8
  export interface IntlMessageGetter extends IntlMessageGetterAdditions {
package/dist/react.d.ts CHANGED
@@ -9,7 +9,7 @@ export declare class IntlManagerReact<DefaultElements extends RichTextElementMap
9
9
  */
10
10
  IntlMessage: <T extends TypedIntlMessageGetter<object | undefined>>(props: {
11
11
  message: T;
12
- values?: Omit<FormatValuesFor<T>, DefaultValues>;
12
+ values?: Omit<FormatValuesFor<T>, DefaultValues> | never;
13
13
  }) => React.FunctionComponentElement<{
14
14
  children?: React.ReactNode | undefined;
15
15
  }>;
@@ -18,7 +18,6 @@ export declare class IntlManagerReact<DefaultElements extends RichTextElementMap
18
18
  * and respond to updates about the current locale and other relevant
19
19
  * information.
20
20
  */
21
- format<T extends TypedIntlMessageGetter<object | undefined>>(message: T): JSX.Element;
22
- format<T extends TypedIntlMessageGetter<object | undefined>>(message: T, values: Omit<FormatValuesFor<T>, DefaultValues>): JSX.Element;
23
- format(message: any): JSX.Element;
21
+ format<T extends TypedIntlMessageGetter<object | undefined>>(message: T): React.ReactElement;
22
+ format<T extends TypedIntlMessageGetter<object | undefined>>(message: T, values: Omit<FormatValuesFor<T>, DefaultValues>): React.ReactElement;
24
23
  }
package/dist/react.js CHANGED
@@ -61,19 +61,16 @@ var IntlManagerReact = /** @class */ (function (_super) {
61
61
  // TODO(faulty): This can and should be replaced by a
62
62
  // `use(messagesLoadedPromise)` once `use` is shipped to stable.
63
63
  React.useSyncExternalStore(message.onChange, function () { return message(locale); });
64
- // The return the formatted version of that string.
65
- if (typeof message === 'string')
66
- return message;
67
- return React.createElement(React.Fragment, undefined, _this.formatToParts(message, values));
64
+ // If there are no object parts in the message, it has no formatting and can just be returned as
65
+ // a plain string.
66
+ return typeof message === 'string'
67
+ ? message
68
+ : React.createElement(React.Fragment, undefined, _this.formatToParts(message, values));
68
69
  };
69
70
  return _this;
70
71
  }
71
72
  IntlManagerReact.prototype.format = function (message, values) {
72
- // A string literal means there's no locale dependency, so it can just be
73
- // formatted directly without any subscription.
74
- if (typeof message === 'string')
75
- return this.formatToParts(message);
76
- return React.createElement(this.IntlMessage, { message: message, values: values });
73
+ return React.createElement((this.IntlMessage), { message: message, values: values });
77
74
  };
78
75
  return IntlManagerReact;
79
76
  }(intl_manager_1.IntlManager));
package/dist/types.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { InternalIntlMessage } from './message';
2
2
  import type { IntlMessageGetterAdditions } from './message-loader';
3
+
3
4
  /**
4
5
  * Use this function to define messages as part of the `@discord/intl`
5
6
  * system, with type checking support.
@@ -60,7 +61,7 @@ type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
60
61
  /**
61
62
  * Given a message getter of type `T` (which may be a union of multiple message
62
63
  * types), return a combined type representing the intersection of all values
63
- * needed to satisfy the mix of values.
64
+ * needed to satisfy the mix of values defined in any of the messages.
64
65
  *
65
66
  * For example, given a variable representing one of two message getters, where
66
67
  * one requires a single value to be formatted, but the other requires an
@@ -123,7 +124,7 @@ type FormatValuesFor<T> =
123
124
  // formatter to optimize a little more on each call.
124
125
  [T] extends [TypedIntlMessageGetter<undefined>]
125
126
  ? never
126
- : // The `never` conditino needs to be repeated here, but this time _without_
127
+ : // The `never` condition needs to be repeated here, but this time _without_
127
128
  // the `[]` syntax, so that only the union elements with actual values are
128
129
  // included in the union. Without this, the `infer U` on the latter side
129
130
  // fails to resolve and the type just becomes `never`.
@@ -134,25 +135,3 @@ type FormatValuesFor<T> =
134
135
  ? U
135
136
  : never
136
137
  >;
137
-
138
- /**
139
- * A generic definition of a `format*` function that accepts any message
140
- * argument as the first parameter, and the required values object for that
141
- * message as the second parameter. The second parameter
142
- *
143
- * An optional third parameter for additional options can be specified as the
144
- * second generic type parameter.
145
- */
146
- export interface IntlFormatFn<
147
- T extends object | undefined,
148
- DefaultValues extends string | number | symbol,
149
- OptionsType,
150
- ResultType,
151
- > {
152
- (message: TypedIntlMessageGetter<T>, options?: OptionsType): ResultType;
153
- (
154
- message: TypedIntlMessageGetter<T>,
155
- values: Omit<FormatValuesFor<T>, DefaultValues>,
156
- options?: OptionsType,
157
- ): ResultType;
158
- }
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@discord/intl",
3
- "version": "0.0.2-rc.1",
3
+ "version": "0.0.3-canary.dae5431",
4
4
  "license": "MIT",
5
5
  "description": "Client runtime for managing messages and translations in a React project.",
6
- "main": "./src/index.ts",
6
+ "main": "./dist/index.js",
7
7
  "exports": {
8
8
  ".": {
9
9
  "default": "./dist/index.js",
@@ -94,7 +94,7 @@ export class IntlManager<
94
94
  formatToParts<T extends TypedIntlMessageGetter<object | undefined>>(
95
95
  message: T,
96
96
  values?: Omit<FormatValuesFor<T>, DefaultValues>,
97
- ) {
97
+ ): Array<string | any> {
98
98
  if (typeof message === 'string') return [message];
99
99
  const resolvedMessage = typeof message === 'function' ? message(this.currentLocale) : message;
100
100
  if (typeof resolvedMessage === 'string') return [resolvedMessage];
@@ -5,7 +5,6 @@ import type { MessageFormatElement } from '@formatjs/icu-messageformat-parser';
5
5
  type AnyRawMessage = string | MessageFormatElement[];
6
6
  type MessagesData = Record<string, AnyRawMessage>;
7
7
  export interface IntlMessageGetterAdditions {
8
- isLocaleLoaded(locale: LocaleId): boolean;
9
8
  onChange(callback: () => void): () => void;
10
9
  }
11
10
 
package/src/react.ts CHANGED
@@ -15,7 +15,7 @@ export class IntlManagerReact<
15
15
  */
16
16
  IntlMessage = <T extends TypedIntlMessageGetter<object | undefined>>(props: {
17
17
  message: T;
18
- values?: Omit<FormatValuesFor<T>, DefaultValues>;
18
+ values?: Omit<FormatValuesFor<T>, DefaultValues> | never;
19
19
  }) => {
20
20
  const { message, values } = props;
21
21
  // Use the locale from this point in the application, which may be
@@ -25,14 +25,15 @@ export class IntlManagerReact<
25
25
  // TODO(faulty): This can and should be replaced by a
26
26
  // `use(messagesLoadedPromise)` once `use` is shipped to stable.
27
27
  React.useSyncExternalStore(message.onChange, () => message(locale));
28
- // The return the formatted version of that string.
29
- if (typeof message === 'string') return message;
30
-
31
- return React.createElement(
32
- React.Fragment,
33
- undefined,
34
- this.formatToParts(message, values as Omit<FormatValuesFor<T>, DefaultValues> | never),
35
- );
28
+ // If there are no object parts in the message, it has no formatting and can just be returned as
29
+ // a plain string.
30
+ return typeof message === 'string'
31
+ ? message
32
+ : React.createElement(
33
+ React.Fragment,
34
+ undefined,
35
+ this.formatToParts(message, values as Omit<FormatValuesFor<T>, DefaultValues>),
36
+ );
36
37
  };
37
38
 
38
39
  /**
@@ -40,20 +41,15 @@ export class IntlManagerReact<
40
41
  * and respond to updates about the current locale and other relevant
41
42
  * information.
42
43
  */
43
- format<T extends TypedIntlMessageGetter<object | undefined>>(message: T): JSX.Element;
44
+ format<T extends TypedIntlMessageGetter<object | undefined>>(message: T): React.ReactElement;
44
45
  format<T extends TypedIntlMessageGetter<object | undefined>>(
45
46
  message: T,
46
47
  values: Omit<FormatValuesFor<T>, DefaultValues>,
47
- ): JSX.Element;
48
- format(message: any): JSX.Element;
48
+ ): React.ReactElement;
49
49
  format<T extends TypedIntlMessageGetter<object | undefined>>(
50
50
  message: T,
51
51
  values?: Omit<FormatValuesFor<T>, DefaultValues>,
52
- ) {
53
- // A string literal means there's no locale dependency, so it can just be
54
- // formatted directly without any subscription.
55
- if (typeof message === 'string') return this.formatToParts(message);
56
-
57
- return React.createElement(this.IntlMessage, { message, values });
52
+ ): React.ReactElement {
53
+ return React.createElement(this.IntlMessage<T>, { message, values });
58
54
  }
59
55
  }
package/src/types.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { InternalIntlMessage } from './message';
2
2
  import type { IntlMessageGetterAdditions } from './message-loader';
3
+
3
4
  /**
4
5
  * Use this function to define messages as part of the `@discord/intl`
5
6
  * system, with type checking support.
@@ -60,7 +61,7 @@ type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
60
61
  /**
61
62
  * Given a message getter of type `T` (which may be a union of multiple message
62
63
  * types), return a combined type representing the intersection of all values
63
- * needed to satisfy the mix of values.
64
+ * needed to satisfy the mix of values defined in any of the messages.
64
65
  *
65
66
  * For example, given a variable representing one of two message getters, where
66
67
  * one requires a single value to be formatted, but the other requires an
@@ -123,7 +124,7 @@ type FormatValuesFor<T> =
123
124
  // formatter to optimize a little more on each call.
124
125
  [T] extends [TypedIntlMessageGetter<undefined>]
125
126
  ? never
126
- : // The `never` conditino needs to be repeated here, but this time _without_
127
+ : // The `never` condition needs to be repeated here, but this time _without_
127
128
  // the `[]` syntax, so that only the union elements with actual values are
128
129
  // included in the union. Without this, the `infer U` on the latter side
129
130
  // fails to resolve and the type just becomes `never`.
@@ -134,25 +135,3 @@ type FormatValuesFor<T> =
134
135
  ? U
135
136
  : never
136
137
  >;
137
-
138
- /**
139
- * A generic definition of a `format*` function that accepts any message
140
- * argument as the first parameter, and the required values object for that
141
- * message as the second parameter. The second parameter
142
- *
143
- * An optional third parameter for additional options can be specified as the
144
- * second generic type parameter.
145
- */
146
- export interface IntlFormatFn<
147
- T extends object | undefined,
148
- DefaultValues extends string | number | symbol,
149
- OptionsType,
150
- ResultType,
151
- > {
152
- (message: TypedIntlMessageGetter<T>, options?: OptionsType): ResultType;
153
- (
154
- message: TypedIntlMessageGetter<T>,
155
- values: Omit<FormatValuesFor<T>, DefaultValues>,
156
- options?: OptionsType,
157
- ): ResultType;
158
- }