@kizunajs/i18n 0.0.0-stage → 0.1.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/LICENSE.MD +9 -0
- package/README.md +235 -2
- package/dist/bin.cjs +174 -0
- package/dist/bin.d.cts +1 -0
- package/dist/bin.d.mts +1 -0
- package/dist/bin.mjs +175 -0
- package/dist/generate.cjs +393 -0
- package/dist/generate.d.cts +262 -0
- package/dist/generate.d.mts +262 -0
- package/dist/generate.mjs +385 -0
- package/dist/index.cjs +72 -0
- package/dist/index.d.cts +150 -0
- package/dist/index.d.mts +150 -0
- package/dist/index.mjs +71 -0
- package/dist/load-CE9FYddD.mjs +255 -0
- package/dist/load-dVzjsBK6.cjs +290 -0
- package/dist/source-BrYHodVt.mjs +11 -0
- package/dist/source-CAF21cuM.d.cts +35 -0
- package/dist/source-CDwD6Wcn.cjs +22 -0
- package/dist/source-DJMnQ2hS.d.mts +35 -0
- package/package.json +85 -3
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_load = require("./load-dVzjsBK6.cjs");
|
|
3
|
+
//#region src/config.ts
|
|
4
|
+
/**
|
|
5
|
+
* Declares where the messages are, and what `kizuna-i18n generate` writes from them.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* export default defineConfig({
|
|
9
|
+
* messages: './translations',
|
|
10
|
+
* targets: [
|
|
11
|
+
* typescriptTarget({
|
|
12
|
+
* output: './src/translations.generated.ts',
|
|
13
|
+
* }),
|
|
14
|
+
* ],
|
|
15
|
+
* });
|
|
16
|
+
*/
|
|
17
|
+
function defineConfig(config) {
|
|
18
|
+
return config;
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/target.ts
|
|
22
|
+
/**
|
|
23
|
+
* Declares a target for a language: the options a config passes it, and how it turns the catalog
|
|
24
|
+
* into code. Returns the function a config lists under `targets`.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* export const csharpTarget = defineTarget({
|
|
28
|
+
* language: 'csharp',
|
|
29
|
+
* options: z.object({
|
|
30
|
+
* namespace: z.string().default('Strings'),
|
|
31
|
+
* }),
|
|
32
|
+
* generate: ({ options, catalog }) => renderCsharp(catalog, options.namespace),
|
|
33
|
+
* });
|
|
34
|
+
*/
|
|
35
|
+
function defineTarget(definition) {
|
|
36
|
+
return (input) => {
|
|
37
|
+
const { output, ...rest } = input;
|
|
38
|
+
let options = rest;
|
|
39
|
+
if (definition.options) {
|
|
40
|
+
const parsed = definition.options.safeParse(rest);
|
|
41
|
+
if (!parsed.success) {
|
|
42
|
+
const issues = parsed.error.issues.map((issue) => `${issue.path.join(".") || "options"}: ${issue.message}`);
|
|
43
|
+
throw new Error(`The ${definition.language} target has invalid options: ${issues.join("; ")}`);
|
|
44
|
+
}
|
|
45
|
+
options = parsed.data;
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
language: definition.language,
|
|
49
|
+
output,
|
|
50
|
+
render: (catalog) => definition.generate({
|
|
51
|
+
options,
|
|
52
|
+
catalog
|
|
53
|
+
})
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/runtime.ts
|
|
59
|
+
/**
|
|
60
|
+
* The code every generated TypeScript module runs, written in below its messages. It imports
|
|
61
|
+
* nothing, so an app installs nothing, and it always comes from the release that wrote the
|
|
62
|
+
* messages it reads.
|
|
63
|
+
*/
|
|
64
|
+
const runtimeSource = String.raw`/**
|
|
65
|
+
* A message with params. Text values give a string. A rich value, like a React element, gives the
|
|
66
|
+
* message as parts, which React renders directly.
|
|
67
|
+
*/
|
|
68
|
+
export type Message<Params> = {
|
|
69
|
+
(params: Params): string;
|
|
70
|
+
<Rich>(params: { [Name in keyof Params]: Params[Name] | Rich }): Array<string | Rich>;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* A message with tags. Each tag takes a renderer, which gets the text inside the tag and returns
|
|
75
|
+
* what the tag becomes, like a link.
|
|
76
|
+
*/
|
|
77
|
+
export type TaggedMessage<Params, Tags extends string> = <Result>(
|
|
78
|
+
params: { [Name in keyof Params]: Params[Name] | Result } & {
|
|
79
|
+
[Tag in Tags]: (text: string | Array<string | Result>) => Result;
|
|
80
|
+
}
|
|
81
|
+
) => Array<string | Result>;
|
|
82
|
+
|
|
83
|
+
const cache = new Map<Locale, Translations>();
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Returns the translations of a language. A language the app does not have gives the base
|
|
87
|
+
* language.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* const t = getTranslations('nb');
|
|
91
|
+
*/
|
|
92
|
+
export function getTranslations(locale: Locale): Translations {
|
|
93
|
+
const known = Object.hasOwn(translationsOf, locale) ? locale : baseLocale;
|
|
94
|
+
let translations = cache.get(known);
|
|
95
|
+
if (!translations) {
|
|
96
|
+
translations = translationsOf[known]();
|
|
97
|
+
cache.set(known, translations);
|
|
98
|
+
}
|
|
99
|
+
return translations;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const sameLanguages: Record<string, string[]> = {
|
|
103
|
+
no: ['nb'],
|
|
104
|
+
nb: ['no'],
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Returns the language the app has that best matches the user's preferred languages, or the base
|
|
109
|
+
* language. Takes an Accept-Language header or a list like navigator.languages.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* matchLocale(['nb-NO', 'en-US']);
|
|
113
|
+
*/
|
|
114
|
+
export function matchLocale(preferred: string | readonly string[]): Locale {
|
|
115
|
+
const wanted = typeof preferred === 'string' ? tagsOfHeader(preferred) : preferred;
|
|
116
|
+
const languageOf = (tag: string) => tag.toLowerCase().split('-')[0] ?? '';
|
|
117
|
+
|
|
118
|
+
for (const tag of wanted) {
|
|
119
|
+
const lower = tag.toLowerCase();
|
|
120
|
+
const language = languageOf(tag);
|
|
121
|
+
const match =
|
|
122
|
+
locales.find((locale) => locale.toLowerCase() === lower) ??
|
|
123
|
+
locales.find((locale) => lower.startsWith(locale.toLowerCase() + '-')) ??
|
|
124
|
+
locales.find((locale) => languageOf(locale) === language) ??
|
|
125
|
+
locales.find((locale) => (sameLanguages[language] ?? []).includes(languageOf(locale)));
|
|
126
|
+
if (match) {
|
|
127
|
+
return match;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return baseLocale;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function tagsOfHeader(header: string): string[] {
|
|
134
|
+
return header
|
|
135
|
+
.split(',')
|
|
136
|
+
.map((part) => {
|
|
137
|
+
const [tag = '', ...options] = part.split(';').map((piece) => piece.trim());
|
|
138
|
+
const quality = options.find((option) => option.startsWith('q='));
|
|
139
|
+
return {
|
|
140
|
+
tag,
|
|
141
|
+
quality: quality === undefined ? 1 : Number(quality.slice(2)),
|
|
142
|
+
};
|
|
143
|
+
})
|
|
144
|
+
.filter((entry) => entry.tag !== '' && entry.tag !== '*' && entry.quality > 0)
|
|
145
|
+
.sort((first, second) => second.quality - first.quality)
|
|
146
|
+
.map((entry) => entry.tag);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const TAG_PAIR = '<(\\w+)>([\\s\\S]*?)<\\/\\1>';
|
|
150
|
+
const PARAM = '\\{\\s*([A-Za-z_$][\\w$]*)\\s*\\}';
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* The marks React puts on its elements, so a part that is an element gets a key without importing
|
|
154
|
+
* React.
|
|
155
|
+
*/
|
|
156
|
+
const reactElements = new Set<symbol>([Symbol.for('react.element'), Symbol.for('react.transitional.element')]);
|
|
157
|
+
|
|
158
|
+
function text(template: string): any {
|
|
159
|
+
return (values: Record<string, unknown> = {}) => render(template, values);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function render(template: string, values: Record<string, unknown>): string | unknown[] {
|
|
163
|
+
const hasTag = new RegExp(TAG_PAIR).test(template);
|
|
164
|
+
const hasRichValue = Object.values(values).some((value) => typeof value !== 'string' && typeof value !== 'function');
|
|
165
|
+
if (!hasTag && !hasRichValue) {
|
|
166
|
+
return template.replace(new RegExp(PARAM, 'g'), (_, name: string) => String(values[name] ?? ''));
|
|
167
|
+
}
|
|
168
|
+
return keyElements(partsOf(template, values));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function partsOf(template: string, values: Record<string, unknown>): unknown[] {
|
|
172
|
+
const parts: unknown[] = [];
|
|
173
|
+
const tagPair = new RegExp(TAG_PAIR, 'g');
|
|
174
|
+
let lastIndex = 0;
|
|
175
|
+
let match: RegExpExecArray | null;
|
|
176
|
+
|
|
177
|
+
while ((match = tagPair.exec(template))) {
|
|
178
|
+
const [whole, tag = '', inner = ''] = match;
|
|
179
|
+
pushValues(parts, template.slice(lastIndex, match.index), values);
|
|
180
|
+
const renderer = values[tag];
|
|
181
|
+
if (typeof renderer === 'function') {
|
|
182
|
+
const children = partsOf(inner, values);
|
|
183
|
+
parts.push(renderer(children.every((part) => typeof part === 'string') ? children.join('') : children));
|
|
184
|
+
} else {
|
|
185
|
+
pushValues(parts, whole, values);
|
|
186
|
+
}
|
|
187
|
+
lastIndex = match.index + whole.length;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
pushValues(parts, template.slice(lastIndex), values);
|
|
191
|
+
return parts;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function pushValues(parts: unknown[], stretch: string, values: Record<string, unknown>): void {
|
|
195
|
+
const param = new RegExp(PARAM, 'g');
|
|
196
|
+
let lastIndex = 0;
|
|
197
|
+
let match: RegExpExecArray | null;
|
|
198
|
+
|
|
199
|
+
while ((match = param.exec(stretch))) {
|
|
200
|
+
pushText(parts, stretch.slice(lastIndex, match.index));
|
|
201
|
+
const value = values[match[1] ?? ''] ?? '';
|
|
202
|
+
if (typeof value === 'string') {
|
|
203
|
+
pushText(parts, value);
|
|
204
|
+
} else {
|
|
205
|
+
parts.push(value);
|
|
206
|
+
}
|
|
207
|
+
lastIndex = match.index + match[0].length;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
pushText(parts, stretch.slice(lastIndex));
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function pushText(parts: unknown[], piece: string): void {
|
|
214
|
+
if (piece === '') {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
const last = parts[parts.length - 1];
|
|
218
|
+
if (typeof last === 'string') {
|
|
219
|
+
parts[parts.length - 1] = last + piece;
|
|
220
|
+
} else {
|
|
221
|
+
parts.push(piece);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function keyElements(parts: unknown[]): unknown[] {
|
|
226
|
+
return parts.map((part, index) => {
|
|
227
|
+
const element = part as { $$typeof?: symbol; key?: unknown } | null;
|
|
228
|
+
if (typeof part !== 'object' || element === null || !reactElements.has(element.$$typeof as symbol) || element.key != null) {
|
|
229
|
+
return part;
|
|
230
|
+
}
|
|
231
|
+
const descriptors = Object.getOwnPropertyDescriptors(part);
|
|
232
|
+
descriptors.key = {
|
|
233
|
+
value: 'kizuna-i18n.' + index,
|
|
234
|
+
writable: true,
|
|
235
|
+
enumerable: true,
|
|
236
|
+
configurable: true,
|
|
237
|
+
};
|
|
238
|
+
return Object.create(Object.getPrototypeOf(part), descriptors);
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
`;
|
|
242
|
+
//#endregion
|
|
243
|
+
//#region src/typescript-target.ts
|
|
244
|
+
const IDENTIFIER = /^[A-Za-z_$][\w$]*$/;
|
|
245
|
+
const stringOf = (value) => `'${value.replace(/\\/g, "\\\\").replace(/'/g, "\\'").replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029")}'`;
|
|
246
|
+
const keyOf = (name) => IDENTIFIER.test(name) ? name : stringOf(name);
|
|
247
|
+
const commentOf = (text) => text.replace(/\*\//g, "*\\/");
|
|
248
|
+
function filledOf(segments, values) {
|
|
249
|
+
return segments.map((segment) => {
|
|
250
|
+
if (segment.type === "text") return segment.text;
|
|
251
|
+
if (segment.type === "param") return values[segment.name] ?? "";
|
|
252
|
+
return filledOf(segment.children, values);
|
|
253
|
+
}).join("");
|
|
254
|
+
}
|
|
255
|
+
function exampleOf(message, base) {
|
|
256
|
+
const access = `t.${message.path.map(keyOf).join(".")}`;
|
|
257
|
+
const baseText = message.translations[base];
|
|
258
|
+
if (message.params.length === 0 && message.tags.length === 0) return [`${access}; // ${stringOf(baseText?.source ?? "")}`];
|
|
259
|
+
const values = Object.fromEntries(message.params.map((name) => [name, message.examples?.[name] ?? "…"]));
|
|
260
|
+
const lines = [
|
|
261
|
+
`${access}({`,
|
|
262
|
+
...message.params.map((name) => ` ${keyOf(name)}: ${stringOf(values[name])},`),
|
|
263
|
+
...message.tags.map((tag) => ` ${keyOf(tag)}: (text) => text,`)
|
|
264
|
+
];
|
|
265
|
+
const result = message.tags.length === 0 ? ` // ${stringOf(filledOf(baseText?.segments ?? [], values))}` : "";
|
|
266
|
+
return [...lines, `});${result}`];
|
|
267
|
+
}
|
|
268
|
+
function docOf(message, base, indent) {
|
|
269
|
+
const paragraphs = [];
|
|
270
|
+
if (message.description !== void 0) paragraphs.push(commentOf(message.description).split("\n"));
|
|
271
|
+
if (message.maxLength !== void 0) paragraphs.push([`@maxLength ${message.maxLength}`]);
|
|
272
|
+
paragraphs.push(["@example", ...exampleOf(message, base).map(commentOf)]);
|
|
273
|
+
const body = paragraphs.flatMap((lines, index) => [...index === 0 ? [] : [""], ...lines]);
|
|
274
|
+
return [
|
|
275
|
+
`${indent}/**`,
|
|
276
|
+
...body.map((line) => line === "" ? `${indent} *` : `${indent} * ${line}`),
|
|
277
|
+
`${indent} */`
|
|
278
|
+
];
|
|
279
|
+
}
|
|
280
|
+
function typeOf(message) {
|
|
281
|
+
if (message.params.length === 0 && message.tags.length === 0) return ["string"];
|
|
282
|
+
const params = message.params.map((name) => ` ${keyOf(name)}: string;`);
|
|
283
|
+
if (message.tags.length === 0) return [
|
|
284
|
+
"Message<{",
|
|
285
|
+
...params,
|
|
286
|
+
"}>"
|
|
287
|
+
];
|
|
288
|
+
const tags = message.tags.map(stringOf).join(" | ");
|
|
289
|
+
return message.params.length === 0 ? [`TaggedMessage<{}, ${tags}>`] : [
|
|
290
|
+
"TaggedMessage<{",
|
|
291
|
+
...params,
|
|
292
|
+
`}, ${tags}>`
|
|
293
|
+
];
|
|
294
|
+
}
|
|
295
|
+
const groupsOf = (catalog) => {
|
|
296
|
+
const groups = /* @__PURE__ */ new Map();
|
|
297
|
+
for (const message of catalog.messages) {
|
|
298
|
+
const name = message.path[0];
|
|
299
|
+
groups.set(name, [...groups.get(name) ?? [], message]);
|
|
300
|
+
}
|
|
301
|
+
return groups;
|
|
302
|
+
};
|
|
303
|
+
function translationsTypeOf(catalog) {
|
|
304
|
+
const lines = [
|
|
305
|
+
"/**",
|
|
306
|
+
" * The translations of one language, as `getTranslations` returns them.",
|
|
307
|
+
" */",
|
|
308
|
+
"export type Translations = {"
|
|
309
|
+
];
|
|
310
|
+
for (const [group, messages] of groupsOf(catalog)) {
|
|
311
|
+
lines.push(` ${keyOf(group)}: {`);
|
|
312
|
+
for (const message of messages) {
|
|
313
|
+
const [first = "string", ...rest] = typeOf(message);
|
|
314
|
+
lines.push(...docOf(message, catalog.base, " "));
|
|
315
|
+
lines.push(` ${keyOf(message.path[1])}: ${first}${rest.length === 0 ? ";" : ""}`);
|
|
316
|
+
rest.forEach((line, index) => lines.push(` ${line}${index === rest.length - 1 ? ";" : ""}`));
|
|
317
|
+
}
|
|
318
|
+
lines.push(" };");
|
|
319
|
+
}
|
|
320
|
+
lines.push("};");
|
|
321
|
+
return lines;
|
|
322
|
+
}
|
|
323
|
+
function translationsOfLocale(catalog, locale) {
|
|
324
|
+
const lines = [` ${keyOf(locale)}: () => ({`];
|
|
325
|
+
for (const [group, messages] of groupsOf(catalog)) {
|
|
326
|
+
lines.push(` ${keyOf(group)}: {`);
|
|
327
|
+
for (const message of messages) {
|
|
328
|
+
const shown = require_load.shownLocaleOf(catalog, message.translations, locale);
|
|
329
|
+
const source = message.translations[shown]?.source ?? "";
|
|
330
|
+
const value = message.params.length === 0 && message.tags.length === 0 ? stringOf(source) : `text(${stringOf(source)})`;
|
|
331
|
+
lines.push(` ${keyOf(message.path[1])}: ${value},`);
|
|
332
|
+
}
|
|
333
|
+
lines.push(" },");
|
|
334
|
+
}
|
|
335
|
+
lines.push(" }),");
|
|
336
|
+
return lines;
|
|
337
|
+
}
|
|
338
|
+
function render(catalog) {
|
|
339
|
+
const names = catalog.locales.map((locale) => locale.name);
|
|
340
|
+
return [
|
|
341
|
+
"/* eslint-disable */",
|
|
342
|
+
"/**",
|
|
343
|
+
" * Generated by @kizunajs/i18n. Do not edit.",
|
|
344
|
+
" *",
|
|
345
|
+
" * Regenerate: kizuna-i18n generate",
|
|
346
|
+
" */",
|
|
347
|
+
"",
|
|
348
|
+
"/**",
|
|
349
|
+
" * A language the app has.",
|
|
350
|
+
" */",
|
|
351
|
+
`export type Locale = ${names.map(stringOf).join(" | ")};`,
|
|
352
|
+
"",
|
|
353
|
+
"/**",
|
|
354
|
+
" * Every language the app has, the base first.",
|
|
355
|
+
" */",
|
|
356
|
+
`export const locales: readonly Locale[] = [${names.map(stringOf).join(", ")}];`,
|
|
357
|
+
"",
|
|
358
|
+
"/**",
|
|
359
|
+
" * The base language, shown when no other language matches.",
|
|
360
|
+
" */",
|
|
361
|
+
`export const baseLocale: Locale = ${stringOf(catalog.base)};`,
|
|
362
|
+
"",
|
|
363
|
+
...translationsTypeOf(catalog),
|
|
364
|
+
"",
|
|
365
|
+
"const translationsOf: Record<Locale, () => Translations> = {",
|
|
366
|
+
...names.flatMap((locale) => translationsOfLocale(catalog, locale)),
|
|
367
|
+
"};",
|
|
368
|
+
"",
|
|
369
|
+
runtimeSource
|
|
370
|
+
].join("\n");
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* Generates the TypeScript file your app imports its translations from.
|
|
374
|
+
* Each message gets its type and its texts in JSDoc, and the file imports nothing.
|
|
375
|
+
*
|
|
376
|
+
* @example
|
|
377
|
+
* typescriptTarget({
|
|
378
|
+
* output: './src/translations.generated.ts',
|
|
379
|
+
* });
|
|
380
|
+
*/
|
|
381
|
+
const typescriptTarget = defineTarget({
|
|
382
|
+
language: "typescript",
|
|
383
|
+
generate: ({ catalog }) => render(catalog)
|
|
384
|
+
});
|
|
385
|
+
//#endregion
|
|
386
|
+
exports.defineConfig = defineConfig;
|
|
387
|
+
exports.defineTarget = defineTarget;
|
|
388
|
+
exports.formatProblems = require_load.formatProblems;
|
|
389
|
+
exports.loadMessages = require_load.loadMessages;
|
|
390
|
+
exports.problemsOf = require_load.problemsOf;
|
|
391
|
+
exports.sourceOf = require_load.sourceOf;
|
|
392
|
+
exports.toCatalog = require_load.toCatalog;
|
|
393
|
+
exports.typescriptTarget = typescriptTarget;
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import { n as MessagesSource, t as LocaleDefinition } from "./source-CAF21cuM.cjs";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
|
|
4
|
+
//#region src/segments.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* One piece of a parsed text: literal text, a `{param}`, or a `<tag>` with what it wraps.
|
|
7
|
+
*/
|
|
8
|
+
type Segment = {
|
|
9
|
+
type: 'text';
|
|
10
|
+
text: string;
|
|
11
|
+
} | {
|
|
12
|
+
type: 'param';
|
|
13
|
+
name: string;
|
|
14
|
+
} | {
|
|
15
|
+
type: 'tag';
|
|
16
|
+
name: string;
|
|
17
|
+
children: Segment[];
|
|
18
|
+
};
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region src/catalog.d.ts
|
|
21
|
+
/**
|
|
22
|
+
* One language's text of a message: as written, and parsed.
|
|
23
|
+
*/
|
|
24
|
+
type CatalogText = {
|
|
25
|
+
/**
|
|
26
|
+
* The text as written, like `Welcome back, {firstName}`.
|
|
27
|
+
*/
|
|
28
|
+
source: string;
|
|
29
|
+
/**
|
|
30
|
+
* The text parsed into text, params and tags.
|
|
31
|
+
*/
|
|
32
|
+
segments: Segment[];
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* One message, with every language that wrote it.
|
|
36
|
+
*/
|
|
37
|
+
type CatalogMessage = {
|
|
38
|
+
/**
|
|
39
|
+
* The dotted path to the message, like `cart.title`.
|
|
40
|
+
*/
|
|
41
|
+
key: string;
|
|
42
|
+
/**
|
|
43
|
+
* The same path as its parts, so a target can rebuild the nesting.
|
|
44
|
+
*/
|
|
45
|
+
path: string[];
|
|
46
|
+
/**
|
|
47
|
+
* What the message is for, written for whoever translates it.
|
|
48
|
+
*/
|
|
49
|
+
description?: string;
|
|
50
|
+
/**
|
|
51
|
+
* The most characters the message may have, in every language.
|
|
52
|
+
*/
|
|
53
|
+
maxLength?: number;
|
|
54
|
+
/**
|
|
55
|
+
* Sample values for the params.
|
|
56
|
+
*/
|
|
57
|
+
examples?: Record<string, string>;
|
|
58
|
+
/**
|
|
59
|
+
* Every param, in the order they first appear in the base language.
|
|
60
|
+
*/
|
|
61
|
+
params: string[];
|
|
62
|
+
/**
|
|
63
|
+
* Every tag, in the order they first appear in the base language.
|
|
64
|
+
*/
|
|
65
|
+
tags: string[];
|
|
66
|
+
/**
|
|
67
|
+
* Each language's text. A language with a fallback may be left out.
|
|
68
|
+
*/
|
|
69
|
+
translations: Record<string, CatalogText>;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Every message in every language, checked and parsed, as a target receives it.
|
|
73
|
+
*/
|
|
74
|
+
type Catalog = {
|
|
75
|
+
/**
|
|
76
|
+
* The catalog format's version.
|
|
77
|
+
*/
|
|
78
|
+
format: 1;
|
|
79
|
+
/**
|
|
80
|
+
* The base language.
|
|
81
|
+
*/
|
|
82
|
+
base: string;
|
|
83
|
+
/**
|
|
84
|
+
* Every language, the base first, with the language it falls back to.
|
|
85
|
+
*/
|
|
86
|
+
locales: LocaleDefinition[];
|
|
87
|
+
/**
|
|
88
|
+
* Every message, group by group, in the order they were written.
|
|
89
|
+
*/
|
|
90
|
+
messages: CatalogMessage[];
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* Something wrong with a message in one language.
|
|
94
|
+
*/
|
|
95
|
+
type Problem = {
|
|
96
|
+
/**
|
|
97
|
+
* The dotted path to the message.
|
|
98
|
+
*/
|
|
99
|
+
key: string;
|
|
100
|
+
/**
|
|
101
|
+
* The language it is wrong in.
|
|
102
|
+
*/
|
|
103
|
+
locale: string;
|
|
104
|
+
/**
|
|
105
|
+
* What is wrong, like `leaves out {firstName}`.
|
|
106
|
+
*/
|
|
107
|
+
message: string;
|
|
108
|
+
/**
|
|
109
|
+
* `error` fails a check. `notice` reports a language that shows its fallback's text.
|
|
110
|
+
*/
|
|
111
|
+
severity: 'error' | 'notice';
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* Lists every problem in the messages: a missing language, a wrong param or tag, a text over its
|
|
115
|
+
* length limit, and each language that shows its fallback's text.
|
|
116
|
+
*/
|
|
117
|
+
declare function problemsOf(source: MessagesSource): Problem[];
|
|
118
|
+
/**
|
|
119
|
+
* Formats problems one per line, as `kizuna-i18n check` prints them.
|
|
120
|
+
*/
|
|
121
|
+
declare function formatProblems(problems: Problem[]): string;
|
|
122
|
+
/**
|
|
123
|
+
* Builds the catalog from an app's messages. Throws when the messages have an error, listing
|
|
124
|
+
* every problem.
|
|
125
|
+
*/
|
|
126
|
+
declare function toCatalog(source: MessagesSource): Catalog;
|
|
127
|
+
//#endregion
|
|
128
|
+
//#region src/target.d.ts
|
|
129
|
+
/**
|
|
130
|
+
* A file a target writes, relative to its `output`.
|
|
131
|
+
*/
|
|
132
|
+
type TargetFile = {
|
|
133
|
+
path: string;
|
|
134
|
+
contents: string;
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* What a target's `generate` receives.
|
|
138
|
+
*/
|
|
139
|
+
type TargetContext<Options> = {
|
|
140
|
+
/**
|
|
141
|
+
* The options the config passed, less `output`, checked against the target's schema.
|
|
142
|
+
*/
|
|
143
|
+
options: Options;
|
|
144
|
+
/**
|
|
145
|
+
* The catalog to generate from.
|
|
146
|
+
*/
|
|
147
|
+
catalog: Catalog;
|
|
148
|
+
};
|
|
149
|
+
/**
|
|
150
|
+
* A target as a config lists it: one language, written to `output`.
|
|
151
|
+
*/
|
|
152
|
+
type Target = {
|
|
153
|
+
/**
|
|
154
|
+
* The language it generates, like `swift`.
|
|
155
|
+
*/
|
|
156
|
+
language: string;
|
|
157
|
+
/**
|
|
158
|
+
* Where it writes, resolved against the config's folder. A target that writes one file treats
|
|
159
|
+
* it as the file, and a target that writes several treats it as their folder.
|
|
160
|
+
*/
|
|
161
|
+
output: string;
|
|
162
|
+
/**
|
|
163
|
+
* Renders the catalog: one file's contents, or several files.
|
|
164
|
+
*/
|
|
165
|
+
render: (catalog: Catalog) => string | TargetFile[];
|
|
166
|
+
};
|
|
167
|
+
type OptionsInput<Schema> = Schema extends z.ZodType ? z.input<Schema> : {};
|
|
168
|
+
type OptionsOutput<Schema> = Schema extends z.ZodType ? z.output<Schema> : {};
|
|
169
|
+
/**
|
|
170
|
+
* What {@link defineTarget} takes.
|
|
171
|
+
*/
|
|
172
|
+
type TargetDefinition<Schema extends z.ZodType | undefined> = {
|
|
173
|
+
/**
|
|
174
|
+
* The language this target generates, like `swift` or `csharp`.
|
|
175
|
+
*/
|
|
176
|
+
language: string;
|
|
177
|
+
/**
|
|
178
|
+
* A Zod schema for what a config passes in, beside `output`.
|
|
179
|
+
*/
|
|
180
|
+
options?: Schema;
|
|
181
|
+
/**
|
|
182
|
+
* Returns the generated code for the catalog.
|
|
183
|
+
*/
|
|
184
|
+
generate: (context: TargetContext<OptionsOutput<Schema>>) => string | TargetFile[];
|
|
185
|
+
};
|
|
186
|
+
/**
|
|
187
|
+
* Declares a target for a language: the options a config passes it, and how it turns the catalog
|
|
188
|
+
* into code. Returns the function a config lists under `targets`.
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* export const csharpTarget = defineTarget({
|
|
192
|
+
* language: 'csharp',
|
|
193
|
+
* options: z.object({
|
|
194
|
+
* namespace: z.string().default('Strings'),
|
|
195
|
+
* }),
|
|
196
|
+
* generate: ({ options, catalog }) => renderCsharp(catalog, options.namespace),
|
|
197
|
+
* });
|
|
198
|
+
*/
|
|
199
|
+
declare function defineTarget<Schema extends z.ZodType | undefined = undefined>(definition: TargetDefinition<Schema>): (options: OptionsInput<Schema> & {
|
|
200
|
+
output: string;
|
|
201
|
+
}) => Target;
|
|
202
|
+
//#endregion
|
|
203
|
+
//#region src/config.d.ts
|
|
204
|
+
/**
|
|
205
|
+
* What `i18n.config.ts` exports.
|
|
206
|
+
*/
|
|
207
|
+
type I18nConfig = {
|
|
208
|
+
/**
|
|
209
|
+
* The folder of message files, resolved against the config's folder. Every group a file
|
|
210
|
+
* exports is read, named by its export.
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
* './translations'
|
|
214
|
+
*/
|
|
215
|
+
messages: string;
|
|
216
|
+
/**
|
|
217
|
+
* The code to generate, one target per language.
|
|
218
|
+
*/
|
|
219
|
+
targets?: Target[];
|
|
220
|
+
};
|
|
221
|
+
/**
|
|
222
|
+
* Declares where the messages are, and what `kizuna-i18n generate` writes from them.
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* export default defineConfig({
|
|
226
|
+
* messages: './translations',
|
|
227
|
+
* targets: [
|
|
228
|
+
* typescriptTarget({
|
|
229
|
+
* output: './src/translations.generated.ts',
|
|
230
|
+
* }),
|
|
231
|
+
* ],
|
|
232
|
+
* });
|
|
233
|
+
*/
|
|
234
|
+
declare function defineConfig(config: I18nConfig): I18nConfig;
|
|
235
|
+
//#endregion
|
|
236
|
+
//#region src/load.d.ts
|
|
237
|
+
/**
|
|
238
|
+
* Builds an app's messages from its groups, by name. Throws when two groups were made by
|
|
239
|
+
* different `defineI18n` calls.
|
|
240
|
+
*/
|
|
241
|
+
declare function sourceOf(groups: Record<string, unknown>): MessagesSource;
|
|
242
|
+
/**
|
|
243
|
+
* Imports every module in the messages folder and collects the groups they export, each named by
|
|
244
|
+
* its export.
|
|
245
|
+
*/
|
|
246
|
+
declare function loadMessages(folder: string): Promise<MessagesSource>;
|
|
247
|
+
//#endregion
|
|
248
|
+
//#region src/typescript-target.d.ts
|
|
249
|
+
/**
|
|
250
|
+
* Generates the TypeScript file your app imports its translations from.
|
|
251
|
+
* Each message gets its type and its texts in JSDoc, and the file imports nothing.
|
|
252
|
+
*
|
|
253
|
+
* @example
|
|
254
|
+
* typescriptTarget({
|
|
255
|
+
* output: './src/translations.generated.ts',
|
|
256
|
+
* });
|
|
257
|
+
*/
|
|
258
|
+
declare const typescriptTarget: (options: {
|
|
259
|
+
output: string;
|
|
260
|
+
}) => Target;
|
|
261
|
+
//#endregion
|
|
262
|
+
export { type Catalog, type CatalogMessage, type CatalogText, type I18nConfig, type LocaleDefinition, type MessagesSource, type Problem, type Segment, type Target, type TargetContext, defineConfig, defineTarget, formatProblems, loadMessages, problemsOf, sourceOf, toCatalog, typescriptTarget };
|