@discord/intl 0.0.6-rc.0 → 0.0.6-rc.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.
@@ -1,6 +1,6 @@
1
1
  import { IntlShape } from '@formatjs/intl';
2
- import { LocaleImportMap, LocaleId, MessageLoader } from './message-loader';
3
- import type { TypedIntlMessageGetter, FormatValuesFor, RichTextElementMap } from './types';
2
+ import { LocaleId, LocaleImportMap, MessageLoader } from './message-loader';
3
+ import type { FormatValuesFor, RichTextElementMap, TypedIntlMessageGetter } from './types';
4
4
  /**
5
5
  * Fallback locale used for all internationalization when an operation in the
6
6
  * requested locale is not possible.
@@ -44,6 +44,16 @@ export declare class IntlManager<DefaultElements extends RichTextElementMap, Def
44
44
  */
45
45
  formatToParts<T extends TypedIntlMessageGetter<object | undefined>>(message: T): Array<string | any>;
46
46
  formatToParts<T extends TypedIntlMessageGetter<object | undefined>>(message: T, values: Omit<FormatValuesFor<T>, DefaultValues>): Array<string | any>;
47
+ /**
48
+ * For messages with no formatting values (i.e., plain strings, they can skip
49
+ * a majority of the processing work and just resolve the message itself for
50
+ * the current locale, then return the plain string directly.
51
+ *
52
+ * This is separate from `formatToPlainString`, which still allows for
53
+ * formatting variables and other elements but reduces the output to only
54
+ * include textual elements in a plain string.
55
+ */
56
+ string(message: TypedIntlMessageGetter<undefined>): string;
47
57
  /**
48
58
  * Format the given message with the provided values, removing any styling
49
59
  * and non-textual content from the message, returning a plain string.
@@ -115,6 +115,19 @@ var IntlManager = /** @class */ (function () {
115
115
  }
116
116
  return result;
117
117
  };
