@discord/intl 0.0.6-rc.0 → 0.0.6-rc.1
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.js +0 -6
- package/package.json +2 -2
- package/src/message.ts +0 -7
- package/dist/types.d.ts +0 -137
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@discord/intl",
|
|
3
|
-
"version": "0.0.6-rc.
|
|
3
|
+
"version": "0.0.6-rc.1",
|
|
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,6 @@
|
|
|
38
38
|
}
|
|
39
39
|
},
|
|
40
40
|
"scripts": {
|
|
41
|
-
"build": "tsc
|
|
41
|
+
"build": "tsc"
|
|
42
42
|
}
|
|
43
43
|
}
|
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/dist/types.d.ts
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import type { InternalIntlMessage } from './message';
|
|
2
|
-
import type { IntlMessageGetterAdditions } from './message-loader';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Use this function to define messages as part of the `@discord/intl`
|
|
6
|
-
* system, with type checking support.
|
|
7
|
-
*
|
|
8
|
-
* This function does not actually exist, and gets compiled out as part
|
|
9
|
-
* of the bundling process both on web and mobile. See the
|
|
10
|
-
* `@discord/intl/README.md` for more information.
|
|
11
|
-
*/
|
|
12
|
-
export declare function defineMessages(messages: object): object;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* A map of tag names to render functions to apply when the tags are
|
|
16
|
-
* encountered while formatting a message. Each `IntlManager` can be
|
|
17
|
-
* constructed with a different map of these elements, for example to allow
|
|
18
|
-
* one manager to render React output while another renders accessibility text.
|
|
19
|
-
*/
|
|
20
|
-
export type RichTextElementMap = Record<string, (chunks: any[]) => any>;
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Messages with no rich formatting and no value interpolations are just plain
|
|
24
|
-
* strings, and are able to skip multiple formatting steps to work more
|
|
25
|
-
* efficiently. This type represents those messages distinctly.
|
|
26
|
-
*/
|
|
27
|
-
type PlainIntlMessage = string;
|
|
28
|
-
/**
|
|
29
|
-
* A richly-formatted message that injects dynamic values while formatting,
|
|
30
|
-
* with the types of those values defined by the `FormatValues` object. All
|
|
31
|
-
* messages of this type must have at least one value in the object, otherwise
|
|
32
|
-
* the message should be typed as a `PlainIntlMessage`.
|
|
33
|
-
*/
|
|
34
|
-
type TypedIntlMessage<FormatValues extends object> = InternalIntlMessage & {
|
|
35
|
-
values: FormatValues;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
type AnyIntlMessage<FormatValues extends object | undefined = undefined> =
|
|
39
|
-
FormatValues extends object ? TypedIntlMessage<FormatValues> : PlainIntlMessage;
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Getter function that retrieves a message that best matches the requested
|
|
43
|
-
* locale. `FormatValues` represents either an object containing all of the
|
|
44
|
-
* value types that are required to format the message, or `undefined` to
|
|
45
|
-
* represent that there are no values required and the message is a plain
|
|
46
|
-
* string.
|
|
47
|
-
*/
|
|
48
|
-
export type TypedIntlMessageGetter<FormatValues extends object | undefined> =
|
|
49
|
-
IntlMessageGetterAdditions & {
|
|
50
|
-
(locale: string): AnyIntlMessage<FormatValues>;
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Common utility type for converting a union type to an intersection of all
|
|
55
|
-
* the types in that union.
|
|
56
|
-
*/
|
|
57
|
-
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void
|
|
58
|
-
? I
|
|
59
|
-
: never;
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Given a message getter of type `T` (which may be a union of multiple message
|
|
63
|
-
* types), return a combined type representing the intersection of all values
|
|
64
|
-
* needed to satisfy the mix of values defined in any of the messages.
|
|
65
|
-
*
|
|
66
|
-
* For example, given a variable representing one of two message getters, where
|
|
67
|
-
* one requires a single value to be formatted, but the other requires an
|
|
68
|
-
* additional value on top of that one:
|
|
69
|
-
*
|
|
70
|
-
* ```typescript
|
|
71
|
-
* // message1: "Hello {username}"
|
|
72
|
-
* // message2: "Hello {username}, it's {currentDate, date}"
|
|
73
|
-
* const message = someCondition ? message1 || message2;
|
|
74
|
-
* ```
|
|
75
|
-
*
|
|
76
|
-
* Attempting to format this message would require that both `username` _and_
|
|
77
|
-
* `currentDate` are supplied, so that all possible cases are covered, which is
|
|
78
|
-
* what this type resolves:
|
|
79
|
-
*
|
|
80
|
-
* ```typescript
|
|
81
|
-
* type ResolvedValuesTypes = FormatValuesFor<typeof message>;
|
|
82
|
-
* // => {username: string, currentDate: Date}
|
|
83
|
-
* ```
|
|
84
|
-
*
|
|
85
|
-
* This values type can then be used to enforce that all values are passed to
|
|
86
|
-
* functions that format the message, like `i18n.format`:
|
|
87
|
-
*
|
|
88
|
-
* ```typescript
|
|
89
|
-
* declare function format<T>(message: T, values: FormatValuesFor<T>): string;
|
|
90
|
-
* ```
|
|
91
|
-
*
|
|
92
|
-
* ## Mixed message types
|
|
93
|
-
*
|
|
94
|
-
* If T is a union of message types including both rich messages requiring
|
|
95
|
-
* values _and_ plain string messages with no values, this type will still
|
|
96
|
-
* properly resolve all of the required values, assuming that the getters for
|
|
97
|
-
* the plain messages use the convention of passing `undefined` as the generic
|
|
98
|
-
* argument, representing "no values should be passed for this message".
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
* ## Why this type?
|
|
102
|
-
*
|
|
103
|
-
* All "messages" are really defined as functions that return a specific
|
|
104
|
-
* value of the message for a requested locale, `(locale: string) => Message`.
|
|
105
|
-
* This is named `TypedIntlMessageGetter<T>`, where `T` is the definition of
|
|
106
|
-
* the values required to format the message.
|
|
107
|
-
*
|
|
108
|
-
* This type requires being passed the entire getter function rather than the
|
|
109
|
-
* returned message types, as narrowing to the returned type prevents
|
|
110
|
-
* TypeScript from distributing the union properly. This surfaces as error
|
|
111
|
-
* messages showing up on the _message_ type rather than the _value_ type when
|
|
112
|
-
* they don't match, which doesn't give the user any helpful information about
|
|
113
|
-
* what's wrong. This method ensures the value type is always the one being
|
|
114
|
-
* checked against the provided message types, and error messages will be
|
|
115
|
-
* accurate as a result.
|
|
116
|
-
*/
|
|
117
|
-
type FormatValuesFor<T> =
|
|
118
|
-
// `[]` syntax forces TypeScript to not distribute the possible union of
|
|
119
|
-
// types, meaning this represents "_all_ values of T are an extension of
|
|
120
|
-
// the parent type", which in this case represents messages with no
|
|
121
|
-
// format values. If that condition is matched, then `FormatValues` _cannot_
|
|
122
|
-
// be supplied, represented by the `never`. This prevents accidentally
|
|
123
|
-
// passing an object, even an empty one, for those strings, and allows the
|
|
124
|
-
// formatter to optimize a little more on each call.
|
|
125
|
-
[T] extends [TypedIntlMessageGetter<undefined>]
|
|
126
|
-
? never
|
|
127
|
-
: // The `never` condition needs to be repeated here, but this time _without_
|
|
128
|
-
// the `[]` syntax, so that only the union elements with actual values are
|
|
129
|
-
// included in the union. Without this, the `infer U` on the latter side
|
|
130
|
-
// fails to resolve and the type just becomes `never`.
|
|
131
|
-
UnionToIntersection<
|
|
132
|
-
T extends TypedIntlMessageGetter<undefined>
|
|
133
|
-
? never
|
|
134
|
-
: T extends TypedIntlMessageGetter<infer U>
|
|
135
|
-
? U
|
|
136
|
-
: never
|
|
137
|
-
>;
|