@discord/intl 0.7.1 → 0.8.0-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/formatters/react.js +7 -1
- package/dist/intl-manager.d.ts +1 -1
- package/dist/intl-manager.js +8 -10
- package/dist/types.d.ts +8 -13
- package/package.json +2 -2
- package/src/formatters/react.ts +6 -1
- package/src/intl-manager.ts +11 -14
- package/src/types.d.ts +8 -13
package/dist/formatters/react.js
CHANGED
|
@@ -34,7 +34,13 @@ function createReactBuilder(richTextElements) {
|
|
|
34
34
|
this.result.push(richTextElements[tag](children, `${this._nodeKey++}`));
|
|
35
35
|
}
|
|
36
36
|
pushLiteralText(text) {
|
|
37
|
-
this.result.
|
|
37
|
+
const last = this.result[this.result.length - 1];
|
|
38
|
+
if (typeof last === 'string') {
|
|
39
|
+
this.result[this.result.length - 1] += text;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
this.result.push(text);
|
|
43
|
+
}
|
|
38
44
|
}
|
|
39
45
|
pushObject(value) {
|
|
40
46
|
// @ts-expect-error this is technically invalid, but we'll just assume that if a format returns
|
package/dist/intl-manager.d.ts
CHANGED
|
@@ -46,7 +46,7 @@ export declare class IntlManager {
|
|
|
46
46
|
* For static messages with no rich text and no dynamic placeholders, use this method to
|
|
47
47
|
* immediately return the plain string value of the message in the current locale.
|
|
48
48
|
*/
|
|
49
|
-
string<T extends TypedIntlMessageGetter<
|
|
49
|
+
string<T extends TypedIntlMessageGetter<{}>>(message: T): string;
|
|
50
50
|
/**
|
|
51
51
|
* Return a raw string representing the syntax of the original message, as
|
|
52
52
|
* authored, with no values replaced. The result of this function could be
|
package/dist/intl-manager.js
CHANGED
|
@@ -71,17 +71,15 @@ class IntlManager {
|
|
|
71
71
|
const resolved = message(this.currentLocale);
|
|
72
72
|
if (resolved == null || resolved.ast.length === 0)
|
|
73
73
|
return '';
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
74
|
+
let result = '';
|
|
75
|
+
for (const element of resolved.ast) {
|
|
76
|
+
if (typeof element !== 'string') {
|
|
77
|
+
throw new Error('Attempted to call `string` formatting on a non-literal message: ' +
|
|
78
|
+
JSON.stringify(resolved));
|
|
79
|
+
}
|
|
80
|
+
result += element;
|
|
78
81
|
}
|
|
79
|
-
|
|
80
|
-
// the generic typing being sound enough to know that the message can only
|
|
81
|
-
// contain a single static text node and no placeholders or rich text.
|
|
82
|
-
// As it stands, this is _technically_ typed as string | undefined, even though
|
|
83
|
-
// TypeScript doesn't actually complain about it.
|
|
84
|
-
return value;
|
|
82
|
+
return result;
|
|
85
83
|
}
|
|
86
84
|
/**
|
|
87
85
|
* Return a raw string representing the syntax of the original message, as
|
package/dist/types.d.ts
CHANGED
|
@@ -49,11 +49,10 @@ export interface IntlMessageGetter extends IntlMessageGetterAdditions {
|
|
|
49
49
|
/**
|
|
50
50
|
* Getter function that retrieves a message that best matches the requested
|
|
51
51
|
* locale. `FormatValues` represents either an object containing all of the
|
|
52
|
-
* value types that are required to format the message, or `
|
|
53
|
-
*
|
|
54
|
-
* string.
|
|
52
|
+
* value types that are required to format the message, or `{}` to represent
|
|
53
|
+
* that there are no values required and the message is a plain string.
|
|
55
54
|
*/
|
|
56
|
-
export interface TypedIntlMessageGetter<FormatValues extends object
|
|
55
|
+
export interface TypedIntlMessageGetter<FormatValues extends object>
|
|
57
56
|
extends IntlMessageGetterAdditions {
|
|
58
57
|
// TODO: This is lossy and unfortunate that typing can't be propagated
|
|
59
58
|
// to the returned message type, but doing so causes problems with
|
|
@@ -94,11 +93,7 @@ export interface TypedIntlMessageGetter<FormatValues extends object | undefined>
|
|
|
94
93
|
* downstream will ensure that all format values are always checked for all
|
|
95
94
|
* possible cases of the message.
|
|
96
95
|
*/
|
|
97
|
-
__phantom: object extends FormatValues
|
|
98
|
-
? any
|
|
99
|
-
: FormatValues extends undefined
|
|
100
|
-
? any
|
|
101
|
-
: keyof FormatValues;
|
|
96
|
+
__phantom: object extends FormatValues ? any : {} extends FormatValues ? any : keyof FormatValues;
|
|
102
97
|
}
|
|
103
98
|
|
|
104
99
|
/**
|
|
@@ -145,7 +140,7 @@ type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
|
|
|
145
140
|
* If T is a union of message types including both rich messages requiring
|
|
146
141
|
* values _and_ plain string messages with no values, this type will still
|
|
147
142
|
* properly resolve all of the required values, assuming that the getters for
|
|
148
|
-
* the plain messages use the convention of passing `
|
|
143
|
+
* the plain messages use the convention of passing `{}` as the generic
|
|
149
144
|
* argument, representing "no values should be passed for this message".
|
|
150
145
|
*
|
|
151
146
|
*
|
|
@@ -173,13 +168,13 @@ export type FormatValuesFor<T> =
|
|
|
173
168
|
// be supplied, represented by the `never`. This prevents accidentally
|
|
174
169
|
// passing an object, even an empty one, for those strings, and allows the
|
|
175
170
|
// formatter to optimize a little more on each call.
|
|
176
|
-
[
|
|
171
|
+
[{}] extends [T]
|
|
177
172
|
? never
|
|
178
173
|
: // The `never` condition needs to be repeated here, but this time _without_
|
|
179
174
|
// the `[]` syntax, so that only the union elements with actual values are
|
|
180
175
|
// included in the union. Without this, the `infer U` on the latter side
|
|
181
176
|
// fails to resolve and the type just becomes `never`.
|
|
182
|
-
UnionToIntersection<
|
|
177
|
+
UnionToIntersection<{} extends T ? never : T>;
|
|
183
178
|
|
|
184
179
|
/**
|
|
185
180
|
* A template type for replaceable placeholder types to be defined by formatter
|
|
@@ -254,7 +249,7 @@ export type RequiredFormatValues<
|
|
|
254
249
|
Values extends IntlMessageGetter,
|
|
255
250
|
FunctionTypes extends FunctionTypeMap,
|
|
256
251
|
> =
|
|
257
|
-
IntlMessageGetterInnerType<Values>
|
|
252
|
+
{} extends IntlMessageGetterInnerType<Values>
|
|
258
253
|
? {}
|
|
259
254
|
: CheckStrictAny<
|
|
260
255
|
IntlMessageGetterInnerType<Values>,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@discord/intl",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0-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",
|
|
@@ -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.
|
|
22
|
+
"@discord/intl-ast": "0.8.0-rc.1"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
25
|
"@swc/cli": "^0.3.12",
|
package/src/formatters/react.ts
CHANGED
|
@@ -56,7 +56,12 @@ function createReactBuilder(richTextElements: RichTextFormattingMap<ReactFunctio
|
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
pushLiteralText(text: string) {
|
|
59
|
-
this.result.
|
|
59
|
+
const last = this.result[this.result.length - 1];
|
|
60
|
+
if (typeof last === 'string') {
|
|
61
|
+
this.result[this.result.length - 1] += text;
|
|
62
|
+
} else {
|
|
63
|
+
this.result.push(text);
|
|
64
|
+
}
|
|
60
65
|
}
|
|
61
66
|
|
|
62
67
|
pushObject(value: object) {
|
package/src/intl-manager.ts
CHANGED
|
@@ -116,24 +116,21 @@ export class IntlManager {
|
|
|
116
116
|
* For static messages with no rich text and no dynamic placeholders, use this method to
|
|
117
117
|
* immediately return the plain string value of the message in the current locale.
|
|
118
118
|
*/
|
|
119
|
-
string<T extends TypedIntlMessageGetter<
|
|
119
|
+
string<T extends TypedIntlMessageGetter<{}>>(message: T): string {
|
|
120
120
|
const resolved = message(this.currentLocale);
|
|
121
121
|
if (resolved == null || resolved.ast.length === 0) return '';
|
|
122
122
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
123
|
+
let result = '';
|
|
124
|
+
for (const element of resolved.ast) {
|
|
125
|
+
if (typeof element !== 'string') {
|
|
126
|
+
throw new Error(
|
|
127
|
+
'Attempted to call `string` formatting on a non-literal message: ' +
|
|
128
|
+
JSON.stringify(resolved),
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
result += element;
|
|
129
132
|
}
|
|
130
|
-
|
|
131
|
-
// TODO: Figure out how to make this typing exact. This currently relies on
|
|
132
|
-
// the generic typing being sound enough to know that the message can only
|
|
133
|
-
// contain a single static text node and no placeholders or rich text.
|
|
134
|
-
// As it stands, this is _technically_ typed as string | undefined, even though
|
|
135
|
-
// TypeScript doesn't actually complain about it.
|
|
136
|
-
return value;
|
|
133
|
+
return result;
|
|
137
134
|
}
|
|
138
135
|
|
|
139
136
|
/**
|
package/src/types.d.ts
CHANGED
|
@@ -49,11 +49,10 @@ export interface IntlMessageGetter extends IntlMessageGetterAdditions {
|
|
|
49
49
|
/**
|
|
50
50
|
* Getter function that retrieves a message that best matches the requested
|
|
51
51
|
* locale. `FormatValues` represents either an object containing all of the
|
|
52
|
-
* value types that are required to format the message, or `
|
|
53
|
-
*
|
|
54
|
-
* string.
|
|
52
|
+
* value types that are required to format the message, or `{}` to represent
|
|
53
|
+
* that there are no values required and the message is a plain string.
|
|
55
54
|
*/
|
|
56
|
-
export interface TypedIntlMessageGetter<FormatValues extends object
|
|
55
|
+
export interface TypedIntlMessageGetter<FormatValues extends object>
|
|
57
56
|
extends IntlMessageGetterAdditions {
|
|
58
57
|
// TODO: This is lossy and unfortunate that typing can't be propagated
|
|
59
58
|
// to the returned message type, but doing so causes problems with
|
|
@@ -94,11 +93,7 @@ export interface TypedIntlMessageGetter<FormatValues extends object | undefined>
|
|
|
94
93
|
* downstream will ensure that all format values are always checked for all
|
|
95
94
|
* possible cases of the message.
|
|
96
95
|
*/
|
|
97
|
-
__phantom: object extends FormatValues
|
|
98
|
-
? any
|
|
99
|
-
: FormatValues extends undefined
|
|
100
|
-
? any
|
|
101
|
-
: keyof FormatValues;
|
|
96
|
+
__phantom: object extends FormatValues ? any : {} extends FormatValues ? any : keyof FormatValues;
|
|
102
97
|
}
|
|
103
98
|
|
|
104
99
|
/**
|
|
@@ -145,7 +140,7 @@ type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
|
|
|
145
140
|
* If T is a union of message types including both rich messages requiring
|
|
146
141
|
* values _and_ plain string messages with no values, this type will still
|
|
147
142
|
* properly resolve all of the required values, assuming that the getters for
|
|
148
|
-
* the plain messages use the convention of passing `
|
|
143
|
+
* the plain messages use the convention of passing `{}` as the generic
|
|
149
144
|
* argument, representing "no values should be passed for this message".
|
|
150
145
|
*
|
|
151
146
|
*
|
|
@@ -173,13 +168,13 @@ export type FormatValuesFor<T> =
|
|
|
173
168
|
// be supplied, represented by the `never`. This prevents accidentally
|
|
174
169
|
// passing an object, even an empty one, for those strings, and allows the
|
|
175
170
|
// formatter to optimize a little more on each call.
|
|
176
|
-
[
|
|
171
|
+
[{}] extends [T]
|
|
177
172
|
? never
|
|
178
173
|
: // The `never` condition needs to be repeated here, but this time _without_
|
|
179
174
|
// the `[]` syntax, so that only the union elements with actual values are
|
|
180
175
|
// included in the union. Without this, the `infer U` on the latter side
|
|
181
176
|
// fails to resolve and the type just becomes `never`.
|
|
182
|
-
UnionToIntersection<
|
|
177
|
+
UnionToIntersection<{} extends T ? never : T>;
|
|
183
178
|
|
|
184
179
|
/**
|
|
185
180
|
* A template type for replaceable placeholder types to be defined by formatter
|
|
@@ -254,7 +249,7 @@ export type RequiredFormatValues<
|
|
|
254
249
|
Values extends IntlMessageGetter,
|
|
255
250
|
FunctionTypes extends FunctionTypeMap,
|
|
256
251
|
> =
|
|
257
|
-
IntlMessageGetterInnerType<Values>
|
|
252
|
+
{} extends IntlMessageGetterInnerType<Values>
|
|
258
253
|
? {}
|
|
259
254
|
: CheckStrictAny<
|
|
260
255
|
IntlMessageGetterInnerType<Values>,
|