@xndrjs/i18n 0.6.0 → 0.7.0-alpha.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.
Files changed (51) hide show
  1. package/README.md +154 -134
  2. package/dist/codegen/index.js.map +1 -1
  3. package/dist/index.d.ts +276 -61
  4. package/dist/index.js +687 -126
  5. package/dist/index.js.map +1 -1
  6. package/dist/validation/index.d.ts +23 -7
  7. package/dist/validation/index.js +196 -89
  8. package/dist/validation/index.js.map +1 -1
  9. package/package.json +1 -1
  10. package/src/IcuTranslationProviderMulti.test.ts +115 -215
  11. package/src/IcuTranslationProviderMulti.ts +107 -96
  12. package/src/IcuTranslationProviderSingle.test.ts +36 -294
  13. package/src/IcuTranslationProviderSingle.ts +55 -72
  14. package/src/builder-load-registry.ts +18 -0
  15. package/src/builder-loaders.ts +18 -0
  16. package/src/builder-multi.ts +481 -0
  17. package/src/builder-types.test.ts +80 -0
  18. package/src/builder-types.ts +24 -0
  19. package/src/builder.test.ts +421 -0
  20. package/src/builder.ts +112 -0
  21. package/src/codegen/delivery-artifacts.test.ts +2 -1
  22. package/src/codegen/delivery-artifacts.ts +2 -1
  23. package/src/codegen/emit/dictionary-file.test.ts +0 -7
  24. package/src/codegen/emit/dictionary-schema-file.ts +55 -42
  25. package/src/codegen/emit/instance-file.test.ts +88 -0
  26. package/src/codegen/emit/instance-file.ts +164 -17
  27. package/src/codegen/emit/namespace-loaders-file.test.ts +35 -10
  28. package/src/codegen/emit/namespace-loaders-file.ts +11 -61
  29. package/src/codegen/emit/types-file.test.ts +0 -2
  30. package/src/codegen/generate-i18n-types.test.ts +8 -303
  31. package/src/codegen/generate-i18n-types.ts +16 -1
  32. package/src/codegen/project-locales-set-namespace.test.ts +25 -32
  33. package/src/engine.ts +71 -0
  34. package/src/index.ts +47 -7
  35. package/src/patch-key.test.ts +186 -0
  36. package/src/patch-key.ts +140 -0
  37. package/src/project-locales.test.ts +76 -0
  38. package/src/project-locales.ts +58 -1
  39. package/src/scope-multi.ts +124 -0
  40. package/src/scope-single.ts +85 -0
  41. package/src/scope-types.ts +27 -0
  42. package/src/scope.test.ts +481 -0
  43. package/src/single-builder.ts +153 -0
  44. package/src/types.ts +16 -0
  45. package/src/validation/create-normalized-schema.ts +1 -1
  46. package/src/validation/errors.ts +2 -0
  47. package/src/validation/index.ts +135 -27
  48. package/src/validation/normalize.ts +176 -46
  49. package/src/validation/types.ts +4 -0
  50. package/src/validation/validate-normalized.ts +43 -1
  51. package/src/validation/validation.test.ts +200 -7
