@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
package/README.md
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# Kizuna i18n
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/@kizunajs/i18n)
|
|
3
4
|

|
|
4
5
|
|
|
5
6
|
Write your translations in TypeScript. Get typed text in your app, and a build that fails when a language is missing or a text won't fit.
|
|
@@ -7,8 +8,8 @@ Write your translations in TypeScript. Get typed text in your app, and a build t
|
|
|
7
8
|
## Features
|
|
8
9
|
|
|
9
10
|
- **Broken translations don't compile**: forget Norwegian, or write `{fornavn}` where English has `{firstName}`, and `tsc` names the language that's wrong
|
|
10
|
-
- **Text that fits**:
|
|
11
|
-
- **Docs on hover**: every message shows its text, what it's for and its limit in your editor
|
|
11
|
+
- **Text that fits**: `maxLength: 16` on a button label, and a German text that's too long fails the build
|
|
12
|
+
- **Docs on hover**: every message shows its text in every language, what it's for and its limit in your editor
|
|
12
13
|
- **Nothing to install**: the generated file has no imports, so your app ships no i18n library
|
|
13
14
|
- **Links inside sentences**: `<terms>the terms of sale</terms>` renders as your own `<a>`, in every language
|
|
14
15
|
- **Ship a language half done**: `de` shows the English text for messages nobody has translated yet
|
|
@@ -24,40 +25,54 @@ pnpm add -D @kizunajs/i18n
|
|
|
24
25
|
|
|
25
26
|
## Usage
|
|
26
27
|
|
|
27
|
-
Declare your languages:
|
|
28
|
+
Declare your languages, where the messages are, and what to generate:
|
|
28
29
|
|
|
29
30
|
```ts
|
|
30
|
-
//
|
|
31
|
-
import {
|
|
31
|
+
// i18n.config.ts
|
|
32
|
+
import { defineConfig, typescriptTarget } from '@kizunajs/i18n/generate';
|
|
32
33
|
|
|
33
|
-
export
|
|
34
|
+
export default defineConfig({
|
|
34
35
|
base: 'en',
|
|
35
36
|
locales: {
|
|
36
37
|
en: {},
|
|
37
38
|
nb: {},
|
|
38
39
|
},
|
|
40
|
+
messages: './translations',
|
|
41
|
+
targets: [
|
|
42
|
+
typescriptTarget({
|
|
43
|
+
output: './src/translations.generated.ts',
|
|
44
|
+
}),
|
|
45
|
+
],
|
|
39
46
|
});
|
|
40
47
|
```
|
|
41
48
|
|
|
42
|
-
|
|
49
|
+
Each language is a BCP 47 tag, like `en` or `pt-BR`. A tag like `en-us` or `pt_BR` fails, and the error says what to write.
|
|
50
|
+
|
|
51
|
+
Import the config as `i18n`, declare each message, then give it its texts:
|
|
43
52
|
|
|
44
53
|
```ts
|
|
45
54
|
// translations/cart.ts
|
|
46
|
-
import
|
|
55
|
+
import i18n from '../i18n.config';
|
|
47
56
|
|
|
48
57
|
export const cart = i18n.messages({
|
|
49
|
-
title: i18n.message({
|
|
58
|
+
title: i18n.message().translations({
|
|
50
59
|
en: 'Your cart',
|
|
51
60
|
nb: 'Handlekurven din',
|
|
52
61
|
}),
|
|
53
|
-
greeting: i18n
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
62
|
+
greeting: i18n
|
|
63
|
+
.message({
|
|
64
|
+
params: {
|
|
65
|
+
firstName: i18n.string(),
|
|
66
|
+
},
|
|
67
|
+
})
|
|
68
|
+
.translations({
|
|
69
|
+
en: 'Welcome back, {firstName}',
|
|
70
|
+
nb: 'Velkommen tilbake, {firstName}',
|
|
71
|
+
}),
|
|
57
72
|
});
|
|
58
73
|
```
|
|
59
74
|
|
|
60
|
-
Every language must use the
|
|
75
|
+
Every language must use exactly the declared `{params}`:
|
|
61
76
|
|
|
62
77
|
```ts
|
|
63
78
|
nb: 'Velkommen tilbake, {fornavn}',
|
|
@@ -65,21 +80,7 @@ nb: 'Velkommen tilbake, {fornavn}',
|
|
|
65
80
|
// '"nb uses {fornavn}, the message has {firstName}"'.
|
|
66
81
|
```
|
|
67
82
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
```ts
|
|
71
|
-
// i18n.config.ts
|
|
72
|
-
import { defineConfig, typescriptTarget } from '@kizunajs/i18n/generate';
|
|
73
|
-
|
|
74
|
-
export default defineConfig({
|
|
75
|
-
messages: './translations',
|
|
76
|
-
targets: [
|
|
77
|
-
typescriptTarget({
|
|
78
|
-
output: './src/translations.generated.ts',
|
|
79
|
-
}),
|
|
80
|
-
],
|
|
81
|
-
});
|
|
82
|
-
```
|
|
83
|
+
Generate the code:
|
|
83
84
|
|
|
84
85
|
```sh
|
|
85
86
|
pnpm kizuna-i18n generate
|
|
@@ -118,6 +119,50 @@ import { matchLocale } from './translations.generated';
|
|
|
118
119
|
export const getLocale = async () => matchLocale((await headers()).get('accept-language') ?? '');
|
|
119
120
|
```
|
|
120
121
|
|
|
122
|
+
## Nested groups
|
|
123
|
+
|
|
124
|
+
A plain object inside a group nests a group, at any depth:
|
|
125
|
+
|
|
126
|
+
```ts
|
|
127
|
+
export const checkout = i18n.messages({
|
|
128
|
+
form: {
|
|
129
|
+
email: {
|
|
130
|
+
label: i18n.message().translations({
|
|
131
|
+
en: 'Email',
|
|
132
|
+
nb: 'E-post',
|
|
133
|
+
}),
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
```ts
|
|
140
|
+
t.checkout.form.email.label; // 'E-post'
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## Params
|
|
144
|
+
|
|
145
|
+
A param is `i18n.string()` or `i18n.number()`, and the generated code takes that type:
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
items: i18n
|
|
149
|
+
.message({
|
|
150
|
+
params: {
|
|
151
|
+
count: i18n.number(),
|
|
152
|
+
},
|
|
153
|
+
})
|
|
154
|
+
.translations({
|
|
155
|
+
en: 'Items in your cart: {count}',
|
|
156
|
+
nb: 'Varer i handlekurven: {count}',
|
|
157
|
+
}),
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
```ts
|
|
161
|
+
t.cart.items({
|
|
162
|
+
count: 3,
|
|
163
|
+
}); // 'Varer i handlekurven: 3'
|
|
164
|
+
```
|
|
165
|
+
|
|
121
166
|
## Length limits
|
|
122
167
|
|
|
123
168
|
`maxLength` sets the most characters a text may have, in every language:
|
|
@@ -125,27 +170,31 @@ export const getLocale = async () => matchLocale((await headers()).get('accept-l
|
|
|
125
170
|
```ts
|
|
126
171
|
title: i18n
|
|
127
172
|
.message({
|
|
173
|
+
maxLength: 16,
|
|
174
|
+
})
|
|
175
|
+
.translations({
|
|
128
176
|
en: 'Your cart',
|
|
129
177
|
nb: 'Handlekurven din',
|
|
130
|
-
})
|
|
131
|
-
.maxLength(16),
|
|
178
|
+
}),
|
|
132
179
|
```
|
|
133
180
|
|
|
134
|
-
The compiler checks texts without params. `kizuna-i18n check` checks the rest, with
|
|
181
|
+
The compiler checks texts without params. `kizuna-i18n check` checks the rest, with each param's example filled in.
|
|
135
182
|
|
|
136
183
|
## Descriptions and examples
|
|
137
184
|
|
|
138
|
-
`
|
|
185
|
+
`description` and each param's `example` show up in the generated JSDoc:
|
|
139
186
|
|
|
140
187
|
```ts
|
|
141
188
|
greeting: i18n
|
|
142
189
|
.message({
|
|
190
|
+
description: 'Shown above the cart to a customer who is logged in',
|
|
191
|
+
params: {
|
|
192
|
+
firstName: i18n.string().example('Ada'),
|
|
193
|
+
},
|
|
194
|
+
})
|
|
195
|
+
.translations({
|
|
143
196
|
en: 'Welcome back, {firstName}',
|
|
144
197
|
nb: 'Velkommen tilbake, {firstName}',
|
|
145
|
-
})
|
|
146
|
-
.describe('Shown above the cart to a customer who is logged in')
|
|
147
|
-
.example({
|
|
148
|
-
firstName: 'Ada',
|
|
149
198
|
}),
|
|
150
199
|
```
|
|
151
200
|
|
|
@@ -154,24 +203,21 @@ greeting: i18n
|
|
|
154
203
|
A language with a `fallback` may leave messages out:
|
|
155
204
|
|
|
156
205
|
```ts
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
de: {
|
|
163
|
-
fallback: 'en',
|
|
164
|
-
},
|
|
206
|
+
locales: {
|
|
207
|
+
en: {},
|
|
208
|
+
nb: {},
|
|
209
|
+
de: {
|
|
210
|
+
fallback: 'en',
|
|
165
211
|
},
|
|
166
|
-
}
|
|
212
|
+
},
|
|
167
213
|
```
|
|
168
214
|
|
|
169
215
|
## Tags
|
|
170
216
|
|
|
171
|
-
Pass a function for each
|
|
217
|
+
Every language must use the base language's `<tags>`. Pass a function for each tag. It gets the translated text inside the tag:
|
|
172
218
|
|
|
173
219
|
```ts
|
|
174
|
-
terms: i18n.message({
|
|
220
|
+
terms: i18n.message().translations({
|
|
175
221
|
en: 'I agree to <terms>the terms of sale</terms>',
|
|
176
222
|
nb: 'Jeg godtar <terms>salgsvilkårene</terms>',
|
|
177
223
|
}),
|
package/dist/bin.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
const require_load = require("./load-
|
|
2
|
+
const require_load = require("./load-CdaJ2I49.cjs");
|
|
3
3
|
let node_fs = require("node:fs");
|
|
4
4
|
let node_path = require("node:path");
|
|
5
5
|
let jiti = require("jiti");
|
|
@@ -85,13 +85,13 @@ const usage = `Usage: kizuna-i18n <command> [--config <path>]
|
|
|
85
85
|
generate --watch Writes again on every change to the messages.
|
|
86
86
|
--config <path> The config file. Default: i18n.config.ts`;
|
|
87
87
|
async function readConfig(configPath) {
|
|
88
|
-
return await (0, jiti.createJiti)(require("url").pathToFileURL(__filename).href, {
|
|
88
|
+
return require_load.configOf(await (0, jiti.createJiti)(require("url").pathToFileURL(__filename).href, {
|
|
89
89
|
interopDefault: true,
|
|
90
90
|
jsx: true,
|
|
91
91
|
tsconfigPaths: true,
|
|
92
92
|
moduleCache: false,
|
|
93
93
|
fsCache: false
|
|
94
|
-
}).import(configPath, { default: true });
|
|
94
|
+
}).import(configPath, { default: true }));
|
|
95
95
|
}
|
|
96
96
|
async function check(configPath) {
|
|
97
97
|
const config = await readConfig(configPath);
|
package/dist/bin.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
2
|
+
import { a as formatProblems, c as toCatalog, o as problemsOf, r as configOf, t as loadMessages } from "./load-EiVgmBB9.mjs";
|
|
3
3
|
import { existsSync, mkdirSync, readFileSync, watch, writeFileSync } from "node:fs";
|
|
4
4
|
import { dirname, join, relative, resolve } from "node:path";
|
|
5
5
|
import { createJiti } from "jiti";
|
|
@@ -85,13 +85,13 @@ const usage = `Usage: kizuna-i18n <command> [--config <path>]
|
|
|
85
85
|
generate --watch Writes again on every change to the messages.
|
|
86
86
|
--config <path> The config file. Default: i18n.config.ts`;
|
|
87
87
|
async function readConfig(configPath) {
|
|
88
|
-
return await createJiti(import.meta.url, {
|
|
88
|
+
return configOf(await createJiti(import.meta.url, {
|
|
89
89
|
interopDefault: true,
|
|
90
90
|
jsx: true,
|
|
91
91
|
tsconfigPaths: true,
|
|
92
92
|
moduleCache: false,
|
|
93
93
|
fsCache: false
|
|
94
|
-
}).import(configPath, { default: true });
|
|
94
|
+
}).import(configPath, { default: true }));
|
|
95
95
|
}
|
|
96
96
|
async function check(configPath) {
|
|
97
97
|
const config = await readConfig(configPath);
|
package/dist/generate.cjs
CHANGED
|
@@ -1,23 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_load = require("./load-
|
|
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
|
|
2
|
+
const require_load = require("./load-CdaJ2I49.cjs");
|
|
21
3
|
//#region src/target.ts
|
|
22
4
|
/**
|
|
23
5
|
* Declares a target for a language: the options a config passes it, and how it turns the catalog
|
|
@@ -62,29 +44,33 @@ function defineTarget(definition) {
|
|
|
62
44
|
* messages it reads.
|
|
63
45
|
*/
|
|
64
46
|
const runtimeSource = String.raw`/**
|
|
65
|
-
* A message with params.
|
|
66
|
-
* message as parts, which React renders directly.
|
|
47
|
+
* A message with params. A React element as a param returns parts in place of a string.
|
|
67
48
|
*/
|
|
68
49
|
export type Message<Params> = {
|
|
69
50
|
(params: Params): string;
|
|
70
|
-
<Rich>(params: { [Name in keyof Params]: Params[Name] | Rich }): Array<string | Rich>;
|
|
51
|
+
<Rich extends object>(params: { [Name in keyof Params]: Params[Name] | Rich }): Array<string | Rich>;
|
|
71
52
|
};
|
|
72
53
|
|
|
73
54
|
/**
|
|
74
|
-
* A message with tags.
|
|
75
|
-
* what the tag becomes, like a link.
|
|
55
|
+
* A message with tags. Pass a function for each tag.
|
|
76
56
|
*/
|
|
77
|
-
export type TaggedMessage<Params, Tags extends string> =
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
57
|
+
export type TaggedMessage<Params, Tags extends string> = {
|
|
58
|
+
<Result>(
|
|
59
|
+
params: Params & {
|
|
60
|
+
[Tag in Tags]: (text: string | Array<string | Result>) => Result;
|
|
61
|
+
}
|
|
62
|
+
): Array<string | Result>;
|
|
63
|
+
<Result, Rich extends object>(
|
|
64
|
+
params: { [Name in keyof Params]: Params[Name] | Rich } & {
|
|
65
|
+
[Tag in Tags]: (text: string | Array<string | Rich | Result>) => Result;
|
|
66
|
+
}
|
|
67
|
+
): Array<string | Rich | Result>;
|
|
68
|
+
};
|
|
82
69
|
|
|
83
70
|
const cache = new Map<Locale, Translations>();
|
|
84
71
|
|
|
85
72
|
/**
|
|
86
|
-
* Returns the translations of a language.
|
|
87
|
-
* language.
|
|
73
|
+
* Returns the translations of a language.
|
|
88
74
|
*
|
|
89
75
|
* @example
|
|
90
76
|
* const t = getTranslations('nb');
|
|
@@ -105,8 +91,8 @@ const sameLanguages: Record<string, string[]> = {
|
|
|
105
91
|
};
|
|
106
92
|
|
|
107
93
|
/**
|
|
108
|
-
* Returns the language
|
|
109
|
-
*
|
|
94
|
+
* Returns the language that best matches an Accept-Language header or a list like
|
|
95
|
+
* navigator.languages, or the base language.
|
|
110
96
|
*
|
|
111
97
|
* @example
|
|
112
98
|
* matchLocale(['nb-NO', 'en-US']);
|
|
@@ -150,8 +136,7 @@ const TAG_PAIR = '<(\\w+)>([\\s\\S]*?)<\\/\\1>';
|
|
|
150
136
|
const PARAM = '\\{\\s*([A-Za-z_$][\\w$]*)\\s*\\}';
|
|
151
137
|
|
|
152
138
|
/**
|
|
153
|
-
*
|
|
154
|
-
* React.
|
|
139
|
+
* Marks React puts on its elements, so parts get keys without importing React.
|
|
155
140
|
*/
|
|
156
141
|
const reactElements = new Set<symbol>([Symbol.for('react.element'), Symbol.for('react.transitional.element')]);
|
|
157
142
|
|
|
@@ -161,7 +146,9 @@ function text(template: string): any {
|
|
|
161
146
|
|
|
162
147
|
function render(template: string, values: Record<string, unknown>): string | unknown[] {
|
|
163
148
|
const hasTag = new RegExp(TAG_PAIR).test(template);
|
|
164
|
-
const hasRichValue = Object.values(values).some(
|
|
149
|
+
const hasRichValue = Object.values(values).some(
|
|
150
|
+
(value) => typeof value !== 'string' && typeof value !== 'number' && typeof value !== 'function'
|
|
151
|
+
);
|
|
165
152
|
if (!hasTag && !hasRichValue) {
|
|
166
153
|
return template.replace(new RegExp(PARAM, 'g'), (_, name: string) => String(values[name] ?? ''));
|
|
167
154
|
}
|
|
@@ -199,8 +186,8 @@ function pushValues(parts: unknown[], stretch: string, values: Record<string, un
|
|
|
199
186
|
while ((match = param.exec(stretch))) {
|
|
200
187
|
pushText(parts, stretch.slice(lastIndex, match.index));
|
|
201
188
|
const value = values[match[1] ?? ''] ?? '';
|
|
202
|
-
if (typeof value === 'string') {
|
|
203
|
-
pushText(parts, value);
|
|
189
|
+
if (typeof value === 'string' || typeof value === 'number') {
|
|
190
|
+
pushText(parts, String(value));
|
|
204
191
|
} else {
|
|
205
192
|
parts.push(value);
|
|
206
193
|
}
|
|
@@ -249,27 +236,33 @@ function filledOf(segments, values) {
|
|
|
249
236
|
return segments.map((segment) => {
|
|
250
237
|
if (segment.type === "text") return segment.text;
|
|
251
238
|
if (segment.type === "param") return values[segment.name] ?? "";
|
|
252
|
-
return filledOf(segment.children, values)
|
|
239
|
+
return `<${segment.name}>${filledOf(segment.children, values)}</${segment.name}>`;
|
|
253
240
|
}).join("");
|
|
254
241
|
}
|
|
255
|
-
function
|
|
242
|
+
function textsOf(message, catalog, values) {
|
|
243
|
+
return catalog.locales.map((locale) => {
|
|
244
|
+
const text = message.translations[locale.name];
|
|
245
|
+
if (text === void 0) return `// ${locale.name}: shows ${require_load.shownLocaleOf(catalog, message.translations, locale.name)}`;
|
|
246
|
+
return `// ${locale.name}: ${stringOf(filledOf(text.segments, values))}`;
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
function exampleOf(message, catalog) {
|
|
256
250
|
const access = `t.${message.path.map(keyOf).join(".")}`;
|
|
257
|
-
const
|
|
258
|
-
if (message.params.length === 0 && message.tags.length === 0) return [`${access}
|
|
259
|
-
|
|
260
|
-
const lines = [
|
|
251
|
+
const values = Object.fromEntries(message.params.map((param) => [param.name, param.example === void 0 ? "…" : String(param.example)]));
|
|
252
|
+
if (message.params.length === 0 && message.tags.length === 0) return [`${access};`, ...textsOf(message, catalog, values)];
|
|
253
|
+
return [
|
|
261
254
|
`${access}({`,
|
|
262
|
-
...message.params.map((
|
|
263
|
-
...message.tags.map((tag) => ` ${keyOf(tag)}: (text) => text,`)
|
|
255
|
+
...message.params.map((param) => ` ${keyOf(param.name)}: ${typeof param.example === "number" ? String(param.example) : stringOf(values[param.name])},`),
|
|
256
|
+
...message.tags.map((tag) => ` ${keyOf(tag)}: (text) => text,`),
|
|
257
|
+
"});",
|
|
258
|
+
...textsOf(message, catalog, values)
|
|
264
259
|
];
|
|
265
|
-
const result = message.tags.length === 0 ? ` // ${stringOf(filledOf(baseText?.segments ?? [], values))}` : "";
|
|
266
|
-
return [...lines, `});${result}`];
|
|
267
260
|
}
|
|
268
|
-
function docOf(message,
|
|
261
|
+
function docOf(message, catalog, indent) {
|
|
269
262
|
const paragraphs = [];
|
|
270
263
|
if (message.description !== void 0) paragraphs.push(commentOf(message.description).split("\n"));
|
|
271
264
|
if (message.maxLength !== void 0) paragraphs.push([`@maxLength ${message.maxLength}`]);
|
|
272
|
-
paragraphs.push(["@example", ...exampleOf(message,
|
|
265
|
+
paragraphs.push(["@example", ...exampleOf(message, catalog).map(commentOf)]);
|
|
273
266
|
const body = paragraphs.flatMap((lines, index) => [...index === 0 ? [] : [""], ...lines]);
|
|
274
267
|
return [
|
|
275
268
|
`${indent}/**`,
|
|
@@ -279,7 +272,7 @@ function docOf(message, base, indent) {
|
|
|
279
272
|
}
|
|
280
273
|
function typeOf(message) {
|
|
281
274
|
if (message.params.length === 0 && message.tags.length === 0) return ["string"];
|
|
282
|
-
const params = message.params.map((
|
|
275
|
+
const params = message.params.map((param) => ` ${keyOf(param.name)}: ${param.type};`);
|
|
283
276
|
if (message.tags.length === 0) return [
|
|
284
277
|
"Message<{",
|
|
285
278
|
...params,
|
|
@@ -292,48 +285,66 @@ function typeOf(message) {
|
|
|
292
285
|
`}, ${tags}>`
|
|
293
286
|
];
|
|
294
287
|
}
|
|
295
|
-
|
|
296
|
-
const
|
|
288
|
+
function treeOf(catalog) {
|
|
289
|
+
const root = /* @__PURE__ */ new Map();
|
|
297
290
|
for (const message of catalog.messages) {
|
|
298
|
-
|
|
299
|
-
|
|
291
|
+
let branch = root;
|
|
292
|
+
for (const name of message.path.slice(0, -1)) {
|
|
293
|
+
let next = branch.get(name);
|
|
294
|
+
if (!(next instanceof Map)) {
|
|
295
|
+
next = /* @__PURE__ */ new Map();
|
|
296
|
+
branch.set(name, next);
|
|
297
|
+
}
|
|
298
|
+
branch = next;
|
|
299
|
+
}
|
|
300
|
+
branch.set(message.path[message.path.length - 1], message);
|
|
300
301
|
}
|
|
301
|
-
return
|
|
302
|
-
}
|
|
302
|
+
return root;
|
|
303
|
+
}
|
|
304
|
+
function typeLinesOf(branch, catalog, indent) {
|
|
305
|
+
return [...branch].flatMap(([name, node]) => {
|
|
306
|
+
if (node instanceof Map) return [
|
|
307
|
+
`${indent}${keyOf(name)}: {`,
|
|
308
|
+
...typeLinesOf(node, catalog, `${indent} `),
|
|
309
|
+
`${indent}};`
|
|
310
|
+
];
|
|
311
|
+
const [first = "string", ...rest] = typeOf(node);
|
|
312
|
+
return [
|
|
313
|
+
...docOf(node, catalog, indent),
|
|
314
|
+
`${indent}${keyOf(name)}: ${first}${rest.length === 0 ? ";" : ""}`,
|
|
315
|
+
...rest.map((line, index) => `${indent}${line}${index === rest.length - 1 ? ";" : ""}`)
|
|
316
|
+
];
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
function valueLinesOf(branch, catalog, locale, indent) {
|
|
320
|
+
return [...branch].flatMap(([name, node]) => {
|
|
321
|
+
if (node instanceof Map) return [
|
|
322
|
+
`${indent}${keyOf(name)}: {`,
|
|
323
|
+
...valueLinesOf(node, catalog, locale, `${indent} `),
|
|
324
|
+
`${indent}},`
|
|
325
|
+
];
|
|
326
|
+
const shown = require_load.shownLocaleOf(catalog, node.translations, locale);
|
|
327
|
+
const source = node.translations[shown]?.source ?? "";
|
|
328
|
+
const value = node.params.length === 0 && node.tags.length === 0 ? stringOf(source) : `text(${stringOf(source)})`;
|
|
329
|
+
return [`${indent}${keyOf(name)}: ${value},`];
|
|
330
|
+
});
|
|
331
|
+
}
|
|
303
332
|
function translationsTypeOf(catalog) {
|
|
304
|
-
|
|
333
|
+
return [
|
|
305
334
|
"/**",
|
|
306
335
|
" * The translations of one language, as `getTranslations` returns them.",
|
|
307
336
|
" */",
|
|
308
|
-
"export type Translations = {"
|
|
337
|
+
"export type Translations = {",
|
|
338
|
+
...typeLinesOf(treeOf(catalog), catalog, " "),
|
|
339
|
+
"};"
|
|
309
340
|
];
|
|
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
341
|
}
|
|
323
342
|
function translationsOfLocale(catalog, locale) {
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
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;
|
|
343
|
+
return [
|
|
344
|
+
` ${keyOf(locale)}: () => ({`,
|
|
345
|
+
...valueLinesOf(treeOf(catalog), catalog, locale, " "),
|
|
346
|
+
" }),"
|
|
347
|
+
];
|
|
337
348
|
}
|
|
338
349
|
function render(catalog) {
|
|
339
350
|
const names = catalog.locales.map((locale) => locale.name);
|
|
@@ -383,7 +394,7 @@ const typescriptTarget = defineTarget({
|
|
|
383
394
|
generate: ({ catalog }) => render(catalog)
|
|
384
395
|
});
|
|
385
396
|
//#endregion
|
|
386
|
-
exports.defineConfig = defineConfig;
|
|
397
|
+
exports.defineConfig = require_load.defineConfig;
|
|
387
398
|
exports.defineTarget = defineTarget;
|
|
388
399
|
exports.formatProblems = require_load.formatProblems;
|
|
389
400
|
exports.loadMessages = require_load.loadMessages;
|
package/dist/generate.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { n as MessagesSource, t as
|
|
1
|
+
import { c as ParamDefinition, n as Languages, o as LocaleDefinition, r as LocaleOptions, s as MessagesSource, t as I18n } from "./types-BE4-k_kg.cjs";
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
|
|
4
4
|
//#region src/segments.d.ts
|
|
@@ -31,6 +31,15 @@ type CatalogText = {
|
|
|
31
31
|
*/
|
|
32
32
|
segments: Segment[];
|
|
33
33
|
};
|
|
34
|
+
/**
|
|
35
|
+
* One param of a message.
|
|
36
|
+
*/
|
|
37
|
+
type CatalogParam = {
|
|
38
|
+
/**
|
|
39
|
+
* The name the texts use, like `firstName` for `{firstName}`.
|
|
40
|
+
*/
|
|
41
|
+
name: string;
|
|
42
|
+
} & ParamDefinition;
|
|
34
43
|
/**
|
|
35
44
|
* One message, with every language that wrote it.
|
|
36
45
|
*/
|
|
@@ -52,13 +61,9 @@ type CatalogMessage = {
|
|
|
52
61
|
*/
|
|
53
62
|
maxLength?: number;
|
|
54
63
|
/**
|
|
55
|
-
*
|
|
56
|
-
*/
|
|
57
|
-
examples?: Record<string, string>;
|
|
58
|
-
/**
|
|
59
|
-
* Every param, in the order they first appear in the base language.
|
|
64
|
+
* Every param, in the order the message declares them.
|
|
60
65
|
*/
|
|
61
|
-
params:
|
|
66
|
+
params: CatalogParam[];
|
|
62
67
|
/**
|
|
63
68
|
* Every tag, in the order they first appear in the base language.
|
|
64
69
|
*/
|
|
@@ -111,8 +116,8 @@ type Problem = {
|
|
|
111
116
|
severity: 'error' | 'notice';
|
|
112
117
|
};
|
|
113
118
|
/**
|
|
114
|
-
* Lists every problem in the messages: a missing language, a
|
|
115
|
-
* length limit, and each language that shows its fallback's text.
|
|
119
|
+
* Lists every problem in the messages: a missing language, a param the message does not declare, a
|
|
120
|
+
* wrong tag, a text over its length limit, and each language that shows its fallback's text.
|
|
116
121
|
*/
|
|
117
122
|
declare function problemsOf(source: MessagesSource): Problem[];
|
|
118
123
|
/**
|
|
@@ -202,9 +207,9 @@ declare function defineTarget<Schema extends z.ZodType | undefined = undefined>(
|
|
|
202
207
|
//#endregion
|
|
203
208
|
//#region src/config.d.ts
|
|
204
209
|
/**
|
|
205
|
-
* What `i18n.config.ts`
|
|
210
|
+
* What `i18n.config.ts` declares: the languages, where the messages are, and what to generate.
|
|
206
211
|
*/
|
|
207
|
-
type I18nConfig = {
|
|
212
|
+
type I18nConfig<Locales extends Record<string, LocaleOptions> = Record<string, LocaleOptions>, Base extends keyof Locales & string = string> = Languages<Locales, Base> & {
|
|
208
213
|
/**
|
|
209
214
|
* The folder of message files, resolved against the config's folder. Every group a file
|
|
210
215
|
* exports is read, named by its export.
|
|
@@ -219,10 +224,16 @@ type I18nConfig = {
|
|
|
219
224
|
targets?: Target[];
|
|
220
225
|
};
|
|
221
226
|
/**
|
|
222
|
-
* Declares where the messages are, and what `kizuna-i18n generate` writes from
|
|
227
|
+
* Declares your languages, where the messages are, and what `kizuna-i18n generate` writes from
|
|
228
|
+
* them. Export it as the default, and import it in message files as `i18n`.
|
|
223
229
|
*
|
|
224
230
|
* @example
|
|
225
231
|
* export default defineConfig({
|
|
232
|
+
* base: 'en',
|
|
233
|
+
* locales: {
|
|
234
|
+
* en: {},
|
|
235
|
+
* nb: {},
|
|
236
|
+
* },
|
|
226
237
|
* messages: './translations',
|
|
227
238
|
* targets: [
|
|
228
239
|
* typescriptTarget({
|
|
@@ -231,12 +242,12 @@ type I18nConfig = {
|
|
|
231
242
|
* ],
|
|
232
243
|
* });
|
|
233
244
|
*/
|
|
234
|
-
declare function defineConfig(config: I18nConfig):
|
|
245
|
+
declare function defineConfig<const Locales extends Record<string, LocaleOptions>, const Base extends keyof Locales & string>(config: I18nConfig<Locales, Base>): I18n<Locales, Base>;
|
|
235
246
|
//#endregion
|
|
236
247
|
//#region src/load.d.ts
|
|
237
248
|
/**
|
|
238
249
|
* Builds an app's messages from its groups, by name. Throws when two groups were made by
|
|
239
|
-
* different
|
|
250
|
+
* different configs.
|
|
240
251
|
*/
|
|
241
252
|
declare function sourceOf(groups: Record<string, unknown>): MessagesSource;
|
|
242
253
|
/**
|
|
@@ -259,4 +270,4 @@ declare const typescriptTarget: (options: {
|
|
|
259
270
|
output: string;
|
|
260
271
|
}) => Target;
|
|
261
272
|
//#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 };
|
|
273
|
+
export { type Catalog, type CatalogMessage, type CatalogParam, type CatalogText, type I18nConfig, type LocaleDefinition, type MessagesSource, type Problem, type Segment, type Target, type TargetContext, defineConfig, defineTarget, formatProblems, loadMessages, problemsOf, sourceOf, toCatalog, typescriptTarget };
|