@kizunajs/i18n 0.2.0 → 0.3.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 +21 -30
- package/dist/bin.cjs +3 -3
- package/dist/bin.mjs +3 -3
- package/dist/generate.cjs +2 -20
- package/dist/generate.d.cts +12 -6
- package/dist/generate.d.mts +12 -6
- package/dist/generate.mjs +1 -19
- package/dist/index.cjs +0 -81
- package/dist/index.d.cts +2 -214
- package/dist/index.d.mts +2 -214
- package/dist/index.mjs +1 -80
- package/dist/{load-Ddl3iCSC.cjs → load-CdaJ2I49.cjs} +145 -4
- package/dist/{load-CMmjOpu-.mjs → load-EiVgmBB9.mjs} +133 -4
- package/dist/types-BE4-k_kg.d.cts +253 -0
- package/dist/types-DC3XWG_J.d.mts +253 -0
- package/package.json +1 -1
- package/dist/source-B3RV-rox.cjs +0 -32
- package/dist/source-BWSeiZiF.mjs +0 -15
- package/dist/source-C6_RixNW.d.cts +0 -56
- package/dist/source-Cu0aGXpT.d.mts +0 -56
package/dist/index.d.mts
CHANGED
|
@@ -1,214 +1,2 @@
|
|
|
1
|
-
import { a as
|
|
2
|
-
|
|
3
|
-
//#region src/types.d.ts
|
|
4
|
-
/**
|
|
5
|
-
* What a language may set in `defineI18n`.
|
|
6
|
-
*/
|
|
7
|
-
type LocaleOptions = {
|
|
8
|
-
/**
|
|
9
|
-
* The language whose text shows for a message this language leaves out.
|
|
10
|
-
*/
|
|
11
|
-
fallback?: string;
|
|
12
|
-
};
|
|
13
|
-
type Trim<Text extends string> = Text extends ` ${infer Rest}` ? Trim<Rest> : Text extends `${infer Rest} ` ? Trim<Rest> : Text;
|
|
14
|
-
type LastPart<Text extends string> = Text extends `${string}{${infer Rest}` ? LastPart<Rest> : Text;
|
|
15
|
-
type ParamName<Inside extends string> = Trim<LastPart<Inside>> extends infer Name extends string ? (Name extends '' | `${string} ${string}` ? never : Name) : never;
|
|
16
|
-
/**
|
|
17
|
-
* The `{param}` names in a text, as a union.
|
|
18
|
-
*/
|
|
19
|
-
type ParamsIn<Text> = Text extends `${string}{${infer Inside}}${infer Rest}` ? ParamName<Inside> | ParamsIn<Rest> : never;
|
|
20
|
-
/**
|
|
21
|
-
* The `<tag>` names in a text, as a union, nested tags included.
|
|
22
|
-
*/
|
|
23
|
-
type TagsIn<Text> = Text extends `${string}<${infer Tag}>${infer Inner}</${infer Tag}>${infer Rest}` ? Tag | TagsIn<Inner> | TagsIn<Rest> : never;
|
|
24
|
-
type RequiredLocalesOf<Locales> = { [Locale in keyof Locales]-?: Locales[Locale] extends {
|
|
25
|
-
fallback: string;
|
|
26
|
-
} ? never : Locale }[keyof Locales] & string;
|
|
27
|
-
type CheckText<Locale extends string, Text, Params extends string, Tags extends string, Limit extends number> = Text extends string ? string extends Text ? Text : [Exclude<ParamsIn<Text>, Params>] extends [never] ? CheckLeftOut<Locale, Text, Params, Tags, Limit> : [Exclude<Params, ParamsIn<Text>>] extends [never] ? `${Locale} uses {${Exclude<ParamsIn<Text>, Params>}}, which the message does not have` : `${Locale} uses {${Exclude<ParamsIn<Text>, Params>}}, the message has {${Params}}` : string;
|
|
28
|
-
type CheckLeftOut<Locale extends string, Text extends string, Params extends string, Tags extends string, Limit extends number> = [Exclude<Params, ParamsIn<Text>>] extends [never] ? [Exclude<TagsIn<Text>, Tags>] extends [never] ? [Exclude<Tags, TagsIn<Text>>] extends [never] ? CheckLength<Locale, Text, Limit> : `${Locale} leaves out <${Exclude<Tags, TagsIn<Text>>}>` : `${Locale} uses <${Exclude<TagsIn<Text>, Tags>}>, which the message does not have` : `${Locale} leaves out {${Exclude<Params, ParamsIn<Text>>}}`;
|
|
29
|
-
type LengthOf<Text extends string, Count extends unknown[] = []> = Text extends `${string}${infer Rest}` ? LengthOf<Rest, [...Count, unknown]> : Count['length'];
|
|
30
|
-
type IsLonger<Text extends string, Limit extends number, Count extends unknown[] = []> = Count['length'] extends Limit ? Text extends '' ? false : true : Text extends `${string}${infer Rest}` ? IsLonger<Rest, Limit, [...Count, unknown]> : false;
|
|
31
|
-
type CheckLength<Locale extends string, Text extends string, Limit extends number> = [Limit] extends [never] ? Text : number extends Limit ? Text : [ParamsIn<Text>] extends [never] ? IsLonger<Text, Limit> extends true ? `${Locale} is ${LengthOf<Text>} characters, the limit is ${Limit}` : Text : Text;
|
|
32
|
-
type BaseTextOf<Texts, Base extends string> = Base extends keyof Texts ? Texts[Base] : never;
|
|
33
|
-
type ParamsOf<Declaration> = Declaration extends {
|
|
34
|
-
params: infer Params;
|
|
35
|
-
} ? keyof Params & string : never;
|
|
36
|
-
type LimitOf<Declaration> = Declaration extends {
|
|
37
|
-
maxLength: infer Limit extends number;
|
|
38
|
-
} ? Limit : never;
|
|
39
|
-
/**
|
|
40
|
-
* Checks every language of a message against its declared params, the base language's tags and
|
|
41
|
-
* its length limit. A wrong text becomes the sentence that says what is wrong, so the compiler
|
|
42
|
-
* shows it on that text.
|
|
43
|
-
*/
|
|
44
|
-
type CheckTexts<Texts, Locales, Base extends string, Params extends string, Limit extends number> = { [Locale in keyof Texts]: Locale extends keyof Locales ? CheckText<Locale & string, Texts[Locale], Params, TagsIn<BaseTextOf<Texts, Base>>, Limit> : `${Locale & string} is not one of the languages` } & { [Locale in RequiredLocalesOf<Locales>]: string };
|
|
45
|
-
/**
|
|
46
|
-
* A text param made by `i18n.string`.
|
|
47
|
-
*/
|
|
48
|
-
type StringParam = {
|
|
49
|
-
/**
|
|
50
|
-
* Gives a sample value. The generated JSDoc shows it, and `kizuna-i18n check` fills it in to
|
|
51
|
-
* check the length.
|
|
52
|
-
*
|
|
53
|
-
* @example
|
|
54
|
-
* i18n.string().example('Ada');
|
|
55
|
-
*/
|
|
56
|
-
example: (value: string) => StringParam;
|
|
57
|
-
readonly [paramSource]: unknown;
|
|
58
|
-
};
|
|
59
|
-
/**
|
|
60
|
-
* A number param made by `i18n.number`.
|
|
61
|
-
*/
|
|
62
|
-
type NumberParam = {
|
|
63
|
-
/**
|
|
64
|
-
* Gives a sample value. The generated JSDoc shows it, and `kizuna-i18n check` fills it in to
|
|
65
|
-
* check the length.
|
|
66
|
-
*
|
|
67
|
-
* @example
|
|
68
|
-
* i18n.number().example(3);
|
|
69
|
-
*/
|
|
70
|
-
example: (value: number) => NumberParam;
|
|
71
|
-
readonly [paramSource]: unknown;
|
|
72
|
-
};
|
|
73
|
-
/**
|
|
74
|
-
* What a message is: what it is for, its length limit and its params.
|
|
75
|
-
*/
|
|
76
|
-
type MessageDeclaration = {
|
|
77
|
-
/**
|
|
78
|
-
* Tells whoever translates the message what it is for.
|
|
79
|
-
*/
|
|
80
|
-
description?: string;
|
|
81
|
-
/**
|
|
82
|
-
* The most characters the message may have, in every language.
|
|
83
|
-
*/
|
|
84
|
-
maxLength?: number;
|
|
85
|
-
/**
|
|
86
|
-
* The params every language uses, like `{firstName}`.
|
|
87
|
-
*
|
|
88
|
-
* @example
|
|
89
|
-
* params: {
|
|
90
|
-
* firstName: i18n.string().example('Ada'),
|
|
91
|
-
* },
|
|
92
|
-
*/
|
|
93
|
-
params?: Record<string, StringParam | NumberParam>;
|
|
94
|
-
};
|
|
95
|
-
/**
|
|
96
|
-
* A message that is declared and waits for its texts.
|
|
97
|
-
*/
|
|
98
|
-
type DeclaredMessage<Declaration, Locales, Base extends string> = {
|
|
99
|
-
/**
|
|
100
|
-
* Gives the message one text per language.
|
|
101
|
-
*
|
|
102
|
-
* @example
|
|
103
|
-
* i18n.message().translations({
|
|
104
|
-
* en: 'Your cart',
|
|
105
|
-
* nb: 'Handlekurven din',
|
|
106
|
-
* });
|
|
107
|
-
*/
|
|
108
|
-
translations: <const Texts extends Record<string, string>>(texts: CheckTexts<Texts, Locales, Base, ParamsOf<Declaration>, LimitOf<Declaration>>) => Message;
|
|
109
|
-
};
|
|
110
|
-
/**
|
|
111
|
-
* A message made by `i18n.message`.
|
|
112
|
-
*/
|
|
113
|
-
type Message = {
|
|
114
|
-
readonly [messageSource]: unknown;
|
|
115
|
-
};
|
|
116
|
-
/**
|
|
117
|
-
* Messages by name. A plain object inside is a nested group, like `t.checkout.form.email`.
|
|
118
|
-
*/
|
|
119
|
-
type MessageTree = {
|
|
120
|
-
[name: string]: Message | MessageTree;
|
|
121
|
-
};
|
|
122
|
-
/**
|
|
123
|
-
* A group of messages made by `i18n.messages`. `kizuna-i18n generate` reads every group the
|
|
124
|
-
* translations folder exports, named by its export.
|
|
125
|
-
*/
|
|
126
|
-
type Messages = {
|
|
127
|
-
readonly [messagesSource]: unknown;
|
|
128
|
-
};
|
|
129
|
-
/**
|
|
130
|
-
* What `defineI18n` returns.
|
|
131
|
-
*/
|
|
132
|
-
type I18n<Locales extends Record<string, LocaleOptions>, Base extends keyof Locales & string> = {
|
|
133
|
-
/**
|
|
134
|
-
* Declares a message. `.translations` then gives it one text per language.
|
|
135
|
-
*
|
|
136
|
-
* @example
|
|
137
|
-
* i18n
|
|
138
|
-
* .message({
|
|
139
|
-
* description: 'Shown above the cart to a customer who is logged in',
|
|
140
|
-
* params: {
|
|
141
|
-
* firstName: i18n.string().example('Ada'),
|
|
142
|
-
* },
|
|
143
|
-
* })
|
|
144
|
-
* .translations({
|
|
145
|
-
* en: 'Welcome back, {firstName}',
|
|
146
|
-
* nb: 'Velkommen tilbake, {firstName}',
|
|
147
|
-
* });
|
|
148
|
-
*/
|
|
149
|
-
message: <const Declaration extends MessageDeclaration = {}>(declaration?: Declaration) => DeclaredMessage<Declaration, Locales, Base>;
|
|
150
|
-
/**
|
|
151
|
-
* Declares a text param.
|
|
152
|
-
*
|
|
153
|
-
* @example
|
|
154
|
-
* i18n.string().example('Ada');
|
|
155
|
-
*/
|
|
156
|
-
string: () => StringParam;
|
|
157
|
-
/**
|
|
158
|
-
* Declares a number param.
|
|
159
|
-
*
|
|
160
|
-
* @example
|
|
161
|
-
* i18n.number().example(3);
|
|
162
|
-
*/
|
|
163
|
-
number: () => NumberParam;
|
|
164
|
-
/**
|
|
165
|
-
* Groups messages. Each key names a message, like `t.cart.title`, and a plain object nests a
|
|
166
|
-
* group, like `t.cart.summary.total`.
|
|
167
|
-
*
|
|
168
|
-
* @example
|
|
169
|
-
* export const cart = i18n.messages({
|
|
170
|
-
* title: i18n.message().translations({
|
|
171
|
-
* en: 'Your cart',
|
|
172
|
-
* nb: 'Handlekurven din',
|
|
173
|
-
* }),
|
|
174
|
-
* summary: {
|
|
175
|
-
* total: i18n.message().translations({
|
|
176
|
-
* en: 'Total',
|
|
177
|
-
* nb: 'Totalt',
|
|
178
|
-
* }),
|
|
179
|
-
* },
|
|
180
|
-
* });
|
|
181
|
-
*/
|
|
182
|
-
messages: (messages: MessageTree) => Messages;
|
|
183
|
-
};
|
|
184
|
-
//#endregion
|
|
185
|
-
//#region src/define-i18n.d.ts
|
|
186
|
-
/**
|
|
187
|
-
* Declares your languages and names the base language, the one every other language is checked
|
|
188
|
-
* against. Returns `i18n`, which declares the messages.
|
|
189
|
-
*
|
|
190
|
-
* @example
|
|
191
|
-
* export const i18n = defineI18n({
|
|
192
|
-
* base: 'en',
|
|
193
|
-
* locales: {
|
|
194
|
-
* en: {},
|
|
195
|
-
* nb: {},
|
|
196
|
-
* },
|
|
197
|
-
* });
|
|
198
|
-
*/
|
|
199
|
-
declare function defineI18n<const Locales extends Record<string, LocaleOptions>, const Base extends keyof Locales & string>(config: {
|
|
200
|
-
/**
|
|
201
|
-
* The base language. Every other language is checked against its tags.
|
|
202
|
-
*/
|
|
203
|
-
base: Base;
|
|
204
|
-
/**
|
|
205
|
-
* Every language, by its BCP 47 tag, like `en` or `pt-BR`.
|
|
206
|
-
*/
|
|
207
|
-
locales: Locales & { [Locale in keyof Locales]: Locale extends Base ? {
|
|
208
|
-
fallback?: never;
|
|
209
|
-
} : {
|
|
210
|
-
fallback?: Exclude<keyof Locales & string, Locale>;
|
|
211
|
-
} };
|
|
212
|
-
}): I18n<Locales, Base>;
|
|
213
|
-
//#endregion
|
|
214
|
-
export { type I18n, type Message, type Messages, defineI18n };
|
|
1
|
+
import { a as Messages, i as Message, t as I18n } from "./types-DC3XWG_J.mjs";
|
|
2
|
+
export { type I18n, type Message, type Messages };
|
package/dist/index.mjs
CHANGED
|
@@ -1,80 +1 @@
|
|
|
1
|
-
|
|
2
|
-
//#region src/define-i18n.ts
|
|
3
|
-
function buildParam(definition) {
|
|
4
|
-
return {
|
|
5
|
-
[paramSource]: definition,
|
|
6
|
-
example: (value) => buildParam({
|
|
7
|
-
...definition,
|
|
8
|
-
example: value
|
|
9
|
-
})
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
function buildMessage(declaration, translations) {
|
|
13
|
-
const definition = {
|
|
14
|
-
translations: { ...translations },
|
|
15
|
-
params: Object.fromEntries(Object.entries(declaration.params ?? {}).map(([name, param]) => [name, param[paramSource]])),
|
|
16
|
-
...declaration.description === void 0 ? {} : { description: declaration.description },
|
|
17
|
-
...declaration.maxLength === void 0 ? {} : { maxLength: declaration.maxLength }
|
|
18
|
-
};
|
|
19
|
-
return { [messageSource]: definition };
|
|
20
|
-
}
|
|
21
|
-
function messagesOf(tree, path) {
|
|
22
|
-
return Object.entries(tree).flatMap(([name, value]) => {
|
|
23
|
-
const definition = value[messageSource];
|
|
24
|
-
return definition === void 0 ? messagesOf(value, [...path, name]) : [{
|
|
25
|
-
path: [...path, name],
|
|
26
|
-
definition
|
|
27
|
-
}];
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
function checkFallbacks(locales, base) {
|
|
31
|
-
const names = locales.map((locale) => locale.name);
|
|
32
|
-
if (!names.includes(base)) throw new Error(`The base language '${base}' is not one of the languages.`);
|
|
33
|
-
for (const locale of locales) {
|
|
34
|
-
const seen = [locale.name];
|
|
35
|
-
let current = locale;
|
|
36
|
-
while (current?.fallback !== void 0) {
|
|
37
|
-
const next = current.fallback;
|
|
38
|
-
if (!names.includes(next)) throw new Error(`'${current.name}' falls back to '${next}', which is not one of the languages.`);
|
|
39
|
-
if (seen.includes(next)) throw new Error(`'${locale.name}' falls back in a circle: ${[...seen, next].join(", ")}.`);
|
|
40
|
-
seen.push(next);
|
|
41
|
-
current = locales.find((candidate) => candidate.name === next);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Declares your languages and names the base language, the one every other language is checked
|
|
47
|
-
* against. Returns `i18n`, which declares the messages.
|
|
48
|
-
*
|
|
49
|
-
* @example
|
|
50
|
-
* export const i18n = defineI18n({
|
|
51
|
-
* base: 'en',
|
|
52
|
-
* locales: {
|
|
53
|
-
* en: {},
|
|
54
|
-
* nb: {},
|
|
55
|
-
* },
|
|
56
|
-
* });
|
|
57
|
-
*/
|
|
58
|
-
function defineI18n(config) {
|
|
59
|
-
const base = config.base;
|
|
60
|
-
const locales = Object.entries(config.locales).map(([name, options]) => options.fallback === void 0 ? { name } : {
|
|
61
|
-
name,
|
|
62
|
-
fallback: options.fallback
|
|
63
|
-
});
|
|
64
|
-
checkFallbacks(locales, base);
|
|
65
|
-
return {
|
|
66
|
-
message: (declaration = {}) => ({ translations: (texts) => buildMessage(declaration, texts) }),
|
|
67
|
-
string: () => buildParam({ type: "string" }),
|
|
68
|
-
number: () => buildParam({ type: "number" }),
|
|
69
|
-
messages: (messages) => {
|
|
70
|
-
const group = {
|
|
71
|
-
base,
|
|
72
|
-
locales,
|
|
73
|
-
messages: messagesOf(messages, [])
|
|
74
|
-
};
|
|
75
|
-
return { [messagesSource]: group };
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
//#endregion
|
|
80
|
-
export { defineI18n };
|
|
1
|
+
export {};
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
const require_source = require("./source-B3RV-rox.cjs");
|
|
2
1
|
let node_fs = require("node:fs");
|
|
3
2
|
let node_path = require("node:path");
|
|
4
3
|
let jiti = require("jiti");
|
|
@@ -194,17 +193,147 @@ function toCatalog(source) {
|
|
|
194
193
|
};
|
|
195
194
|
}
|
|
196
195
|
//#endregion
|
|
196
|
+
//#region src/source.ts
|
|
197
|
+
/**
|
|
198
|
+
* Where a message made by `i18n.message` keeps its definition.
|
|
199
|
+
*/
|
|
200
|
+
const messageSource = Symbol.for("kizuna-i18n.message");
|
|
201
|
+
/**
|
|
202
|
+
* Where a group made by `i18n.messages` keeps its messages.
|
|
203
|
+
*/
|
|
204
|
+
const messagesSource = Symbol.for("kizuna-i18n.messages");
|
|
205
|
+
/**
|
|
206
|
+
* Where a param made by `i18n.string` or `i18n.number` keeps its definition.
|
|
207
|
+
*/
|
|
208
|
+
const paramSource = Symbol.for("kizuna-i18n.param");
|
|
209
|
+
/**
|
|
210
|
+
* Where the `i18n` that `defineConfig` returns keeps the config.
|
|
211
|
+
*/
|
|
212
|
+
const configSource = Symbol.for("kizuna-i18n.config");
|
|
213
|
+
//#endregion
|
|
214
|
+
//#region src/define-i18n.ts
|
|
215
|
+
function buildParam(definition) {
|
|
216
|
+
return {
|
|
217
|
+
[paramSource]: definition,
|
|
218
|
+
example: (value) => buildParam({
|
|
219
|
+
...definition,
|
|
220
|
+
example: value
|
|
221
|
+
})
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
function buildMessage(declaration, translations) {
|
|
225
|
+
const definition = {
|
|
226
|
+
translations: { ...translations },
|
|
227
|
+
params: Object.fromEntries(Object.entries(declaration.params ?? {}).map(([name, param]) => [name, param[paramSource]])),
|
|
228
|
+
...declaration.description === void 0 ? {} : { description: declaration.description },
|
|
229
|
+
...declaration.maxLength === void 0 ? {} : { maxLength: declaration.maxLength }
|
|
230
|
+
};
|
|
231
|
+
return { [messageSource]: definition };
|
|
232
|
+
}
|
|
233
|
+
function messagesOf(tree, path) {
|
|
234
|
+
return Object.entries(tree).flatMap(([name, value]) => {
|
|
235
|
+
const definition = value[messageSource];
|
|
236
|
+
return definition === void 0 ? messagesOf(value, [...path, name]) : [{
|
|
237
|
+
path: [...path, name],
|
|
238
|
+
definition
|
|
239
|
+
}];
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
const languageNames = new Intl.DisplayNames(["en"], {
|
|
243
|
+
type: "language",
|
|
244
|
+
fallback: "none"
|
|
245
|
+
});
|
|
246
|
+
function checkTag(name) {
|
|
247
|
+
let canonical;
|
|
248
|
+
try {
|
|
249
|
+
canonical = Intl.getCanonicalLocales(name)[0];
|
|
250
|
+
} catch {
|
|
251
|
+
throw new Error(`'${name}' is not a language tag. Write one like 'en' or 'pt-BR'.`);
|
|
252
|
+
}
|
|
253
|
+
if (canonical !== name) throw new Error(`Write '${canonical}', not '${name}'.`);
|
|
254
|
+
if (languageNames.of(new Intl.Locale(name).language) === void 0) throw new Error(`'${name}' is not a known language.`);
|
|
255
|
+
}
|
|
256
|
+
function checkLocales(locales, base) {
|
|
257
|
+
const names = locales.map((locale) => locale.name);
|
|
258
|
+
for (const name of names) checkTag(name);
|
|
259
|
+
if (!names.includes(base)) throw new Error(`The base language '${base}' is not one of the languages.`);
|
|
260
|
+
for (const locale of locales) {
|
|
261
|
+
const seen = [locale.name];
|
|
262
|
+
let current = locale;
|
|
263
|
+
while (current?.fallback !== void 0) {
|
|
264
|
+
const next = current.fallback;
|
|
265
|
+
if (!names.includes(next)) throw new Error(`'${current.name}' falls back to '${next}', which is not one of the languages.`);
|
|
266
|
+
if (seen.includes(next)) throw new Error(`'${locale.name}' falls back in a circle: ${[...seen, next].join(", ")}.`);
|
|
267
|
+
seen.push(next);
|
|
268
|
+
current = locales.find((candidate) => candidate.name === next);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
function defineI18n(config) {
|
|
273
|
+
const base = config.base;
|
|
274
|
+
const locales = Object.entries(config.locales).map(([name, options]) => options.fallback === void 0 ? { name } : {
|
|
275
|
+
name,
|
|
276
|
+
fallback: options.fallback
|
|
277
|
+
});
|
|
278
|
+
checkLocales(locales, base);
|
|
279
|
+
return {
|
|
280
|
+
message: (declaration = {}) => ({ translations: (texts) => buildMessage(declaration, texts) }),
|
|
281
|
+
string: () => buildParam({ type: "string" }),
|
|
282
|
+
number: () => buildParam({ type: "number" }),
|
|
283
|
+
messages: (messages) => {
|
|
284
|
+
const group = {
|
|
285
|
+
base,
|
|
286
|
+
locales,
|
|
287
|
+
messages: messagesOf(messages, [])
|
|
288
|
+
};
|
|
289
|
+
return { [messagesSource]: group };
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
//#endregion
|
|
294
|
+
//#region src/config.ts
|
|
295
|
+
/**
|
|
296
|
+
* Declares your languages, where the messages are, and what `kizuna-i18n generate` writes from
|
|
297
|
+
* them. Export it as the default, and import it in message files as `i18n`.
|
|
298
|
+
*
|
|
299
|
+
* @example
|
|
300
|
+
* export default defineConfig({
|
|
301
|
+
* base: 'en',
|
|
302
|
+
* locales: {
|
|
303
|
+
* en: {},
|
|
304
|
+
* nb: {},
|
|
305
|
+
* },
|
|
306
|
+
* messages: './translations',
|
|
307
|
+
* targets: [
|
|
308
|
+
* typescriptTarget({
|
|
309
|
+
* output: './src/translations.generated.ts',
|
|
310
|
+
* }),
|
|
311
|
+
* ],
|
|
312
|
+
* });
|
|
313
|
+
*/
|
|
314
|
+
function defineConfig(config) {
|
|
315
|
+
return Object.assign(defineI18n(config), { [configSource]: config });
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Returns the config an `i18n.config.ts` exports by default.
|
|
319
|
+
*/
|
|
320
|
+
function configOf(exported) {
|
|
321
|
+
const config = typeof exported === "object" && exported !== null ? exported[configSource] : void 0;
|
|
322
|
+
if (config === void 0) throw new Error("The config does not export defineConfig({ ... }) as its default.");
|
|
323
|
+
return config;
|
|
324
|
+
}
|
|
325
|
+
//#endregion
|
|
197
326
|
//#region src/load.ts
|
|
198
327
|
const MODULE = /\.(ts|tsx|mts|cts|js|mjs|cjs)$/;
|
|
199
328
|
const SKIPPED = /\.(test|spec)\.|\.d\.(ts|mts|cts)$/;
|
|
200
|
-
const groupOf = (value) => typeof value === "object" && value !== null ? value[
|
|
329
|
+
const groupOf = (value) => typeof value === "object" && value !== null ? value[messagesSource] : void 0;
|
|
201
330
|
const languagesOf = (group) => JSON.stringify({
|
|
202
331
|
base: group.base,
|
|
203
332
|
locales: group.locales
|
|
204
333
|
});
|
|
205
334
|
/**
|
|
206
335
|
* Builds an app's messages from its groups, by name. Throws when two groups were made by
|
|
207
|
-
* different
|
|
336
|
+
* different configs.
|
|
208
337
|
*/
|
|
209
338
|
function sourceOf(groups) {
|
|
210
339
|
const definitions = Object.entries(groups).flatMap(([name, value]) => {
|
|
@@ -213,7 +342,7 @@ function sourceOf(groups) {
|
|
|
213
342
|
});
|
|
214
343
|
const first = definitions[0]?.[1];
|
|
215
344
|
if (!first) throw new Error("There are no messages. Export a group made by i18n.messages({ ... }).");
|
|
216
|
-
for (const [name, group] of definitions) if (languagesOf(group) !== languagesOf(first)) throw new Error(`The group '${name}' was made for other languages than the rest. Make every group with the
|
|
345
|
+
for (const [name, group] of definitions) if (languagesOf(group) !== languagesOf(first)) throw new Error(`The group '${name}' was made for other languages than the rest. Make every group with the i18n your config exports.`);
|
|
217
346
|
return {
|
|
218
347
|
base: first.base,
|
|
219
348
|
locales: first.locales,
|
|
@@ -257,6 +386,18 @@ async function loadMessages(folder) {
|
|
|
257
386
|
return sourceOf(groups);
|
|
258
387
|
}
|
|
259
388
|
//#endregion
|
|
389
|
+
Object.defineProperty(exports, "configOf", {
|
|
390
|
+
enumerable: true,
|
|
391
|
+
get: function() {
|
|
392
|
+
return configOf;
|
|
393
|
+
}
|
|
394
|
+
});
|
|
395
|
+
Object.defineProperty(exports, "defineConfig", {
|
|
396
|
+
enumerable: true,
|
|
397
|
+
get: function() {
|
|
398
|
+
return defineConfig;
|
|
399
|
+
}
|
|
400
|
+
});
|
|
260
401
|
Object.defineProperty(exports, "formatProblems", {
|
|
261
402
|
enumerable: true,
|
|
262
403
|
get: function() {
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { n as messagesSource } from "./source-BWSeiZiF.mjs";
|
|
2
1
|
import { readdirSync } from "node:fs";
|
|
3
2
|
import { join, relative } from "node:path";
|
|
4
3
|
import { createJiti } from "jiti";
|
|
@@ -194,6 +193,136 @@ function toCatalog(source) {
|
|
|
194
193
|
};
|
|
195
194
|
}
|
|
196
195
|
//#endregion
|
|
196
|
+
//#region src/source.ts
|
|
197
|
+
/**
|
|
198
|
+
* Where a message made by `i18n.message` keeps its definition.
|
|
199
|
+
*/
|
|
200
|
+
const messageSource = Symbol.for("kizuna-i18n.message");
|
|
201
|
+
/**
|
|
202
|
+
* Where a group made by `i18n.messages` keeps its messages.
|
|
203
|
+
*/
|
|
204
|
+
const messagesSource = Symbol.for("kizuna-i18n.messages");
|
|
205
|
+
/**
|
|
206
|
+
* Where a param made by `i18n.string` or `i18n.number` keeps its definition.
|
|
207
|
+
*/
|
|
208
|
+
const paramSource = Symbol.for("kizuna-i18n.param");
|
|
209
|
+
/**
|
|
210
|
+
* Where the `i18n` that `defineConfig` returns keeps the config.
|
|
211
|
+
*/
|
|
212
|
+
const configSource = Symbol.for("kizuna-i18n.config");
|
|
213
|
+
//#endregion
|
|
214
|
+
//#region src/define-i18n.ts
|
|
215
|
+
function buildParam(definition) {
|
|
216
|
+
return {
|
|
217
|
+
[paramSource]: definition,
|
|
218
|
+
example: (value) => buildParam({
|
|
219
|
+
...definition,
|
|
220
|
+
example: value
|
|
221
|
+
})
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
function buildMessage(declaration, translations) {
|
|
225
|
+
const definition = {
|
|
226
|
+
translations: { ...translations },
|
|
227
|
+
params: Object.fromEntries(Object.entries(declaration.params ?? {}).map(([name, param]) => [name, param[paramSource]])),
|
|
228
|
+
...declaration.description === void 0 ? {} : { description: declaration.description },
|
|
229
|
+
...declaration.maxLength === void 0 ? {} : { maxLength: declaration.maxLength }
|
|
230
|
+
};
|
|
231
|
+
return { [messageSource]: definition };
|
|
232
|
+
}
|
|
233
|
+
function messagesOf(tree, path) {
|
|
234
|
+
return Object.entries(tree).flatMap(([name, value]) => {
|
|
235
|
+
const definition = value[messageSource];
|
|
236
|
+
return definition === void 0 ? messagesOf(value, [...path, name]) : [{
|
|
237
|
+
path: [...path, name],
|
|
238
|
+
definition
|
|
239
|
+
}];
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
const languageNames = new Intl.DisplayNames(["en"], {
|
|
243
|
+
type: "language",
|
|
244
|
+
fallback: "none"
|
|
245
|
+
});
|
|
246
|
+
function checkTag(name) {
|
|
247
|
+
let canonical;
|
|
248
|
+
try {
|
|
249
|
+
canonical = Intl.getCanonicalLocales(name)[0];
|
|
250
|
+
} catch {
|
|
251
|
+
throw new Error(`'${name}' is not a language tag. Write one like 'en' or 'pt-BR'.`);
|
|
252
|
+
}
|
|
253
|
+
if (canonical !== name) throw new Error(`Write '${canonical}', not '${name}'.`);
|
|
254
|
+
if (languageNames.of(new Intl.Locale(name).language) === void 0) throw new Error(`'${name}' is not a known language.`);
|
|
255
|
+
}
|
|
256
|
+
function checkLocales(locales, base) {
|
|
257
|
+
const names = locales.map((locale) => locale.name);
|
|
258
|
+
for (const name of names) checkTag(name);
|
|
259
|
+
if (!names.includes(base)) throw new Error(`The base language '${base}' is not one of the languages.`);
|
|
260
|
+
for (const locale of locales) {
|
|
261
|
+
const seen = [locale.name];
|
|
262
|
+
let current = locale;
|
|
263
|
+
while (current?.fallback !== void 0) {
|
|
264
|
+
const next = current.fallback;
|
|
265
|
+
if (!names.includes(next)) throw new Error(`'${current.name}' falls back to '${next}', which is not one of the languages.`);
|
|
266
|
+
if (seen.includes(next)) throw new Error(`'${locale.name}' falls back in a circle: ${[...seen, next].join(", ")}.`);
|
|
267
|
+
seen.push(next);
|
|
268
|
+
current = locales.find((candidate) => candidate.name === next);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
function defineI18n(config) {
|
|
273
|
+
const base = config.base;
|
|
274
|
+
const locales = Object.entries(config.locales).map(([name, options]) => options.fallback === void 0 ? { name } : {
|
|
275
|
+
name,
|
|
276
|
+
fallback: options.fallback
|
|
277
|
+
});
|
|
278
|
+
checkLocales(locales, base);
|
|
279
|
+
return {
|
|
280
|
+
message: (declaration = {}) => ({ translations: (texts) => buildMessage(declaration, texts) }),
|
|
281
|
+
string: () => buildParam({ type: "string" }),
|
|
282
|
+
number: () => buildParam({ type: "number" }),
|
|
283
|
+
messages: (messages) => {
|
|
284
|
+
const group = {
|
|
285
|
+
base,
|
|
286
|
+
locales,
|
|
287
|
+
messages: messagesOf(messages, [])
|
|
288
|
+
};
|
|
289
|
+
return { [messagesSource]: group };
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
//#endregion
|
|
294
|
+
//#region src/config.ts
|
|
295
|
+
/**
|
|
296
|
+
* Declares your languages, where the messages are, and what `kizuna-i18n generate` writes from
|
|
297
|
+
* them. Export it as the default, and import it in message files as `i18n`.
|
|
298
|
+
*
|
|
299
|
+
* @example
|
|
300
|
+
* export default defineConfig({
|
|
301
|
+
* base: 'en',
|
|
302
|
+
* locales: {
|
|
303
|
+
* en: {},
|
|
304
|
+
* nb: {},
|
|
305
|
+
* },
|
|
306
|
+
* messages: './translations',
|
|
307
|
+
* targets: [
|
|
308
|
+
* typescriptTarget({
|
|
309
|
+
* output: './src/translations.generated.ts',
|
|
310
|
+
* }),
|
|
311
|
+
* ],
|
|
312
|
+
* });
|
|
313
|
+
*/
|
|
314
|
+
function defineConfig(config) {
|
|
315
|
+
return Object.assign(defineI18n(config), { [configSource]: config });
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Returns the config an `i18n.config.ts` exports by default.
|
|
319
|
+
*/
|
|
320
|
+
function configOf(exported) {
|
|
321
|
+
const config = typeof exported === "object" && exported !== null ? exported[configSource] : void 0;
|
|
322
|
+
if (config === void 0) throw new Error("The config does not export defineConfig({ ... }) as its default.");
|
|
323
|
+
return config;
|
|
324
|
+
}
|
|
325
|
+
//#endregion
|
|
197
326
|
//#region src/load.ts
|
|
198
327
|
const MODULE = /\.(ts|tsx|mts|cts|js|mjs|cjs)$/;
|
|
199
328
|
const SKIPPED = /\.(test|spec)\.|\.d\.(ts|mts|cts)$/;
|
|
@@ -204,7 +333,7 @@ const languagesOf = (group) => JSON.stringify({
|
|
|
204
333
|
});
|
|
205
334
|
/**
|
|
206
335
|
* Builds an app's messages from its groups, by name. Throws when two groups were made by
|
|
207
|
-
* different
|
|
336
|
+
* different configs.
|
|
208
337
|
*/
|
|
209
338
|
function sourceOf(groups) {
|
|
210
339
|
const definitions = Object.entries(groups).flatMap(([name, value]) => {
|
|
@@ -213,7 +342,7 @@ function sourceOf(groups) {
|
|
|
213
342
|
});
|
|
214
343
|
const first = definitions[0]?.[1];
|
|
215
344
|
if (!first) throw new Error("There are no messages. Export a group made by i18n.messages({ ... }).");
|
|
216
|
-
for (const [name, group] of definitions) if (languagesOf(group) !== languagesOf(first)) throw new Error(`The group '${name}' was made for other languages than the rest. Make every group with the
|
|
345
|
+
for (const [name, group] of definitions) if (languagesOf(group) !== languagesOf(first)) throw new Error(`The group '${name}' was made for other languages than the rest. Make every group with the i18n your config exports.`);
|
|
217
346
|
return {
|
|
218
347
|
base: first.base,
|
|
219
348
|
locales: first.locales,
|
|
@@ -257,4 +386,4 @@ async function loadMessages(folder) {
|
|
|
257
386
|
return sourceOf(groups);
|
|
258
387
|
}
|
|
259
388
|
//#endregion
|
|
260
|
-
export {
|
|
389
|
+
export { formatProblems as a, toCatalog as c, defineConfig as i, sourceOf as n, problemsOf as o, configOf as r, shownLocaleOf as s, loadMessages as t };
|