@kizunajs/i18n 0.1.1 → 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 +94 -48
- package/dist/bin.cjs +3 -3
- package/dist/bin.mjs +3 -3
- package/dist/generate.cjs +96 -85
- package/dist/generate.d.cts +26 -15
- package/dist/generate.d.mts +26 -15
- package/dist/generate.mjs +95 -84
- package/dist/index.cjs +0 -72
- package/dist/index.d.cts +2 -150
- package/dist/index.d.mts +2 -150
- package/dist/index.mjs +1 -71
- package/dist/{load-dVzjsBK6.cjs → load-CdaJ2I49.cjs} +165 -19
- package/dist/{load-CE9FYddD.mjs → load-EiVgmBB9.mjs} +153 -19
- 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-BrYHodVt.mjs +0 -11
- package/dist/source-CAF21cuM.d.cts +0 -35
- package/dist/source-CDwD6Wcn.cjs +0 -22
- package/dist/source-DJMnQ2hS.d.mts +0 -35
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { n as messagesSource } from "./source-BrYHodVt.mjs";
|
|
2
1
|
import { readdirSync } from "node:fs";
|
|
3
2
|
import { join, relative } from "node:path";
|
|
4
3
|
import { createJiti } from "jiti";
|
|
@@ -81,6 +80,7 @@ function renderedOf(segments, examples) {
|
|
|
81
80
|
}
|
|
82
81
|
const braced = (names) => names.map((name) => `{${name}}`).join(" and ");
|
|
83
82
|
const angled = (names) => names.map((name) => `<${name}>`).join(" and ");
|
|
83
|
+
const examplesOf = (definition) => Object.fromEntries(Object.entries(definition.params).flatMap(([name, param]) => param.example === void 0 ? [] : [[name, String(param.example)]]));
|
|
84
84
|
function checkText(text, definition, params, tags) {
|
|
85
85
|
const names = namesOf(parseText(text));
|
|
86
86
|
const extra = names.params.filter((name) => !params.includes(name));
|
|
@@ -94,7 +94,7 @@ function checkText(text, definition, params, tags) {
|
|
|
94
94
|
if (extraTags.length > 0) problems.push(`uses ${angled(extraTags)}, which the message does not have`);
|
|
95
95
|
if (missingTags.length > 0) problems.push(`leaves out ${angled(missingTags)}`);
|
|
96
96
|
if (definition.maxLength !== void 0) {
|
|
97
|
-
const length = lengthOf(renderedOf(parseText(text), definition
|
|
97
|
+
const length = lengthOf(renderedOf(parseText(text), examplesOf(definition)));
|
|
98
98
|
if (length > definition.maxLength) {
|
|
99
99
|
const filled = names.params.length > 0 ? " with the examples filled in" : "";
|
|
100
100
|
problems.push(`is ${length} characters${filled}, the limit is ${definition.maxLength}`);
|
|
@@ -117,18 +117,17 @@ function shownLocaleOf(source, translations, locale) {
|
|
|
117
117
|
}
|
|
118
118
|
return source.base;
|
|
119
119
|
}
|
|
120
|
-
const paramsOf = (definition, base) => namesOf(parseText(definition.translations[base] ?? "")).params;
|
|
121
120
|
const tagsOf = (definition, base) => namesOf(parseText(definition.translations[base] ?? "")).tags;
|
|
122
121
|
/**
|
|
123
|
-
* Lists every problem in the messages: a missing language, a
|
|
124
|
-
* length limit, and each language that shows its fallback's text.
|
|
122
|
+
* Lists every problem in the messages: a missing language, a param the message does not declare, a
|
|
123
|
+
* wrong tag, a text over its length limit, and each language that shows its fallback's text.
|
|
125
124
|
*/
|
|
126
125
|
function problemsOf(source) {
|
|
127
126
|
const names = source.locales.map((locale) => locale.name);
|
|
128
127
|
const problems = [];
|
|
129
|
-
for (const
|
|
130
|
-
const key =
|
|
131
|
-
const params =
|
|
128
|
+
for (const { path, definition } of source.messages) {
|
|
129
|
+
const key = path.join(".");
|
|
130
|
+
const params = Object.keys(definition.params);
|
|
132
131
|
const tags = tagsOf(definition, source.base);
|
|
133
132
|
const add = (locale, message, severity = "error") => problems.push({
|
|
134
133
|
key,
|
|
@@ -136,7 +135,6 @@ function problemsOf(source) {
|
|
|
136
135
|
message,
|
|
137
136
|
severity
|
|
138
137
|
});
|
|
139
|
-
for (const name of Object.keys(definition.examples ?? {})) if (!params.includes(name)) add(source.base, `has an example for {${name}}, which the message does not have`);
|
|
140
138
|
for (const locale of Object.keys(definition.translations)) if (!names.includes(locale)) add(locale, "is not one of the languages");
|
|
141
139
|
for (const locale of source.locales) {
|
|
142
140
|
const text = definition.translations[locale.name];
|
|
@@ -166,15 +164,18 @@ function toCatalog(source) {
|
|
|
166
164
|
const errors = problemsOf(source).filter((problem) => problem.severity === "error");
|
|
167
165
|
if (errors.length > 0) throw new Error(`The messages have ${errors.length === 1 ? "a problem" : `${errors.length} problems`}:\n${formatProblems(errors)}`);
|
|
168
166
|
const locales = [...source.locales.filter((locale) => locale.name === source.base), ...source.locales.filter((locale) => locale.name !== source.base)];
|
|
169
|
-
const messages =
|
|
167
|
+
const messages = source.messages.map(({ path, definition }) => {
|
|
170
168
|
const segments = parseText(definition.translations[source.base] ?? "");
|
|
171
169
|
return {
|
|
172
|
-
key:
|
|
173
|
-
path
|
|
170
|
+
key: path.join("."),
|
|
171
|
+
path,
|
|
174
172
|
...definition.description === void 0 ? {} : { description: definition.description },
|
|
175
173
|
...definition.maxLength === void 0 ? {} : { maxLength: definition.maxLength },
|
|
176
|
-
|
|
177
|
-
|
|
174
|
+
params: Object.entries(definition.params).map(([name, param]) => ({
|
|
175
|
+
name,
|
|
176
|
+
...param
|
|
177
|
+
})),
|
|
178
|
+
tags: namesOf(segments).tags,
|
|
178
179
|
translations: Object.fromEntries(locales.filter((locale) => definition.translations[locale.name] !== void 0).map((locale) => {
|
|
179
180
|
const text = definition.translations[locale.name];
|
|
180
181
|
return [locale.name, {
|
|
@@ -183,7 +184,7 @@ function toCatalog(source) {
|
|
|
183
184
|
}];
|
|
184
185
|
}))
|
|
185
186
|
};
|
|
186
|
-
})
|
|
187
|
+
});
|
|
187
188
|
return {
|
|
188
189
|
format: 1,
|
|
189
190
|
base: source.base,
|
|
@@ -192,6 +193,136 @@ function toCatalog(source) {
|
|
|
192
193
|
};
|
|
193
194
|
}
|
|
194
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
|
|
195
326
|
//#region src/load.ts
|
|
196
327
|
const MODULE = /\.(ts|tsx|mts|cts|js|mjs|cjs)$/;
|
|
197
328
|
const SKIPPED = /\.(test|spec)\.|\.d\.(ts|mts|cts)$/;
|
|
@@ -202,7 +333,7 @@ const languagesOf = (group) => JSON.stringify({
|
|
|
202
333
|
});
|
|
203
334
|
/**
|
|
204
335
|
* Builds an app's messages from its groups, by name. Throws when two groups were made by
|
|
205
|
-
* different
|
|
336
|
+
* different configs.
|
|
206
337
|
*/
|
|
207
338
|
function sourceOf(groups) {
|
|
208
339
|
const definitions = Object.entries(groups).flatMap(([name, value]) => {
|
|
@@ -211,11 +342,14 @@ function sourceOf(groups) {
|
|
|
211
342
|
});
|
|
212
343
|
const first = definitions[0]?.[1];
|
|
213
344
|
if (!first) throw new Error("There are no messages. Export a group made by i18n.messages({ ... }).");
|
|
214
|
-
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.`);
|
|
215
346
|
return {
|
|
216
347
|
base: first.base,
|
|
217
348
|
locales: first.locales,
|
|
218
|
-
|
|
349
|
+
messages: definitions.flatMap(([name, group]) => group.messages.map((message) => ({
|
|
350
|
+
path: [name, ...message.path],
|
|
351
|
+
definition: message.definition
|
|
352
|
+
})))
|
|
219
353
|
};
|
|
220
354
|
}
|
|
221
355
|
function filesIn(folder) {
|
|
@@ -252,4 +386,4 @@ async function loadMessages(folder) {
|
|
|
252
386
|
return sourceOf(groups);
|
|
253
387
|
}
|
|
254
388
|
//#endregion
|
|
255
|
-
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 };
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
//#region src/source.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Where a message made by `i18n.message` keeps its definition.
|
|
4
|
+
*/
|
|
5
|
+
declare const messageSource: unique symbol;
|
|
6
|
+
/**
|
|
7
|
+
* Where a group made by `i18n.messages` keeps its messages.
|
|
8
|
+
*/
|
|
9
|
+
declare const messagesSource: unique symbol;
|
|
10
|
+
/**
|
|
11
|
+
* Where a param made by `i18n.string` or `i18n.number` keeps its definition.
|
|
12
|
+
*/
|
|
13
|
+
declare const paramSource: unique symbol;
|
|
14
|
+
/**
|
|
15
|
+
* One param as `i18n.string` or `i18n.number` declares it.
|
|
16
|
+
*/
|
|
17
|
+
type ParamDefinition = {
|
|
18
|
+
type: 'string';
|
|
19
|
+
example?: string;
|
|
20
|
+
} | {
|
|
21
|
+
type: 'number';
|
|
22
|
+
example?: number;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* A language, and the language it falls back to.
|
|
26
|
+
*/
|
|
27
|
+
type LocaleDefinition = {
|
|
28
|
+
name: string;
|
|
29
|
+
fallback?: string;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* One message as `i18n.message` declares it and `.translations` fills it in.
|
|
33
|
+
*/
|
|
34
|
+
type MessageDefinition = {
|
|
35
|
+
translations: Record<string, string>;
|
|
36
|
+
description?: string;
|
|
37
|
+
maxLength?: number;
|
|
38
|
+
params: Record<string, ParamDefinition>;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* One message with its path, like `['cart', 'title']`.
|
|
42
|
+
*/
|
|
43
|
+
type SourceMessage = {
|
|
44
|
+
path: string[];
|
|
45
|
+
definition: MessageDefinition;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Every message of an app, group by group, with the languages they were made for.
|
|
49
|
+
*/
|
|
50
|
+
type MessagesSource = {
|
|
51
|
+
base: string;
|
|
52
|
+
locales: LocaleDefinition[];
|
|
53
|
+
messages: SourceMessage[];
|
|
54
|
+
};
|
|
55
|
+
//#endregion
|
|
56
|
+
//#region src/types.d.ts
|
|
57
|
+
/**
|
|
58
|
+
* What a language may set in `defineConfig`.
|
|
59
|
+
*/
|
|
60
|
+
type LocaleOptions = {
|
|
61
|
+
/**
|
|
62
|
+
* The language whose text shows for a message this language leaves out.
|
|
63
|
+
*/
|
|
64
|
+
fallback?: string;
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* The languages of an app, and its base language.
|
|
68
|
+
*/
|
|
69
|
+
type Languages<Locales extends Record<string, LocaleOptions>, Base extends keyof Locales & string> = {
|
|
70
|
+
/**
|
|
71
|
+
* The base language. Every other language is checked against its tags.
|
|
72
|
+
*/
|
|
73
|
+
base: Base;
|
|
74
|
+
/**
|
|
75
|
+
* Every language, by its BCP 47 tag, like `en` or `pt-BR`.
|
|
76
|
+
*/
|
|
77
|
+
locales: Locales & { [Locale in keyof Locales]: Locale extends Base ? {
|
|
78
|
+
fallback?: never;
|
|
79
|
+
} : {
|
|
80
|
+
fallback?: Exclude<keyof Locales & string, Locale>;
|
|
81
|
+
} };
|
|
82
|
+
};
|
|
83
|
+
type Trim<Text extends string> = Text extends ` ${infer Rest}` ? Trim<Rest> : Text extends `${infer Rest} ` ? Trim<Rest> : Text;
|
|
84
|
+
type LastPart<Text extends string> = Text extends `${string}{${infer Rest}` ? LastPart<Rest> : Text;
|
|
85
|
+
type ParamName<Inside extends string> = Trim<LastPart<Inside>> extends infer Name extends string ? (Name extends '' | `${string} ${string}` ? never : Name) : never;
|
|
86
|
+
/**
|
|
87
|
+
* The `{param}` names in a text, as a union.
|
|
88
|
+
*/
|
|
89
|
+
type ParamsIn<Text> = Text extends `${string}{${infer Inside}}${infer Rest}` ? ParamName<Inside> | ParamsIn<Rest> : never;
|
|
90
|
+
/**
|
|
91
|
+
* The `<tag>` names in a text, as a union, nested tags included.
|
|
92
|
+
*/
|
|
93
|
+
type TagsIn<Text> = Text extends `${string}<${infer Tag}>${infer Inner}</${infer Tag}>${infer Rest}` ? Tag | TagsIn<Inner> | TagsIn<Rest> : never;
|
|
94
|
+
type RequiredLocalesOf<Locales> = { [Locale in keyof Locales]-?: Locales[Locale] extends {
|
|
95
|
+
fallback: string;
|
|
96
|
+
} ? never : Locale }[keyof Locales] & string;
|
|
97
|
+
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;
|
|
98
|
+
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>>}}`;
|
|
99
|
+
type LengthOf<Text extends string, Count extends unknown[] = []> = Text extends `${string}${infer Rest}` ? LengthOf<Rest, [...Count, unknown]> : Count['length'];
|
|
100
|
+
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;
|
|
101
|
+
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;
|
|
102
|
+
type BaseTextOf<Texts, Base extends string> = Base extends keyof Texts ? Texts[Base] : never;
|
|
103
|
+
type ParamsOf<Declaration> = Declaration extends {
|
|
104
|
+
params: infer Params;
|
|
105
|
+
} ? keyof Params & string : never;
|
|
106
|
+
type LimitOf<Declaration> = Declaration extends {
|
|
107
|
+
maxLength: infer Limit extends number;
|
|
108
|
+
} ? Limit : never;
|
|
109
|
+
/**
|
|
110
|
+
* Checks every language of a message against its declared params, the base language's tags and
|
|
111
|
+
* its length limit. A wrong text becomes the sentence that says what is wrong, so the compiler
|
|
112
|
+
* shows it on that text.
|
|
113
|
+
*/
|
|
114
|
+
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 };
|
|
115
|
+
/**
|
|
116
|
+
* A text param made by `i18n.string`.
|
|
117
|
+
*/
|
|
118
|
+
type StringParam = {
|
|
119
|
+
/**
|
|
120
|
+
* Sets the value your editor shows in place of this param.
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* i18n.string().example('Ada');
|
|
124
|
+
*/
|
|
125
|
+
example: (value: string) => StringParam;
|
|
126
|
+
readonly [paramSource]: unknown;
|
|
127
|
+
};
|
|
128
|
+
/**
|
|
129
|
+
* A number param made by `i18n.number`.
|
|
130
|
+
*/
|
|
131
|
+
type NumberParam = {
|
|
132
|
+
/**
|
|
133
|
+
* Sets the value your editor shows in place of this param.
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* i18n.number().example(3);
|
|
137
|
+
*/
|
|
138
|
+
example: (value: number) => NumberParam;
|
|
139
|
+
readonly [paramSource]: unknown;
|
|
140
|
+
};
|
|
141
|
+
/**
|
|
142
|
+
* What a message is: what it is for, its length limit and its params.
|
|
143
|
+
*/
|
|
144
|
+
type MessageDeclaration = {
|
|
145
|
+
/**
|
|
146
|
+
* Tells whoever translates the message what it is for.
|
|
147
|
+
*/
|
|
148
|
+
description?: string;
|
|
149
|
+
/**
|
|
150
|
+
* The most characters the message may have, in every language.
|
|
151
|
+
*/
|
|
152
|
+
maxLength?: number;
|
|
153
|
+
/**
|
|
154
|
+
* The params every language uses, like `{firstName}`.
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* params: {
|
|
158
|
+
* firstName: i18n.string().example('Ada'),
|
|
159
|
+
* },
|
|
160
|
+
*/
|
|
161
|
+
params?: Record<string, StringParam | NumberParam>;
|
|
162
|
+
};
|
|
163
|
+
/**
|
|
164
|
+
* A message that is declared and waits for its texts.
|
|
165
|
+
*/
|
|
166
|
+
type DeclaredMessage<Declaration, Locales, Base extends string> = {
|
|
167
|
+
/**
|
|
168
|
+
* Gives the message one text per language.
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* i18n.message().translations({
|
|
172
|
+
* en: 'Your cart',
|
|
173
|
+
* nb: 'Handlekurven din',
|
|
174
|
+
* });
|
|
175
|
+
*/
|
|
176
|
+
translations: <const Texts extends Record<string, string>>(texts: CheckTexts<Texts, Locales, Base, ParamsOf<Declaration>, LimitOf<Declaration>>) => Message;
|
|
177
|
+
};
|
|
178
|
+
/**
|
|
179
|
+
* A message made by `i18n.message`.
|
|
180
|
+
*/
|
|
181
|
+
type Message = {
|
|
182
|
+
readonly [messageSource]: unknown;
|
|
183
|
+
};
|
|
184
|
+
/**
|
|
185
|
+
* Messages by name. A plain object inside is a nested group, like `t.checkout.form.email`.
|
|
186
|
+
*/
|
|
187
|
+
type MessageTree = {
|
|
188
|
+
[name: string]: Message | MessageTree;
|
|
189
|
+
};
|
|
190
|
+
/**
|
|
191
|
+
* A group of messages made by `i18n.messages`. `kizuna-i18n generate` reads every group the
|
|
192
|
+
* translations folder exports, named by its export.
|
|
193
|
+
*/
|
|
194
|
+
type Messages = {
|
|
195
|
+
readonly [messagesSource]: unknown;
|
|
196
|
+
};
|
|
197
|
+
/**
|
|
198
|
+
* What `defineConfig` returns, which declares the messages.
|
|
199
|
+
*/
|
|
200
|
+
type I18n<Locales extends Record<string, LocaleOptions>, Base extends keyof Locales & string> = {
|
|
201
|
+
/**
|
|
202
|
+
* Declares a message. `.translations` then gives it one text per language.
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
* i18n
|
|
206
|
+
* .message({
|
|
207
|
+
* description: 'Shown above the cart to a customer who is logged in',
|
|
208
|
+
* params: {
|
|
209
|
+
* firstName: i18n.string().example('Ada'),
|
|
210
|
+
* },
|
|
211
|
+
* })
|
|
212
|
+
* .translations({
|
|
213
|
+
* en: 'Welcome back, {firstName}',
|
|
214
|
+
* nb: 'Velkommen tilbake, {firstName}',
|
|
215
|
+
* });
|
|
216
|
+
*/
|
|
217
|
+
message: <const Declaration extends MessageDeclaration = {}>(declaration?: Declaration) => DeclaredMessage<Declaration, Locales, Base>;
|
|
218
|
+
/**
|
|
219
|
+
* Declares a text param.
|
|
220
|
+
*
|
|
221
|
+
* @example
|
|
222
|
+
* i18n.string().example('Ada');
|
|
223
|
+
*/
|
|
224
|
+
string: () => StringParam;
|
|
225
|
+
/**
|
|
226
|
+
* Declares a number param.
|
|
227
|
+
*
|
|
228
|
+
* @example
|
|
229
|
+
* i18n.number().example(3);
|
|
230
|
+
*/
|
|
231
|
+
number: () => NumberParam;
|
|
232
|
+
/**
|
|
233
|
+
* Groups messages. Each key names a message, like `t.cart.title`, and a plain object nests a
|
|
234
|
+
* group, like `t.cart.summary.total`.
|
|
235
|
+
*
|
|
236
|
+
* @example
|
|
237
|
+
* export const cart = i18n.messages({
|
|
238
|
+
* title: i18n.message().translations({
|
|
239
|
+
* en: 'Your cart',
|
|
240
|
+
* nb: 'Handlekurven din',
|
|
241
|
+
* }),
|
|
242
|
+
* summary: {
|
|
243
|
+
* total: i18n.message().translations({
|
|
244
|
+
* en: 'Total',
|
|
245
|
+
* nb: 'Totalt',
|
|
246
|
+
* }),
|
|
247
|
+
* },
|
|
248
|
+
* });
|
|
249
|
+
*/
|
|
250
|
+
messages: (messages: MessageTree) => Messages;
|
|
251
|
+
};
|
|
252
|
+
//#endregion
|
|
253
|
+
export { Messages as a, ParamDefinition as c, Message as i, Languages as n, LocaleDefinition as o, LocaleOptions as r, MessagesSource as s, I18n as t };
|