@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/scope-single.ts
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import type { IcuTranslationProviderSingle } from "./IcuTranslationProviderSingle.js";
|
|
2
|
-
import type { KeyDictionary, LocaleFallbackMap, LocaleOfSingle } from "./types.js";
|
|
3
|
-
import type { ParamArgs } from "./scope-types.js";
|
|
4
|
-
|
|
5
|
-
/** Type-safe translation scope for a single-namespace schema. */
|
|
6
|
-
export interface I18nScopeSingle<
|
|
7
|
-
Schema extends KeyDictionary,
|
|
8
|
-
Params extends { [K in keyof Schema]: unknown },
|
|
9
|
-
RequestLocales extends string = LocaleOfSingle<Schema>,
|
|
10
|
-
> {
|
|
11
|
-
t<const K extends keyof Schema & string>(
|
|
12
|
-
key: K,
|
|
13
|
-
locale: RequestLocales,
|
|
14
|
-
...params: ParamArgs<Params[K]>
|
|
15
|
-
): string;
|
|
16
|
-
forLocale<Locale extends RequestLocales>(
|
|
17
|
-
locale: Locale
|
|
18
|
-
): I18nScopeSingleForLocale<Schema, Params, Locale>;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/** Single-namespace scope with a bound locale — `t(key)` and `set(key)` without a locale argument. */
|
|
22
|
-
export interface I18nScopeSingleForLocale<
|
|
23
|
-
Schema extends KeyDictionary,
|
|
24
|
-
Params extends { [K in keyof Schema]: unknown },
|
|
25
|
-
Locale extends string,
|
|
26
|
-
> {
|
|
27
|
-
readonly locale: Locale;
|
|
28
|
-
t<const K extends keyof Schema & string>(key: K, ...params: ParamArgs<Params[K]>): string;
|
|
29
|
-
set<const K extends keyof Schema & string>(key: K, template: string): void;
|
|
30
|
-
forLocale<Next extends Locale>(locale: Next): I18nScopeSingleForLocale<Schema, Params, Next>;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export class I18nScopeSingleImpl<
|
|
34
|
-
Schema extends KeyDictionary,
|
|
35
|
-
Params extends { [K in keyof Schema]: unknown },
|
|
36
|
-
RequestLocales extends string,
|
|
37
|
-
Fallback extends LocaleFallbackMap | undefined,
|
|
38
|
-
> implements I18nScopeSingle<Schema, Params, RequestLocales> {
|
|
39
|
-
constructor(
|
|
40
|
-
private readonly engine: IcuTranslationProviderSingle<Schema, Params, RequestLocales, Fallback>
|
|
41
|
-
) {}
|
|
42
|
-
|
|
43
|
-
t = <const K extends keyof Schema & string>(
|
|
44
|
-
key: K,
|
|
45
|
-
locale: RequestLocales,
|
|
46
|
-
...args: ParamArgs<Params[K]>
|
|
47
|
-
): string => {
|
|
48
|
-
const params = args[0] as Record<string, unknown> | undefined;
|
|
49
|
-
return this.engine.getWithLocale(String(key), locale, params);
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
forLocale = <Locale extends RequestLocales>(
|
|
53
|
-
locale: Locale
|
|
54
|
-
): I18nScopeSingleForLocaleImpl<Schema, Params, RequestLocales, Locale, Fallback> => {
|
|
55
|
-
return new I18nScopeSingleForLocaleImpl(this.engine, locale);
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export class I18nScopeSingleForLocaleImpl<
|
|
60
|
-
Schema extends KeyDictionary,
|
|
61
|
-
Params extends { [K in keyof Schema]: unknown },
|
|
62
|
-
RequestLocales extends string,
|
|
63
|
-
Locale extends RequestLocales,
|
|
64
|
-
Fallback extends LocaleFallbackMap | undefined,
|
|
65
|
-
> implements I18nScopeSingleForLocale<Schema, Params, Locale> {
|
|
66
|
-
constructor(
|
|
67
|
-
private readonly engine: IcuTranslationProviderSingle<Schema, Params, RequestLocales, Fallback>,
|
|
68
|
-
readonly locale: Locale
|
|
69
|
-
) {}
|
|
70
|
-
|
|
71
|
-
t = <const K extends keyof Schema & string>(key: K, ...args: ParamArgs<Params[K]>): string => {
|
|
72
|
-
const params = args[0] as Record<string, unknown> | undefined;
|
|
73
|
-
return this.engine.getWithLocale(String(key), this.locale, params);
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
set = <const K extends keyof Schema & string>(key: K, template: string): void => {
|
|
77
|
-
this.engine.patchKey(String(key), this.locale, template);
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
forLocale = <Next extends Locale>(
|
|
81
|
-
locale: Next
|
|
82
|
-
): I18nScopeSingleForLocaleImpl<Schema, Params, RequestLocales, Next, Fallback> => {
|
|
83
|
-
return new I18nScopeSingleForLocaleImpl(this.engine, locale);
|
|
84
|
-
};
|
|
85
|
-
}
|
package/src/single-builder.ts
DELETED
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
import { invokeNamespaceLoader, type NamespaceLoader } from "./builder-loaders.js";
|
|
2
|
-
import type { I18nEngineSingleImpl } from "./engine.js";
|
|
3
|
-
import type {
|
|
4
|
-
KeyDictionary,
|
|
5
|
-
LocaleFallbackMap,
|
|
6
|
-
LocaleOfSingle,
|
|
7
|
-
PartialKeyDictionary,
|
|
8
|
-
} from "./types.js";
|
|
9
|
-
import type { I18nScopeSingleForLocale, I18nScopeSingleImpl } from "./scope-single.js";
|
|
10
|
-
|
|
11
|
-
type SingleScopeBound<
|
|
12
|
-
Schema extends KeyDictionary,
|
|
13
|
-
Params,
|
|
14
|
-
Locale extends string,
|
|
15
|
-
> = I18nScopeSingleForLocale<Schema, Params & { [K in keyof Schema]: unknown }, Locale>;
|
|
16
|
-
|
|
17
|
-
export type I18nBuilderSingleOptions<
|
|
18
|
-
Schema extends KeyDictionary,
|
|
19
|
-
RequestLocales extends string = LocaleOfSingle<Schema>,
|
|
20
|
-
> = {
|
|
21
|
-
dictionaryLoader?: NamespaceLoader<PartialKeyDictionary<Schema, RequestLocales> | Schema>;
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
export interface I18nBuilderSingle<
|
|
25
|
-
Schema extends KeyDictionary,
|
|
26
|
-
Params extends { [K in keyof Schema]: unknown },
|
|
27
|
-
RequestLocales extends string = LocaleOfSingle<Schema>,
|
|
28
|
-
> {
|
|
29
|
-
withLocale<Locale extends RequestLocales>(
|
|
30
|
-
locale: Locale
|
|
31
|
-
): I18nBuilderSingleForLocaleImpl<Schema, Params, RequestLocales, Locale>;
|
|
32
|
-
|
|
33
|
-
withDeliveryArea<Area extends string>(
|
|
34
|
-
area: Area
|
|
35
|
-
): I18nBuilderSingle<Schema, Params, RequestLocales>;
|
|
36
|
-
|
|
37
|
-
load(): Promise<
|
|
38
|
-
I18nScopeSingleImpl<Schema, Params, RequestLocales, LocaleFallbackMap | undefined>
|
|
39
|
-
>;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export interface I18nBuilderSingleForLocale<
|
|
43
|
-
Schema extends KeyDictionary,
|
|
44
|
-
Params extends { [K in keyof Schema]: unknown },
|
|
45
|
-
Locale extends string,
|
|
46
|
-
> {
|
|
47
|
-
load(): Promise<SingleScopeBound<Schema, Params, Locale>>;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
type SingleBuilderState<
|
|
51
|
-
Schema extends KeyDictionary,
|
|
52
|
-
RequestLocales extends string = LocaleOfSingle<Schema>,
|
|
53
|
-
> = {
|
|
54
|
-
locale?: RequestLocales;
|
|
55
|
-
deliveryArea?: string;
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
export class I18nBuilderSingleImpl<
|
|
59
|
-
Schema extends KeyDictionary,
|
|
60
|
-
Params extends { [K in keyof Schema]: unknown },
|
|
61
|
-
RequestLocales extends string = LocaleOfSingle<Schema>,
|
|
62
|
-
> implements I18nBuilderSingle<Schema, Params, RequestLocales> {
|
|
63
|
-
private state: SingleBuilderState<Schema, RequestLocales> = {};
|
|
64
|
-
|
|
65
|
-
constructor(
|
|
66
|
-
private readonly engine: I18nEngineSingleImpl<Schema, Params, RequestLocales>,
|
|
67
|
-
private readonly options?: I18nBuilderSingleOptions<Schema, RequestLocales>,
|
|
68
|
-
state?: SingleBuilderState<Schema, RequestLocales>
|
|
69
|
-
) {
|
|
70
|
-
if (state !== undefined) {
|
|
71
|
-
this.state = {
|
|
72
|
-
...(state.locale !== undefined ? { locale: state.locale } : {}),
|
|
73
|
-
...(state.deliveryArea !== undefined ? { deliveryArea: state.deliveryArea } : {}),
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
private clone(): SingleBuilderState<Schema, RequestLocales> {
|
|
79
|
-
return {
|
|
80
|
-
...(this.state.locale !== undefined ? { locale: this.state.locale } : {}),
|
|
81
|
-
...(this.state.deliveryArea !== undefined ? { deliveryArea: this.state.deliveryArea } : {}),
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
withLocale<Locale extends RequestLocales>(
|
|
86
|
-
locale: Locale
|
|
87
|
-
): I18nBuilderSingleForLocaleImpl<Schema, Params, RequestLocales, Locale> {
|
|
88
|
-
const { deliveryArea: _deliveryArea, ...rest } = this.clone();
|
|
89
|
-
return new I18nBuilderSingleForLocaleImpl<Schema, Params, RequestLocales, Locale>(
|
|
90
|
-
this.engine,
|
|
91
|
-
this.options,
|
|
92
|
-
{ ...rest, locale }
|
|
93
|
-
);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
withDeliveryArea<Area extends string>(
|
|
97
|
-
_area: Area
|
|
98
|
-
): I18nBuilderSingle<Schema, Params, RequestLocales> {
|
|
99
|
-
const { locale: _locale, ...rest } = this.clone();
|
|
100
|
-
return new I18nBuilderSingleImpl(this.engine, this.options, {
|
|
101
|
-
...rest,
|
|
102
|
-
deliveryArea: _area,
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
async load(): Promise<
|
|
107
|
-
I18nScopeSingleImpl<Schema, Params, RequestLocales, LocaleFallbackMap | undefined>
|
|
108
|
-
> {
|
|
109
|
-
await applySingleBuilderLoad(this.engine, this.options, this.state);
|
|
110
|
-
return this.engine.toScope();
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
export class I18nBuilderSingleForLocaleImpl<
|
|
115
|
-
Schema extends KeyDictionary,
|
|
116
|
-
Params extends { [K in keyof Schema]: unknown },
|
|
117
|
-
RequestLocales extends string = LocaleOfSingle<Schema>,
|
|
118
|
-
Locale extends RequestLocales = RequestLocales,
|
|
119
|
-
> implements I18nBuilderSingleForLocale<Schema, Params, Locale> {
|
|
120
|
-
constructor(
|
|
121
|
-
private readonly engine: I18nEngineSingleImpl<Schema, Params, RequestLocales>,
|
|
122
|
-
private readonly options: I18nBuilderSingleOptions<Schema, RequestLocales> | undefined,
|
|
123
|
-
private readonly state: SingleBuilderState<Schema, RequestLocales> & { locale: Locale }
|
|
124
|
-
) {}
|
|
125
|
-
|
|
126
|
-
async load(): Promise<SingleScopeBound<Schema, Params, Locale>> {
|
|
127
|
-
await applySingleBuilderLoad(this.engine, this.options, this.state);
|
|
128
|
-
return this.engine.toScope({ locale: this.state.locale });
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
async function applySingleBuilderLoad<
|
|
133
|
-
Schema extends KeyDictionary,
|
|
134
|
-
Params extends { [K in keyof Schema]: unknown },
|
|
135
|
-
RequestLocales extends string,
|
|
136
|
-
>(
|
|
137
|
-
engine: I18nEngineSingleImpl<Schema, Params, RequestLocales>,
|
|
138
|
-
options: I18nBuilderSingleOptions<Schema, RequestLocales> | undefined,
|
|
139
|
-
state: SingleBuilderState<Schema, RequestLocales>
|
|
140
|
-
): Promise<void> {
|
|
141
|
-
const partition = state.locale ?? state.deliveryArea;
|
|
142
|
-
const loader = options?.dictionaryLoader;
|
|
143
|
-
|
|
144
|
-
if (loader !== undefined) {
|
|
145
|
-
if (engine.hasBuilderResourceLoaded(partition)) {
|
|
146
|
-
return;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
const data = await invokeNamespaceLoader(loader, partition);
|
|
150
|
-
engine.applyLoadMergeSingle(data);
|
|
151
|
-
engine.markBuilderResourceLoaded(partition);
|
|
152
|
-
}
|
|
153
|
-
}
|