118
+ /**
119
+ * For messages with no formatting values (i.e., plain strings, they can skip
120
+ * a majority of the processing work and just resolve the message itself for
121
+ * the current locale, then return the plain string directly.
122
+ *
123
+ * This is separate from `formatToPlainString`, which still allows for
124
+ * formatting variables and other elements but reduces the output to only
125
+ * include textual elements in a plain string.
126
+ */
127
+ IntlManager.prototype.string = function (message) {
128
+ // TODO(faulty): Optimize this so that plain strings are hotpathed even faster.
129
+ return this.formatToPlainString(message);
130
+ };
118
131
  IntlManager.prototype.formatToPlainString = function (message, values) {
119
132
  if (typeof message === 'string')
120
133
  return message;
package/dist/message.js CHANGED
@@ -43,12 +43,6 @@ var InternalIntlMessage = /** @class */ (function () {
43
43
  function InternalIntlMessage(messageOrAst, locale) {
44
44
  this.locale = locale;
45
45
  if (typeof messageOrAst === 'string') {
46
- // TODO: This part should pre-parse our Markdown shorthand, too, so that
47
- // the AST that comes out of this is a standardized format, and we don't
48
- // have to do any special handling after calling `format`. That also
49
- // removes the need for handling "unsafe" values, since they'll be
50
- // parsed before values get interpolated. We can actually do that in the
51
- // loader, too, and avoid any runtime cost for it.
52
46
  this.message = messageOrAst;
53
47
  this.ast = (0, icu_messageformat_parser_1.parse)(messageOrAst);
54
48
  }
package/dist/react.d.ts CHANGED
@@ -10,7 +10,7 @@ export declare class IntlManagerReact<DefaultElements extends RichTextElementMap
10
10
  IntlMessage: <T extends TypedIntlMessageGetter<object | undefined>>(props: {
11
11
  message: T;
12
12
  values?: Omit<FormatValuesFor<T>, DefaultValues> | never;
13
- }) => React.FunctionComponentElement<{
13
+ }) => string | React.FunctionComponentElement<{
14
14
  children?: React.ReactNode | undefined;
15
15
  }>;
16
16
  /**
package/dist/react.js CHANGED
@@ -57,15 +57,18 @@ var IntlManagerReact = /** @class */ (function (_super) {
57
57
  // Use the locale from this point in the application, which may be
58
58
  // different from the global locale.
59
59
  var locale = React.useSyncExternalStore(_this.onLocaleChange, function () { return _this.currentLocale; });
60
- // Source the actual message to render for that locale from its loader.
61
60
  // TODO(faulty): This can and should be replaced by a
62
61
  // `use(messagesLoadedPromise)` once `use` is shipped to stable.
62
+ // Source the actual message to render for that locale from its loader.
63
63
  React.useSyncExternalStore(message.onChange, function () { return message(locale); });
64
64
  // If there are no object parts in the message, it has no formatting and can just be returned as
65
65
  // a plain string.
66
- return typeof message === 'string'
67
- ? message
68
- : React.createElement(React.Fragment, undefined, _this.formatToParts(message, values));
66
+ if (typeof message === 'string')
67
+ return message;
68
+ var parts = _this.formatToParts(message, values);
69
+ if (parts.length === 1 && typeof parts[0] === 'string')
70
+ return parts[0];
71
+ return React.createElement(React.Fragment, undefined, _this.formatToParts(message, values));
69
72
  };
70
73
  return _this;
71
74
  }
package/dist/types.d.ts CHANGED
@@ -36,7 +36,7 @@ type TypedIntlMessage<FormatValues extends object> = InternalIntlMessage & {
36
36
  };
37
37
 
38
38
  type AnyIntlMessage<FormatValues extends object | undefined = undefined> =
39
- FormatValues extends object ? TypedIntlMessage<FormatValues> : PlainIntlMessage;
39
+ FormatValues extends undefined ? PlainIntlMessage : TypedIntlMessage<FormatValues>;
40
40
 
41
41
  /**
42
42
  * Getter function that retrieves a message that best matches the requested
@@ -45,10 +45,10 @@ type AnyIntlMessage<FormatValues extends object | undefined = undefined> =
45
45
  * represent that there are no values required and the message is a plain
46
46
  * string.
47
47
  */
48
- export type TypedIntlMessageGetter<FormatValues extends object | undefined> =
49
- IntlMessageGetterAdditions & {
50
- (locale: string): AnyIntlMessage<FormatValues>;
51
- };
48
+ export interface TypedIntlMessageGetter<FormatValues extends object | undefined>
49
+ extends IntlMessageGetterAdditions {
50
+ (locale: string): AnyIntlMessage<FormatValues>;
51
+ }
52
52
 
53
53
  /**
54
54
  * Common utility type for converting a union type to an intersection of all
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@discord/intl",
3
- "version": "0.0.6-rc.0",
3
+ "version": "0.0.6-rc.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",
@@ -38,6 +38,7 @@
38
38
  }
39
39
  },
40
40
  "scripts": {
41
- "build": "tsc && cp -r ./src/*.d.ts dist/"
41
+ "build": "tsc",
42
+ "build:release": "tsc && cp ./src/*.d.ts dist/"
42
43
  }
43
44
  }
@@ -1,14 +1,14 @@
1
- import { IntlShape, createIntl } from '@formatjs/intl';
1
+ import { createIntl, IntlShape } from '@formatjs/intl';
2
2
  import {
3
- PART_TYPE as FormatPartType,
4
3
  Formats,
5
4
  Formatters,
6
5
  IntlMessageFormat,
6
+ PART_TYPE as FormatPartType,
7
7
  } from 'intl-messageformat';
8
8
 
9
- import { LocaleImportMap, LocaleId, MessageLoader } from './message-loader';
9
+ import { LocaleId, LocaleImportMap, MessageLoader } from './message-loader';
10
10
 
11
- import type { TypedIntlMessageGetter, FormatValuesFor, RichTextElementMap } from './types';
11
+ import type { FormatValuesFor, RichTextElementMap, TypedIntlMessageGetter } from './types';
12
12
 
13
13
  /**
14
14
  * Fallback locale used for all internationalization when an operation in the
@@ -129,6 +129,20 @@ export class IntlManager<
129
129
  return result;
130
130
  }
131
131
 
132
+ /**
133
+ * For messages with no formatting values (i.e., plain strings, they can skip
134
+ * a majority of the processing work and just resolve the message itself for
135
+ * the current locale, then return the plain string directly.
136
+ *
137
+ * This is separate from `formatToPlainString`, which still allows for
138
+ * formatting variables and other elements but reduces the output to only
139
+ * include textual elements in a plain string.
140
+ */
141
+ string(message: TypedIntlMessageGetter<undefined>): string {
142
+ // TODO(faulty): Optimize this so that plain strings are hotpathed even faster.
143
+ return this.formatToPlainString(message);
144
+ }
145
+
132
146
  /**
133
147
  * Format the given message with the provided values, removing any styling
134
148
  * and non-textual content from the message, returning a plain string.
package/src/message.ts CHANGED
@@ -26,13 +26,6 @@ export class InternalIntlMessage {
26
26
  this.locale = locale;
27
27
 
28
28
  if (typeof messageOrAst === 'string') {
29
- // TODO: This part should pre-parse our Markdown shorthand, too, so that
30
- // the AST that comes out of this is a standardized format, and we don't
31
- // have to do any special handling after calling `format`. That also
32
- // removes the need for handling "unsafe" values, since they'll be
33
- // parsed before values get interpolated. We can actually do that in the
34
- // loader, too, and avoid any runtime cost for it.
35
-
36
29
  this.message = messageOrAst;
37
30
  this.ast = parseMessageFormat(messageOrAst);
38
31
  } else {
package/src/react.ts CHANGED
@@ -21,19 +21,21 @@ export class IntlManagerReact<
21
21
  // Use the locale from this point in the application, which may be
22
22
  // different from the global locale.
23
23
  const locale = React.useSyncExternalStore(this.onLocaleChange, () => this.currentLocale);
24
- // Source the actual message to render for that locale from its loader.
25
24
  // TODO(faulty): This can and should be replaced by a
26
25
  // `use(messagesLoadedPromise)` once `use` is shipped to stable.
26
+ // Source the actual message to render for that locale from its loader.
27
27
  React.useSyncExternalStore(message.onChange, () => message(locale));
28
28
  // If there are no object parts in the message, it has no formatting and can just be returned as
29
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
- );
30
+ if (typeof message === 'string') return message;
31
+
32
+ const parts = this.formatToParts(message, values as Omit<FormatValuesFor<T>, DefaultValues>);
33
+ if (parts.length === 1 && typeof parts[0] === 'string') return parts[0];
34
+ return React.createElement(
35
+ React.Fragment,
36
+ undefined,
37
+ this.formatToParts(message, values as Omit<FormatValuesFor<T>, DefaultValues>),
38
+ );
37
39
  };
38
40
 
39
41
  /**
package/src/types.d.ts CHANGED
@@ -36,7 +36,7 @@ type TypedIntlMessage<FormatValues extends object> = InternalIntlMessage & {
36
36
  };
37
37
 
38
38
  type AnyIntlMessage<FormatValues extends object | undefined = undefined> =
39
- FormatValues extends object ? TypedIntlMessage<FormatValues> : PlainIntlMessage;
39
+ FormatValues extends undefined ? PlainIntlMessage : TypedIntlMessage<FormatValues>;
40
40
 
41
41
  /**
42
42
  * Getter function that retrieves a message that best matches the requested
@@ -45,10 +45,10 @@ type AnyIntlMessage<FormatValues extends object | undefined = undefined> =
45
45
  * represent that there are no values required and the message is a plain
46
46
  * string.
47
47
  */
48
- export type TypedIntlMessageGetter<FormatValues extends object | undefined> =
49
- IntlMessageGetterAdditions & {
50
- (locale: string): AnyIntlMessage<FormatValues>;
51
- };
48
+ export interface TypedIntlMessageGetter<FormatValues extends object | undefined>
49
+ extends IntlMessageGetterAdditions {
50
+ (locale: string): AnyIntlMessage<FormatValues>;
51
+ }
52
52
 
53
53
  /**
54
54
  * Common utility type for converting a union type to an intersection of all