@@ -0,0 +1,124 @@
1
+ import type { IcuTranslationProviderMulti } from "./IcuTranslationProviderMulti.js";
2
+ import type { LocaleFallbackMap, LocaleOfMulti, MultiDictionary } from "./types.js";
3
+ import type { MultiParams, ParamArgs } from "./scope-types.js";
4
+
5
+ /** Type-safe translation scope for a multi-namespace schema subset. */
6
+ export interface I18nScopeMulti<
7
+ Schema extends MultiDictionary,
8
+ Params,
9
+ RequestLocales extends string = LocaleOfMulti<Schema>,
10
+ > {
11
+ t<
12
+ const NS extends keyof Schema & keyof Params & string,
13
+ const K extends keyof Schema[NS] & keyof Params[NS] & string,
14
+ >(
15
+ namespace: NS,
16
+ key: K,
17
+ locale: RequestLocales,
18
+ ...params: ParamArgs<Params[NS][K]>
19
+ ): string;
20
+ forLocale<Locale extends RequestLocales>(
21
+ locale: Locale
22
+ ): I18nScopeMultiForLocale<Schema, Params, RequestLocales, Locale>;
23
+ }
24
+
25
+ /** Multi-namespace scope with a bound locale — `t(namespace, key)` and `set(...)` without a locale argument. */
26
+ export interface I18nScopeMultiForLocale<
27
+ Schema extends MultiDictionary,
28
+ Params,
29
+ RequestLocales extends string,
30
+ Locale extends RequestLocales,
31
+ > {
32
+ readonly locale: Locale;
33
+ t<
34
+ const NS extends keyof Schema & keyof Params & string,
35
+ const K extends keyof Schema[NS] & keyof Params[NS] & string,
36
+ >(
37
+ namespace: NS,
38
+ key: K,
39
+ ...params: ParamArgs<Params[NS][K]>
40
+ ): string;
41
+ set<
42
+ const NS extends keyof Schema & keyof Params & string,
43
+ const K extends keyof Schema[NS] & keyof Params[NS] & string,
44
+ >(
45
+ namespace: NS,
46
+ key: K,
47
+ template: string
48
+ ): void;
49
+ forLocale<Next extends RequestLocales>(
50
+ locale: Next
51
+ ): I18nScopeMultiForLocale<Schema, Params, RequestLocales, Next>;
52
+ }
53
+
54
+ export class I18nScopeMultiImpl<
55
+ Schema extends MultiDictionary,
56
+ Params extends MultiParams<Schema>,
57
+ RequestLocales extends string,
58
+ Fallback extends LocaleFallbackMap | undefined,
59
+ > implements I18nScopeMulti<Schema, Params, RequestLocales> {
60
+ constructor(
61
+ private readonly engine: IcuTranslationProviderMulti<Schema, Params, RequestLocales, Fallback>
62
+ ) {}
63
+
64
+ t = <
65
+ const NS extends keyof Schema & keyof Params & string,
66
+ const K extends keyof Schema[NS] & keyof Params[NS] & string,
67
+ >(
68
+ namespace: NS,
69
+ key: K,
70
+ locale: RequestLocales,
71
+ ...args: ParamArgs<Params[NS][K]>
72
+ ): string => {
73
+ const params = args[0] as Record<string, unknown> | undefined;
74
+ return this.engine.getWithLocale(String(namespace), String(key), locale, params);
75
+ };
76
+
77
+ forLocale = <Locale extends RequestLocales>(
78
+ locale: Locale
79
+ ): I18nScopeMultiForLocaleImpl<Schema, Params, RequestLocales, Locale, Fallback> => {
80
+ return new I18nScopeMultiForLocaleImpl(this.engine, locale);
81
+ };
82
+ }
83
+
84
+ export class I18nScopeMultiForLocaleImpl<
85
+ Schema extends MultiDictionary,
86
+ Params extends MultiParams<Schema>,
87
+ RequestLocales extends string,
88
+ Locale extends RequestLocales,
89
+ Fallback extends LocaleFallbackMap | undefined,
90
+ > implements I18nScopeMultiForLocale<Schema, Params, RequestLocales, Locale> {
91
+ constructor(
92
+ private readonly engine: IcuTranslationProviderMulti<Schema, Params, RequestLocales, Fallback>,
93
+ readonly locale: Locale
94
+ ) {}
95
+
96
+ t = <
97
+ const NS extends keyof Schema & keyof Params & string,
98
+ const K extends keyof Schema[NS] & keyof Params[NS] & string,
99
+ >(
100
+ namespace: NS,
101
+ key: K,
102
+ ...args: ParamArgs<Params[NS][K]>
103
+ ): string => {
104
+ const params = args[0] as Record<string, unknown> | undefined;
105
+ return this.engine.getWithLocale(String(namespace), String(key), this.locale, params);
106
+ };
107
+
108
+ set = <
109
+ const NS extends keyof Schema & keyof Params & string,
110
+ const K extends keyof Schema[NS] & keyof Params[NS] & string,
111
+ >(
112
+ namespace: NS,
113
+ key: K,
114
+ template: string
115
+ ): void => {
116
+ this.engine.patchKeyMulti(String(namespace), String(key), this.locale, template);
117
+ };
118
+
119
+ forLocale = <Next extends RequestLocales>(
120
+ locale: Next
121
+ ): I18nScopeMultiForLocaleImpl<Schema, Params, RequestLocales, Next, Fallback> => {
122
+ return new I18nScopeMultiForLocaleImpl(this.engine, locale);
123
+ };
124
+ }
@@ -0,0 +1,85 @@
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
+ }
@@ -0,0 +1,27 @@
1
+ import type { KeyDictionary, MultiDictionary } from "./types.js";
2
+
3
+ /** ICU argument map for a multi-namespace schema. */
4
+ export type MultiParams<Schema extends MultiDictionary> = {
5
+ [NS in keyof Schema]: { [K in keyof Schema[NS]]: unknown };
6
+ };
7
+
8
+ /** Restrict a multi schema to a subset of namespaces. */
9
+ export type SchemaForNamespaces<
10
+ Schema extends MultiDictionary,
11
+ NsList extends readonly (keyof Schema & string)[],
12
+ > = Pick<Schema, NsList[number]>;
13
+
14
+ /** Restrict multi ICU params to a subset of namespaces. */
15
+ export type ParamsForNamespaces<
16
+ Schema extends MultiDictionary,
17
+ Params extends MultiParams<Schema>,
18
+ NsList extends readonly (keyof Schema & string)[],
19
+ > = Pick<Params, NsList[number]>;
20
+
21
+ /** ICU argument map for a single-namespace schema. */
22
+ export type SingleParams<Schema extends KeyDictionary> = {
23
+ [K in keyof Schema]: unknown;
24
+ };
25
+
26
+ /** Rest args for `t()` — `never` params omit the third argument. */
27
+ export type ParamArgs<P> = [P] extends [never] ? [] : [params: P];