@xndrjs/i18n 0.7.0 → 0.8.0
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/README.md +67 -872
- package/dist/codegen/index.d.ts +117 -51
- package/dist/codegen/index.js +1585 -93
- package/dist/codegen/index.js.map +1 -1
- package/dist/index.d.ts +100 -234
- package/dist/index.js +74 -638
- package/dist/index.js.map +1 -1
- package/dist/validation/index.d.ts +5 -0
- package/dist/validation/index.js.map +1 -1
- package/package.json +2 -2
- package/src/IcuTranslationProviderMulti.test.ts +18 -40
- package/src/IcuTranslationProviderMulti.ts +27 -37
- package/src/audit/audit-dictionaries.ts +1 -1
- package/src/audit/run-audit.test.ts +6 -7
- package/src/builder-load-registry.ts +26 -5
- package/src/builder-loaders.ts +8 -11
- package/src/builder-types.test.ts +11 -42
- package/src/builder.test.ts +82 -243
- package/src/builder.ts +8 -110
- package/src/codegen/codegen-config-schema.ts +82 -77
- package/src/codegen/config.test.ts +55 -143
- package/src/codegen/config.ts +10 -67
- package/src/codegen/dictionary-spec-contract.test.ts +28 -0
- package/src/codegen/dictionary-spec-contract.ts +138 -0
- package/src/codegen/emit/dictionary-schema-file.ts +7 -43
- package/src/codegen/emit/instance-file.test.ts +35 -52
- package/src/codegen/emit/instance-file.ts +170 -268
- package/src/codegen/emit/namespace-loaders-file.test.ts +72 -74
- package/src/codegen/emit/namespace-loaders-file.ts +147 -90
- package/src/codegen/emit/types-file.test.ts +20 -41
- package/src/codegen/emit/types-file.ts +48 -74
- package/src/codegen/generate-i18n-types.test.ts +163 -492
- package/src/codegen/generate-i18n-types.ts +7 -269
- package/src/codegen/paths.ts +0 -14
- package/src/codegen/project-locales-set-namespace.test.ts +44 -86
- package/src/codegen/read-dictionary.test.ts +27 -9
- package/src/codegen/read-dictionary.ts +18 -40
- package/src/codegen/regenerate-namespaces.ts +180 -0
- package/src/codegen/run-codegen.test.ts +230 -0
- package/src/codegen/run-codegen.ts +252 -0
- package/src/codegen/types.ts +0 -6
- package/src/codegen-config/build-config.ts +4 -23
- package/src/codegen-config/codegen-config.test.ts +10 -6
- package/src/codegen-config/index.ts +18 -3
- package/src/engine.ts +3 -27
- package/src/fetch-artifact.ts +16 -0
- package/src/i18n-handle.ts +202 -0
- package/src/index.ts +15 -34
- package/src/project-locales.ts +2 -2
- package/src/scope-multi.ts +1 -20
- package/src/scope-types.ts +14 -6
- package/src/scope.test.ts +3 -312
- package/src/serialized-state.test.ts +114 -0
- package/src/serialized-state.ts +28 -0
- package/src/setup/setup-i18n.test.ts +22 -33
- package/src/setup/setup-i18n.ts +22 -27
- package/src/types.ts +3 -3
- package/src/validation/index.ts +4 -0
- package/src/IcuTranslationProviderSingle.test.ts +0 -177
- package/src/IcuTranslationProviderSingle.ts +0 -114
- package/src/builder-multi.ts +0 -481
- package/src/codegen/emit/dictionary-file.test.ts +0 -215
- package/src/codegen/emit/dictionary-file.ts +0 -244
- package/src/deep-freeze.test.ts +0 -40
- package/src/deep-freeze.ts +0 -22
- package/src/patch-key.test.ts +0 -186
- package/src/patch-key.ts +0 -140
- package/src/scope-single.ts +0 -85
- package/src/single-builder.ts +0 -153
package/src/builder-multi.ts
DELETED
|
@@ -1,481 +0,0 @@
|
|
|
1
|
-
import { invokeNamespaceLoader, type NamespaceLoader } from "./builder-loaders.js";
|
|
2
|
-
import type { DeliveryArtifactsMap, LocalesForDeliveryAreaOrAll } from "./builder-types.js";
|
|
3
|
-
import type { I18nEngineMultiImpl } from "./engine.js";
|
|
4
|
-
import type { LocaleOfMulti, MultiDictionary, PartialKeyDictionary } from "./types.js";
|
|
5
|
-
import type { I18nScopeMulti, I18nScopeMultiForLocale } from "./scope-multi.js";
|
|
6
|
-
import type { MultiParams, ParamsForNamespaces, SchemaForNamespaces } from "./scope-types.js";
|
|
7
|
-
|
|
8
|
-
export type I18nBuilderMultiOptions<
|
|
9
|
-
Schema extends MultiDictionary,
|
|
10
|
-
RequestLocales extends string = LocaleOfMulti<Schema>,
|
|
11
|
-
> = {
|
|
12
|
-
namespaceLoaders?: {
|
|
13
|
-
[NS in keyof Schema & string]?: NamespaceLoader<
|
|
14
|
-
PartialKeyDictionary<Schema[NS], RequestLocales> | Schema[NS]
|
|
15
|
-
>;
|
|
16
|
-
};
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
/** Non-empty namespace list — required before partition or load. */
|
|
20
|
-
type NonEmptyNamespaces<NS extends readonly string[]> = NS extends readonly [] ? never : NS;
|
|
21
|
-
|
|
22
|
-
type MultiBuilderState<
|
|
23
|
-
Schema extends MultiDictionary,
|
|
24
|
-
RequestLocales extends string = LocaleOfMulti<Schema>,
|
|
25
|
-
NsList extends readonly (keyof Schema & string)[] = readonly (keyof Schema & string)[],
|
|
26
|
-
> = {
|
|
27
|
-
namespaces: NsList;
|
|
28
|
-
locale?: RequestLocales;
|
|
29
|
-
deliveryArea?: string;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
/** Builder returned by `createI18n({})` — only `withNamespaces` until namespaces are selected. */
|
|
33
|
-
export interface I18nBuilderMultiInitial<
|
|
34
|
-
Schema extends MultiDictionary,
|
|
35
|
-
Params extends MultiParams<Schema>,
|
|
36
|
-
RequestLocales extends string = LocaleOfMulti<Schema>,
|
|
37
|
-
ActiveLocales extends RequestLocales = RequestLocales,
|
|
38
|
-
DeliveryArea extends string = never,
|
|
39
|
-
DeliveryArtifacts extends DeliveryArtifactsMap<RequestLocales, DeliveryArea> =
|
|
40
|
-
DeliveryArtifactsMap<RequestLocales, DeliveryArea>,
|
|
41
|
-
> {
|
|
42
|
-
withNamespaces<const NS extends readonly (keyof Schema & string)[]>(
|
|
43
|
-
namespaces: NonEmptyNamespaces<NS>
|
|
44
|
-
): I18nBuilderMultiReady<
|
|
45
|
-
Schema,
|
|
46
|
-
Params,
|
|
47
|
-
RequestLocales,
|
|
48
|
-
ActiveLocales,
|
|
49
|
-
NS,
|
|
50
|
-
DeliveryArea,
|
|
51
|
-
DeliveryArtifacts
|
|
52
|
-
>;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/** Builder after `withNamespaces([...])` — partition selection and `load()` are available. */
|
|
56
|
-
export interface I18nBuilderMultiReady<
|
|
57
|
-
Schema extends MultiDictionary,
|
|
58
|
-
Params extends MultiParams<Schema>,
|
|
59
|
-
RequestLocales extends string = LocaleOfMulti<Schema>,
|
|
60
|
-
ActiveLocales extends RequestLocales = RequestLocales,
|
|
61
|
-
NsList extends readonly (keyof Schema & string)[] = readonly (keyof Schema & string)[],
|
|
62
|
-
DeliveryArea extends string = never,
|
|
63
|
-
DeliveryArtifacts extends DeliveryArtifactsMap<RequestLocales, DeliveryArea> =
|
|
64
|
-
DeliveryArtifactsMap<RequestLocales, DeliveryArea>,
|
|
65
|
-
> {
|
|
66
|
-
withNamespaces<const NS extends readonly (keyof Schema & string)[]>(
|
|
67
|
-
namespaces: NonEmptyNamespaces<NS>
|
|
68
|
-
): I18nBuilderMultiReady<
|
|
69
|
-
Schema,
|
|
70
|
-
Params,
|
|
71
|
-
RequestLocales,
|
|
72
|
-
ActiveLocales,
|
|
73
|
-
NS,
|
|
74
|
-
DeliveryArea,
|
|
75
|
-
DeliveryArtifacts
|
|
76
|
-
>;
|
|
77
|
-
|
|
78
|
-
withLocale<Locale extends ActiveLocales>(
|
|
79
|
-
locale: Locale
|
|
80
|
-
): I18nBuilderMultiForLocale<Schema, Params, RequestLocales, ActiveLocales, NsList, Locale>;
|
|
81
|
-
|
|
82
|
-
withDeliveryArea<Area extends DeliveryArea>(
|
|
83
|
-
area: Area
|
|
84
|
-
): I18nBuilderMultiPartitioned<
|
|
85
|
-
Schema,
|
|
86
|
-
Params,
|
|
87
|
-
RequestLocales,
|
|
88
|
-
LocalesForDeliveryAreaOrAll<RequestLocales, DeliveryArea, DeliveryArtifacts, Area>,
|
|
89
|
-
NsList,
|
|
90
|
-
DeliveryArea,
|
|
91
|
-
DeliveryArtifacts
|
|
92
|
-
>;
|
|
93
|
-
|
|
94
|
-
load(): Promise<
|
|
95
|
-
I18nScopeMulti<
|
|
96
|
-
SchemaForNamespaces<Schema, NsList>,
|
|
97
|
-
ParamsForNamespaces<Schema, Params, NsList>,
|
|
98
|
-
ActiveLocales
|
|
99
|
-
>
|
|
100
|
-
>;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/** @deprecated Alias for {@link I18nBuilderMultiInitial} — the multi builder before `withNamespaces`. */
|
|
104
|
-
export type I18nBuilderMulti<
|
|
105
|
-
Schema extends MultiDictionary,
|
|
106
|
-
Params extends MultiParams<Schema>,
|
|
107
|
-
RequestLocales extends string = LocaleOfMulti<Schema>,
|
|
108
|
-
ActiveLocales extends RequestLocales = RequestLocales,
|
|
109
|
-
NsList extends readonly (keyof Schema & string)[] = readonly [],
|
|
110
|
-
DeliveryArea extends string = never,
|
|
111
|
-
DeliveryArtifacts extends DeliveryArtifactsMap<RequestLocales, DeliveryArea> =
|
|
112
|
-
DeliveryArtifactsMap<RequestLocales, DeliveryArea>,
|
|
113
|
-
> = [NsList] extends [readonly []]
|
|
114
|
-
? I18nBuilderMultiInitial<
|
|
115
|
-
Schema,
|
|
116
|
-
Params,
|
|
117
|
-
RequestLocales,
|
|
118
|
-
ActiveLocales,
|
|
119
|
-
DeliveryArea,
|
|
120
|
-
DeliveryArtifacts
|
|
121
|
-
>
|
|
122
|
-
: I18nBuilderMultiReady<
|
|
123
|
-
Schema,
|
|
124
|
-
Params,
|
|
125
|
-
RequestLocales,
|
|
126
|
-
ActiveLocales,
|
|
127
|
-
NsList,
|
|
128
|
-
DeliveryArea,
|
|
129
|
-
DeliveryArtifacts
|
|
130
|
-
>;
|
|
131
|
-
|
|
132
|
-
/** Builder after `withDeliveryArea` — partition is fixed; bind locale via `scope.forLocale` after `load()`. */
|
|
133
|
-
export interface I18nBuilderMultiPartitioned<
|
|
134
|
-
Schema extends MultiDictionary,
|
|
135
|
-
Params extends MultiParams<Schema>,
|
|
136
|
-
RequestLocales extends string,
|
|
137
|
-
ActiveLocales extends RequestLocales,
|
|
138
|
-
NsList extends readonly (keyof Schema & string)[],
|
|
139
|
-
DeliveryArea extends string = never,
|
|
140
|
-
DeliveryArtifacts extends DeliveryArtifactsMap<RequestLocales, DeliveryArea> =
|
|
141
|
-
DeliveryArtifactsMap<RequestLocales, DeliveryArea>,
|
|
142
|
-
> {
|
|
143
|
-
withNamespaces<const NS extends readonly (keyof Schema & string)[]>(
|
|
144
|
-
namespaces: NonEmptyNamespaces<NS>
|
|
145
|
-
): I18nBuilderMultiPartitioned<
|
|
146
|
-
Schema,
|
|
147
|
-
Params,
|
|
148
|
-
RequestLocales,
|
|
149
|
-
ActiveLocales,
|
|
150
|
-
NS,
|
|
151
|
-
DeliveryArea,
|
|
152
|
-
DeliveryArtifacts
|
|
153
|
-
>;
|
|
154
|
-
|
|
155
|
-
load(): Promise<
|
|
156
|
-
I18nScopeMulti<
|
|
157
|
-
SchemaForNamespaces<Schema, NsList>,
|
|
158
|
-
ParamsForNamespaces<Schema, Params, NsList>,
|
|
159
|
-
ActiveLocales
|
|
160
|
-
>
|
|
161
|
-
>;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
export interface I18nBuilderMultiForLocale<
|
|
165
|
-
Schema extends MultiDictionary,
|
|
166
|
-
Params extends MultiParams<Schema>,
|
|
167
|
-
RequestLocales extends string,
|
|
168
|
-
ActiveLocales extends RequestLocales,
|
|
169
|
-
NsList extends readonly (keyof Schema & string)[],
|
|
170
|
-
Locale extends ActiveLocales,
|
|
171
|
-
> {
|
|
172
|
-
load(): Promise<
|
|
173
|
-
I18nScopeMultiForLocale<
|
|
174
|
-
SchemaForNamespaces<Schema, NsList>,
|
|
175
|
-
ParamsForNamespaces<Schema, Params, NsList>,
|
|
176
|
-
Locale,
|
|
177
|
-
Locale
|
|
178
|
-
>
|
|
179
|
-
>;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
export class I18nBuilderMultiInitialImpl<
|
|
183
|
-
Schema extends MultiDictionary,
|
|
184
|
-
Params extends MultiParams<Schema>,
|
|
185
|
-
RequestLocales extends string = LocaleOfMulti<Schema>,
|
|
186
|
-
ActiveLocales extends RequestLocales = RequestLocales,
|
|
187
|
-
DeliveryArea extends string = never,
|
|
188
|
-
DeliveryArtifacts extends DeliveryArtifactsMap<RequestLocales, DeliveryArea> =
|
|
189
|
-
DeliveryArtifactsMap<RequestLocales, DeliveryArea>,
|
|
190
|
-
> implements I18nBuilderMultiInitial<
|
|
191
|
-
Schema,
|
|
192
|
-
Params,
|
|
193
|
-
RequestLocales,
|
|
194
|
-
ActiveLocales,
|
|
195
|
-
DeliveryArea,
|
|
196
|
-
DeliveryArtifacts
|
|
197
|
-
> {
|
|
198
|
-
constructor(
|
|
199
|
-
private readonly engine: I18nEngineMultiImpl<Schema, Params, RequestLocales>,
|
|
200
|
-
private readonly options?: I18nBuilderMultiOptions<Schema, RequestLocales>
|
|
201
|
-
) {}
|
|
202
|
-
|
|
203
|
-
withNamespaces<const NS extends readonly (keyof Schema & string)[]>(
|
|
204
|
-
namespaces: NonEmptyNamespaces<NS>
|
|
205
|
-
): I18nBuilderMultiReadyImpl<
|
|
206
|
-
Schema,
|
|
207
|
-
Params,
|
|
208
|
-
RequestLocales,
|
|
209
|
-
ActiveLocales,
|
|
210
|
-
NS,
|
|
211
|
-
DeliveryArea,
|
|
212
|
-
DeliveryArtifacts
|
|
213
|
-
> {
|
|
214
|
-
return new I18nBuilderMultiReadyImpl<
|
|
215
|
-
Schema,
|
|
216
|
-
Params,
|
|
217
|
-
RequestLocales,
|
|
218
|
-
ActiveLocales,
|
|
219
|
-
NS,
|
|
220
|
-
DeliveryArea,
|
|
221
|
-
DeliveryArtifacts
|
|
222
|
-
>(this.engine, this.options, { namespaces });
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
export class I18nBuilderMultiReadyImpl<
|
|
227
|
-
Schema extends MultiDictionary,
|
|
228
|
-
Params extends MultiParams<Schema>,
|
|
229
|
-
RequestLocales extends string = LocaleOfMulti<Schema>,
|
|
230
|
-
ActiveLocales extends RequestLocales = RequestLocales,
|
|
231
|
-
NsList extends readonly (keyof Schema & string)[] = readonly (keyof Schema & string)[],
|
|
232
|
-
DeliveryArea extends string = never,
|
|
233
|
-
DeliveryArtifacts extends DeliveryArtifactsMap<RequestLocales, DeliveryArea> =
|
|
234
|
-
DeliveryArtifactsMap<RequestLocales, DeliveryArea>,
|
|
235
|
-
> implements I18nBuilderMultiReady<
|
|
236
|
-
Schema,
|
|
237
|
-
Params,
|
|
238
|
-
RequestLocales,
|
|
239
|
-
ActiveLocales,
|
|
240
|
-
NsList,
|
|
241
|
-
DeliveryArea,
|
|
242
|
-
DeliveryArtifacts
|
|
243
|
-
> {
|
|
244
|
-
constructor(
|
|
245
|
-
private readonly engine: I18nEngineMultiImpl<Schema, Params, RequestLocales>,
|
|
246
|
-
private readonly options: I18nBuilderMultiOptions<Schema, RequestLocales> | undefined,
|
|
247
|
-
private readonly state: MultiBuilderState<Schema, RequestLocales, NsList>
|
|
248
|
-
) {}
|
|
249
|
-
|
|
250
|
-
private clone(): MultiBuilderState<Schema, RequestLocales, NsList> {
|
|
251
|
-
return {
|
|
252
|
-
namespaces: this.state.namespaces,
|
|
253
|
-
...(this.state.locale !== undefined ? { locale: this.state.locale } : {}),
|
|
254
|
-
...(this.state.deliveryArea !== undefined ? { deliveryArea: this.state.deliveryArea } : {}),
|
|
255
|
-
};
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
withNamespaces<const NS extends readonly (keyof Schema & string)[]>(
|
|
259
|
-
namespaces: NonEmptyNamespaces<NS>
|
|
260
|
-
): I18nBuilderMultiReadyImpl<
|
|
261
|
-
Schema,
|
|
262
|
-
Params,
|
|
263
|
-
RequestLocales,
|
|
264
|
-
ActiveLocales,
|
|
265
|
-
NS,
|
|
266
|
-
DeliveryArea,
|
|
267
|
-
DeliveryArtifacts
|
|
268
|
-
> {
|
|
269
|
-
return new I18nBuilderMultiReadyImpl<
|
|
270
|
-
Schema,
|
|
271
|
-
Params,
|
|
272
|
-
RequestLocales,
|
|
273
|
-
ActiveLocales,
|
|
274
|
-
NS,
|
|
275
|
-
DeliveryArea,
|
|
276
|
-
DeliveryArtifacts
|
|
277
|
-
>(this.engine, this.options, {
|
|
278
|
-
...this.clone(),
|
|
279
|
-
namespaces,
|
|
280
|
-
});
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
withLocale<Locale extends ActiveLocales>(
|
|
284
|
-
locale: Locale
|
|
285
|
-
): I18nBuilderMultiForLocaleImpl<Schema, Params, RequestLocales, ActiveLocales, NsList, Locale> {
|
|
286
|
-
const { deliveryArea: _deliveryArea, ...rest } = this.clone();
|
|
287
|
-
return new I18nBuilderMultiForLocaleImpl<
|
|
288
|
-
Schema,
|
|
289
|
-
Params,
|
|
290
|
-
RequestLocales,
|
|
291
|
-
ActiveLocales,
|
|
292
|
-
NsList,
|
|
293
|
-
Locale
|
|
294
|
-
>(this.engine, this.options, { ...rest, locale });
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
withDeliveryArea<Area extends DeliveryArea>(
|
|
298
|
-
area: Area
|
|
299
|
-
): I18nBuilderMultiPartitionedImpl<
|
|
300
|
-
Schema,
|
|
301
|
-
Params,
|
|
302
|
-
RequestLocales,
|
|
303
|
-
LocalesForDeliveryAreaOrAll<RequestLocales, DeliveryArea, DeliveryArtifacts, Area>,
|
|
304
|
-
NsList,
|
|
305
|
-
DeliveryArea,
|
|
306
|
-
DeliveryArtifacts
|
|
307
|
-
> {
|
|
308
|
-
const { locale: _locale, ...rest } = this.clone();
|
|
309
|
-
return new I18nBuilderMultiPartitionedImpl<
|
|
310
|
-
Schema,
|
|
311
|
-
Params,
|
|
312
|
-
RequestLocales,
|
|
313
|
-
LocalesForDeliveryAreaOrAll<RequestLocales, DeliveryArea, DeliveryArtifacts, Area>,
|
|
314
|
-
NsList,
|
|
315
|
-
DeliveryArea,
|
|
316
|
-
DeliveryArtifacts
|
|
317
|
-
>(this.engine, this.options, { ...rest, deliveryArea: area });
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
async load(): Promise<
|
|
321
|
-
I18nScopeMulti<
|
|
322
|
-
SchemaForNamespaces<Schema, NsList>,
|
|
323
|
-
ParamsForNamespaces<Schema, Params, NsList>,
|
|
324
|
-
ActiveLocales
|
|
325
|
-
>
|
|
326
|
-
> {
|
|
327
|
-
await applyMultiBuilderLoad(this.engine, this.options, this.state);
|
|
328
|
-
return this.engine.toScope({ namespaces: this.state.namespaces });
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
/** @deprecated Use {@link I18nBuilderMultiInitialImpl} or {@link I18nBuilderMultiReadyImpl}. */
|
|
333
|
-
export class I18nBuilderMultiImpl<
|
|
334
|
-
Schema extends MultiDictionary,
|
|
335
|
-
Params extends MultiParams<Schema>,
|
|
336
|
-
RequestLocales extends string = LocaleOfMulti<Schema>,
|
|
337
|
-
ActiveLocales extends RequestLocales = RequestLocales,
|
|
338
|
-
NsList extends readonly (keyof Schema & string)[] = readonly (keyof Schema & string)[],
|
|
339
|
-
DeliveryArea extends string = never,
|
|
340
|
-
DeliveryArtifacts extends DeliveryArtifactsMap<RequestLocales, DeliveryArea> =
|
|
341
|
-
DeliveryArtifactsMap<RequestLocales, DeliveryArea>,
|
|
342
|
-
> extends I18nBuilderMultiReadyImpl<
|
|
343
|
-
Schema,
|
|
344
|
-
Params,
|
|
345
|
-
RequestLocales,
|
|
346
|
-
ActiveLocales,
|
|
347
|
-
NsList,
|
|
348
|
-
DeliveryArea,
|
|
349
|
-
DeliveryArtifacts
|
|
350
|
-
> {}
|
|
351
|
-
|
|
352
|
-
export class I18nBuilderMultiPartitionedImpl<
|
|
353
|
-
Schema extends MultiDictionary,
|
|
354
|
-
Params extends MultiParams<Schema>,
|
|
355
|
-
RequestLocales extends string,
|
|
356
|
-
ActiveLocales extends RequestLocales,
|
|
357
|
-
NsList extends readonly (keyof Schema & string)[],
|
|
358
|
-
DeliveryArea extends string = never,
|
|
359
|
-
DeliveryArtifacts extends DeliveryArtifactsMap<RequestLocales, DeliveryArea> =
|
|
360
|
-
DeliveryArtifactsMap<RequestLocales, DeliveryArea>,
|
|
361
|
-
> implements I18nBuilderMultiPartitioned<
|
|
362
|
-
Schema,
|
|
363
|
-
Params,
|
|
364
|
-
RequestLocales,
|
|
365
|
-
ActiveLocales,
|
|
366
|
-
NsList,
|
|
367
|
-
DeliveryArea,
|
|
368
|
-
DeliveryArtifacts
|
|
369
|
-
> {
|
|
370
|
-
constructor(
|
|
371
|
-
private readonly engine: I18nEngineMultiImpl<Schema, Params, RequestLocales>,
|
|
372
|
-
private readonly options: I18nBuilderMultiOptions<Schema, RequestLocales> | undefined,
|
|
373
|
-
private readonly state: MultiBuilderState<Schema, RequestLocales, NsList> & {
|
|
374
|
-
deliveryArea: string;
|
|
375
|
-
}
|
|
376
|
-
) {}
|
|
377
|
-
|
|
378
|
-
withNamespaces<const NS extends readonly (keyof Schema & string)[]>(
|
|
379
|
-
namespaces: NonEmptyNamespaces<NS>
|
|
380
|
-
): I18nBuilderMultiPartitionedImpl<
|
|
381
|
-
Schema,
|
|
382
|
-
Params,
|
|
383
|
-
RequestLocales,
|
|
384
|
-
ActiveLocales,
|
|
385
|
-
NS,
|
|
386
|
-
DeliveryArea,
|
|
387
|
-
DeliveryArtifacts
|
|
388
|
-
> {
|
|
389
|
-
return new I18nBuilderMultiPartitionedImpl<
|
|
390
|
-
Schema,
|
|
391
|
-
Params,
|
|
392
|
-
RequestLocales,
|
|
393
|
-
ActiveLocales,
|
|
394
|
-
NS,
|
|
395
|
-
DeliveryArea,
|
|
396
|
-
DeliveryArtifacts
|
|
397
|
-
>(this.engine, this.options, {
|
|
398
|
-
...this.state,
|
|
399
|
-
namespaces,
|
|
400
|
-
});
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
async load(): Promise<
|
|
404
|
-
I18nScopeMulti<
|
|
405
|
-
SchemaForNamespaces<Schema, NsList>,
|
|
406
|
-
ParamsForNamespaces<Schema, Params, NsList>,
|
|
407
|
-
ActiveLocales
|
|
408
|
-
>
|
|
409
|
-
> {
|
|
410
|
-
await applyMultiBuilderLoad(this.engine, this.options, this.state);
|
|
411
|
-
return this.engine.toScope({ namespaces: this.state.namespaces });
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
export class I18nBuilderMultiForLocaleImpl<
|
|
416
|
-
Schema extends MultiDictionary,
|
|
417
|
-
Params extends MultiParams<Schema>,
|
|
418
|
-
RequestLocales extends string,
|
|
419
|
-
ActiveLocales extends RequestLocales,
|
|
420
|
-
NsList extends readonly (keyof Schema & string)[],
|
|
421
|
-
Locale extends ActiveLocales,
|
|
422
|
-
> implements I18nBuilderMultiForLocale<
|
|
423
|
-
Schema,
|
|
424
|
-
Params,
|
|
425
|
-
RequestLocales,
|
|
426
|
-
ActiveLocales,
|
|
427
|
-
NsList,
|
|
428
|
-
Locale
|
|
429
|
-
> {
|
|
430
|
-
constructor(
|
|
431
|
-
private readonly engine: I18nEngineMultiImpl<Schema, Params, RequestLocales>,
|
|
432
|
-
private readonly options: I18nBuilderMultiOptions<Schema, RequestLocales> | undefined,
|
|
433
|
-
private readonly state: MultiBuilderState<Schema, RequestLocales, NsList> & { locale: Locale }
|
|
434
|
-
) {}
|
|
435
|
-
|
|
436
|
-
async load(): Promise<
|
|
437
|
-
I18nScopeMultiForLocale<
|
|
438
|
-
SchemaForNamespaces<Schema, NsList>,
|
|
439
|
-
ParamsForNamespaces<Schema, Params, NsList>,
|
|
440
|
-
Locale,
|
|
441
|
-
Locale
|
|
442
|
-
>
|
|
443
|
-
> {
|
|
444
|
-
await applyMultiBuilderLoad(this.engine, this.options, this.state);
|
|
445
|
-
return this.engine.toScope({ namespaces: this.state.namespaces }).forLocale(this.state.locale);
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
async function applyMultiBuilderLoad<
|
|
450
|
-
Schema extends MultiDictionary,
|
|
451
|
-
Params extends MultiParams<Schema>,
|
|
452
|
-
RequestLocales extends string,
|
|
453
|
-
NsList extends readonly (keyof Schema & string)[],
|
|
454
|
-
>(
|
|
455
|
-
engine: I18nEngineMultiImpl<Schema, Params, RequestLocales>,
|
|
456
|
-
options: I18nBuilderMultiOptions<Schema, RequestLocales> | undefined,
|
|
457
|
-
state: MultiBuilderState<Schema, RequestLocales, NsList>
|
|
458
|
-
): Promise<void> {
|
|
459
|
-
if (state.namespaces.length === 0) {
|
|
460
|
-
throw new Error("[i18n] withNamespaces(...) is required before load().");
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
const partition = state.locale ?? state.deliveryArea;
|
|
464
|
-
|
|
465
|
-
await Promise.all(
|
|
466
|
-
state.namespaces.map(async (namespace) => {
|
|
467
|
-
const loader = options?.namespaceLoaders?.[namespace];
|
|
468
|
-
if (loader === undefined) {
|
|
469
|
-
return;
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
if (engine.hasBuilderResourceLoaded(namespace, partition)) {
|
|
473
|
-
return;
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
const data = await invokeNamespaceLoader(loader, partition);
|
|
477
|
-
engine.applyLoadMergeNamespace(namespace, data);
|
|
478
|
-
engine.markBuilderResourceLoaded(namespace, partition);
|
|
479
|
-
})
|
|
480
|
-
);
|
|
481
|
-
}
|
|
@@ -1,215 +0,0 @@
|
|
|
1
|
-
import path from "node:path";
|
|
2
|
-
import { describe, expect, it } from "vitest";
|
|
3
|
-
import { formatDictionaryFile } from "./dictionary-file.js";
|
|
4
|
-
|
|
5
|
-
const projectRoot = "/project";
|
|
6
|
-
const dictionaryOutputPath = path.join(projectRoot, "src/i18n/dictionary.generated.ts");
|
|
7
|
-
const typesOutputPath = path.join(projectRoot, "src/i18n/i18n-types.generated.ts");
|
|
8
|
-
|
|
9
|
-
describe("formatDictionaryFile", () => {
|
|
10
|
-
it("emits defaultDictionary in canonical mode", () => {
|
|
11
|
-
const output = formatDictionaryFile({
|
|
12
|
-
isSingle: false,
|
|
13
|
-
hasLazy: true,
|
|
14
|
-
entries: [
|
|
15
|
-
{ namespace: "default", filePath: "src/i18n/translations/default.json" },
|
|
16
|
-
{ namespace: "billing", filePath: "src/i18n/translations/billing.json" },
|
|
17
|
-
],
|
|
18
|
-
eagerEntries: [{ namespace: "default", filePath: "src/i18n/translations/default.json" }],
|
|
19
|
-
projectRoot,
|
|
20
|
-
dictionaryOutputPath,
|
|
21
|
-
typesOutputPath,
|
|
22
|
-
schemaTypeName: "AppSchema",
|
|
23
|
-
localeTypeName: "AppLocale",
|
|
24
|
-
importExtension: "none",
|
|
25
|
-
delivery: "canonical",
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
expect(output).toContain("export const defaultDictionary: InitialSchema");
|
|
29
|
-
expect(output).toContain("defaultNs");
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it("emits an empty module when all namespaces are lazy in split mode (multi)", () => {
|
|
33
|
-
const output = formatDictionaryFile({
|
|
34
|
-
isSingle: false,
|
|
35
|
-
hasLazy: true,
|
|
36
|
-
entries: [
|
|
37
|
-
{ namespace: "default", filePath: "src/i18n/translations/default.json" },
|
|
38
|
-
{ namespace: "billing", filePath: "src/i18n/translations/billing.json" },
|
|
39
|
-
],
|
|
40
|
-
eagerEntries: [],
|
|
41
|
-
projectRoot,
|
|
42
|
-
dictionaryOutputPath,
|
|
43
|
-
typesOutputPath,
|
|
44
|
-
schemaTypeName: "AppSchema",
|
|
45
|
-
localeTypeName: "AppLocale",
|
|
46
|
-
importExtension: "none",
|
|
47
|
-
delivery: "split-by-locale",
|
|
48
|
-
requestLocales: ["en", "it"],
|
|
49
|
-
splitPathsByNamespace: {
|
|
50
|
-
default: {
|
|
51
|
-
en: "src/i18n/generated/translations/default.en.json",
|
|
52
|
-
it: "src/i18n/generated/translations/default.it.json",
|
|
53
|
-
},
|
|
54
|
-
billing: {
|
|
55
|
-
en: "src/i18n/generated/translations/billing.en.json",
|
|
56
|
-
it: "src/i18n/generated/translations/billing.it.json",
|
|
57
|
-
},
|
|
58
|
-
},
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
expect(output).toBeNull();
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it("emits defaultDictionaryFor with per-locale imports in split mode (multi)", () => {
|
|
65
|
-
const output = formatDictionaryFile({
|
|
66
|
-
isSingle: false,
|
|
67
|
-
hasLazy: true,
|
|
68
|
-
entries: [
|
|
69
|
-
{ namespace: "default", filePath: "src/i18n/translations/default.json" },
|
|
70
|
-
{ namespace: "billing", filePath: "src/i18n/translations/billing.json" },
|
|
71
|
-
],
|
|
72
|
-
eagerEntries: [{ namespace: "default", filePath: "src/i18n/translations/default.json" }],
|
|
73
|
-
projectRoot,
|
|
74
|
-
dictionaryOutputPath,
|
|
75
|
-
typesOutputPath,
|
|
76
|
-
schemaTypeName: "AppSchema",
|
|
77
|
-
localeTypeName: "AppLocale",
|
|
78
|
-
importExtension: "none",
|
|
79
|
-
delivery: "split-by-locale",
|
|
80
|
-
requestLocales: ["en", "it"],
|
|
81
|
-
splitPathsByNamespace: {
|
|
82
|
-
default: {
|
|
83
|
-
en: "src/i18n/generated/translations/default.en.json",
|
|
84
|
-
it: "src/i18n/generated/translations/default.it.json",
|
|
85
|
-
},
|
|
86
|
-
billing: {
|
|
87
|
-
en: "src/i18n/generated/translations/billing.en.json",
|
|
88
|
-
it: "src/i18n/generated/translations/billing.it.json",
|
|
89
|
-
},
|
|
90
|
-
},
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
expect(output).toContain("import defaultEn from './generated/translations/default.en.json';");
|
|
94
|
-
expect(output).toContain("import defaultIt from './generated/translations/default.it.json';");
|
|
95
|
-
expect(output).toContain(
|
|
96
|
-
"const defaultByLocale = {\n en: defaultEn,\n it: defaultIt,\n} as const;"
|
|
97
|
-
);
|
|
98
|
-
expect(output).toContain(
|
|
99
|
-
"export function defaultDictionaryFor(locale: AppLocale): InitialSchema"
|
|
100
|
-
);
|
|
101
|
-
expect(output).toContain("return {\n default: defaultByLocale[locale],\n };");
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
it("emits defaultDictionaryFor with per-locale imports in split mode (single)", () => {
|
|
105
|
-
const output = formatDictionaryFile({
|
|
106
|
-
isSingle: true,
|
|
107
|
-
hasLazy: false,
|
|
108
|
-
entries: [{ namespace: "translations", filePath: "src/i18n/translations/translations.json" }],
|
|
109
|
-
eagerEntries: [
|
|
110
|
-
{ namespace: "translations", filePath: "src/i18n/translations/translations.json" },
|
|
111
|
-
],
|
|
112
|
-
projectRoot,
|
|
113
|
-
dictionaryOutputPath,
|
|
114
|
-
typesOutputPath,
|
|
115
|
-
schemaTypeName: "AppSchema",
|
|
116
|
-
localeTypeName: "AppLocale",
|
|
117
|
-
importExtension: "none",
|
|
118
|
-
delivery: "split-by-locale",
|
|
119
|
-
requestLocales: ["en", "it"],
|
|
120
|
-
splitPathsByNamespace: {
|
|
121
|
-
translations: {
|
|
122
|
-
en: "src/i18n/generated/translations/translations.en.json",
|
|
123
|
-
it: "src/i18n/generated/translations/translations.it.json",
|
|
124
|
-
},
|
|
125
|
-
},
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
expect(output).toContain(
|
|
129
|
-
"import translationsEn from './generated/translations/translations.en.json';"
|
|
130
|
-
);
|
|
131
|
-
expect(output).toContain(
|
|
132
|
-
"import translationsIt from './generated/translations/translations.it.json';"
|
|
133
|
-
);
|
|
134
|
-
expect(output).toContain("export function defaultDictionaryFor(locale: AppLocale): AppSchema");
|
|
135
|
-
expect(output).toContain("return translationsByLocale[locale];");
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
it("emits defaultDictionaryFor with per-area imports in custom mode (multi)", () => {
|
|
139
|
-
const output = formatDictionaryFile({
|
|
140
|
-
isSingle: false,
|
|
141
|
-
hasLazy: true,
|
|
142
|
-
entries: [
|
|
143
|
-
{ namespace: "default", filePath: "src/i18n/translations/default.json" },
|
|
144
|
-
{ namespace: "billing", filePath: "src/i18n/translations/billing.json" },
|
|
145
|
-
],
|
|
146
|
-
eagerEntries: [{ namespace: "default", filePath: "src/i18n/translations/default.json" }],
|
|
147
|
-
projectRoot,
|
|
148
|
-
dictionaryOutputPath,
|
|
149
|
-
typesOutputPath,
|
|
150
|
-
schemaTypeName: "AppSchema",
|
|
151
|
-
localeTypeName: "AppLocale",
|
|
152
|
-
importExtension: "none",
|
|
153
|
-
delivery: "custom",
|
|
154
|
-
deliveryAreaTypeName: "AppDeliveryArea",
|
|
155
|
-
deliveryAreaNames: ["eu", "us"],
|
|
156
|
-
splitPathsByNamespace: {
|
|
157
|
-
default: {
|
|
158
|
-
eu: "src/i18n/generated/translations/default.eu.json",
|
|
159
|
-
us: "src/i18n/generated/translations/default.us.json",
|
|
160
|
-
},
|
|
161
|
-
billing: {
|
|
162
|
-
eu: "src/i18n/generated/translations/billing.eu.json",
|
|
163
|
-
us: "src/i18n/generated/translations/billing.us.json",
|
|
164
|
-
},
|
|
165
|
-
},
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
expect(output).toContain("import defaultEu from './generated/translations/default.eu.json';");
|
|
169
|
-
expect(output).toContain("import defaultUs from './generated/translations/default.us.json';");
|
|
170
|
-
expect(output).toContain(
|
|
171
|
-
"const defaultByArea = {\n eu: defaultEu,\n us: defaultUs,\n} as const;"
|
|
172
|
-
);
|
|
173
|
-
expect(output).toContain(
|
|
174
|
-
"export function defaultDictionaryFor(area: AppDeliveryArea): InitialSchema"
|
|
175
|
-
);
|
|
176
|
-
expect(output).toContain("return {\n default: defaultByArea[area],\n };");
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
it("emits defaultDictionaryFor with per-area imports in custom mode (single)", () => {
|
|
180
|
-
const output = formatDictionaryFile({
|
|
181
|
-
isSingle: true,
|
|
182
|
-
hasLazy: false,
|
|
183
|
-
entries: [{ namespace: "translations", filePath: "src/i18n/translations/translations.json" }],
|
|
184
|
-
eagerEntries: [
|
|
185
|
-
{ namespace: "translations", filePath: "src/i18n/translations/translations.json" },
|
|
186
|
-
],
|
|
187
|
-
projectRoot,
|
|
188
|
-
dictionaryOutputPath,
|
|
189
|
-
typesOutputPath,
|
|
190
|
-
schemaTypeName: "AppSchema",
|
|
191
|
-
localeTypeName: "AppLocale",
|
|
192
|
-
importExtension: "none",
|
|
193
|
-
delivery: "custom",
|
|
194
|
-
deliveryAreaTypeName: "AppDeliveryArea",
|
|
195
|
-
deliveryAreaNames: ["eu", "us"],
|
|
196
|
-
splitPathsByNamespace: {
|
|
197
|
-
translations: {
|
|
198
|
-
eu: "src/i18n/generated/translations/translations.eu.json",
|
|
199
|
-
us: "src/i18n/generated/translations/translations.us.json",
|
|
200
|
-
},
|
|
201
|
-
},
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
expect(output).toContain(
|
|
205
|
-
"import translationsEu from './generated/translations/translations.eu.json';"
|
|
206
|
-
);
|
|
207
|
-
expect(output).toContain(
|
|
208
|
-
"import translationsUs from './generated/translations/translations.us.json';"
|
|
209
|
-
);
|
|
210
|
-
expect(output).toContain(
|
|
211
|
-
"export function defaultDictionaryFor(area: AppDeliveryArea): AppSchema"
|
|
212
|
-
);
|
|
213
|
-
expect(output).toContain("return translationsByArea[area];");
|
|
214
|
-
});
|
|
215
|
-
});
|