@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/README.md
CHANGED
|
@@ -25,26 +25,34 @@ pnpm add -D @kizunajs/i18n
|
|
|
25
25
|
|
|
26
26
|
## Usage
|
|
27
27
|
|
|
28
|
-
Declare your languages:
|
|
28
|
+
Declare your languages, where the messages are, and what to generate:
|
|
29
29
|
|
|
30
30
|
```ts
|
|
31
|
-
//
|
|
32
|
-
import {
|
|
31
|
+
// i18n.config.ts
|
|
32
|
+
import { defineConfig, typescriptTarget } from '@kizunajs/i18n/generate';
|
|
33
33
|
|
|
34
|
-
export
|
|
34
|
+
export default defineConfig({
|
|
35
35
|
base: 'en',
|
|
36
36
|
locales: {
|
|
37
37
|
en: {},
|
|
38
38
|
nb: {},
|
|
39
39
|
},
|
|
40
|
+
messages: './translations',
|
|
41
|
+
targets: [
|
|
42
|
+
typescriptTarget({
|
|
43
|
+
output: './src/translations.generated.ts',
|
|
44
|
+
}),
|
|
45
|
+
],
|
|
40
46
|
});
|
|
41
47
|
```
|
|
42
48
|
|
|
43
|
-
|
|
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:
|
|
44
52
|
|
|
45
53
|
```ts
|
|
46
54
|
// translations/cart.ts
|
|
47
|
-
import
|
|
55
|
+
import i18n from '../i18n.config';
|
|
48
56
|
|
|
49
57
|
export const cart = i18n.messages({
|
|
50
58
|
title: i18n.message().translations({
|
|
@@ -72,21 +80,7 @@ nb: 'Velkommen tilbake, {fornavn}',
|
|
|
72
80
|
// '"nb uses {fornavn}, the message has {firstName}"'.
|
|
73
81
|
```
|
|
74
82
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
```ts
|
|
78
|
-
// i18n.config.ts
|
|
79
|
-
import { defineConfig, typescriptTarget } from '@kizunajs/i18n/generate';
|
|
80
|
-
|
|
81
|
-
export default defineConfig({
|
|
82
|
-
messages: './translations',
|
|
83
|
-
targets: [
|
|
84
|
-
typescriptTarget({
|
|
85
|
-
output: './src/translations.generated.ts',
|
|
86
|
-
}),
|
|
87
|
-
],
|
|
88
|
-
});
|
|
89
|
-
```
|
|
83
|
+
Generate the code:
|
|
90
84
|
|
|
91
85
|
```sh
|
|
92
86
|
pnpm kizuna-i18n generate
|
|
@@ -209,16 +203,13 @@ greeting: i18n
|
|
|
209
203
|
A language with a `fallback` may leave messages out:
|
|
210
204
|
|
|
211
205
|
```ts
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
de: {
|
|
218
|
-
fallback: 'en',
|
|
219
|
-
},
|
|
206
|
+
locales: {
|
|
207
|
+
en: {},
|
|
208
|
+
nb: {},
|
|
209
|
+
de: {
|
|
210
|
+
fallback: 'en',
|
|
220
211
|
},
|
|
221
|
-
}
|
|
212
|
+
},
|
|
222
213
|
```
|
|
223
214
|
|
|
224
215
|
## Tags
|
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
|
|
@@ -412,7 +394,7 @@ const typescriptTarget = defineTarget({
|
|
|
412
394
|
generate: ({ catalog }) => render(catalog)
|
|
413
395
|
});
|
|
414
396
|
//#endregion
|
|
415
|
-
exports.defineConfig = defineConfig;
|
|
397
|
+
exports.defineConfig = require_load.defineConfig;
|
|
416
398
|
exports.defineTarget = defineTarget;
|
|
417
399
|
exports.formatProblems = require_load.formatProblems;
|
|
418
400
|
exports.loadMessages = require_load.loadMessages;
|
package/dist/generate.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { n 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
|
|
@@ -207,9 +207,9 @@ declare function defineTarget<Schema extends z.ZodType | undefined = undefined>(
|
|
|
207
207
|
//#endregion
|
|
208
208
|
//#region src/config.d.ts
|
|
209
209
|
/**
|
|
210
|
-
* What `i18n.config.ts`
|
|
210
|
+
* What `i18n.config.ts` declares: the languages, where the messages are, and what to generate.
|
|
211
211
|
*/
|
|
212
|
-
type I18nConfig = {
|
|
212
|
+
type I18nConfig<Locales extends Record<string, LocaleOptions> = Record<string, LocaleOptions>, Base extends keyof Locales & string = string> = Languages<Locales, Base> & {
|
|
213
213
|
/**
|
|
214
214
|
* The folder of message files, resolved against the config's folder. Every group a file
|
|
215
215
|
* exports is read, named by its export.
|
|
@@ -224,10 +224,16 @@ type I18nConfig = {
|
|
|
224
224
|
targets?: Target[];
|
|
225
225
|
};
|
|
226
226
|
/**
|
|
227
|
-
* 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`.
|
|
228
229
|
*
|
|
229
230
|
* @example
|
|
230
231
|
* export default defineConfig({
|
|
232
|
+
* base: 'en',
|
|
233
|
+
* locales: {
|
|
234
|
+
* en: {},
|
|
235
|
+
* nb: {},
|
|
236
|
+
* },
|
|
231
237
|
* messages: './translations',
|
|
232
238
|
* targets: [
|
|
233
239
|
* typescriptTarget({
|
|
@@ -236,12 +242,12 @@ type I18nConfig = {
|
|
|
236
242
|
* ],
|
|
237
243
|
* });
|
|
238
244
|
*/
|
|
239
|
-
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>;
|
|
240
246
|
//#endregion
|
|
241
247
|
//#region src/load.d.ts
|
|
242
248
|
/**
|
|
243
249
|
* Builds an app's messages from its groups, by name. Throws when two groups were made by
|
|
244
|
-
* different
|
|
250
|
+
* different configs.
|
|
245
251
|
*/
|
|
246
252
|
declare function sourceOf(groups: Record<string, unknown>): MessagesSource;
|
|
247
253
|
/**
|
package/dist/generate.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { n as
|
|
1
|
+
import { c as ParamDefinition, n as Languages, o as LocaleDefinition, r as LocaleOptions, s as MessagesSource, t as I18n } from "./types-DC3XWG_J.mjs";
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
|
|
4
4
|
//#region src/segments.d.ts
|
|
@@ -207,9 +207,9 @@ declare function defineTarget<Schema extends z.ZodType | undefined = undefined>(
|
|
|
207
207
|
//#endregion
|
|
208
208
|
//#region src/config.d.ts
|
|
209
209
|
/**
|
|
210
|
-
* What `i18n.config.ts`
|
|
210
|
+
* What `i18n.config.ts` declares: the languages, where the messages are, and what to generate.
|
|
211
211
|
*/
|
|
212
|
-
type I18nConfig = {
|
|
212
|
+
type I18nConfig<Locales extends Record<string, LocaleOptions> = Record<string, LocaleOptions>, Base extends keyof Locales & string = string> = Languages<Locales, Base> & {
|
|
213
213
|
/**
|
|
214
214
|
* The folder of message files, resolved against the config's folder. Every group a file
|
|
215
215
|
* exports is read, named by its export.
|
|
@@ -224,10 +224,16 @@ type I18nConfig = {
|
|
|
224
224
|
targets?: Target[];
|
|
225
225
|
};
|
|
226
226
|
/**
|
|
227
|
-
* 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`.
|
|
228
229
|
*
|
|
229
230
|
* @example
|
|
230
231
|
* export default defineConfig({
|
|
232
|
+
* base: 'en',
|
|
233
|
+
* locales: {
|
|
234
|
+
* en: {},
|
|
235
|
+
* nb: {},
|
|
236
|
+
* },
|
|
231
237
|
* messages: './translations',
|
|
232
238
|
* targets: [
|
|
233
239
|
* typescriptTarget({
|
|
@@ -236,12 +242,12 @@ type I18nConfig = {
|
|
|
236
242
|
* ],
|
|
237
243
|
* });
|
|
238
244
|
*/
|
|
239
|
-
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>;
|
|
240
246
|
//#endregion
|
|
241
247
|
//#region src/load.d.ts
|
|
242
248
|
/**
|
|
243
249
|
* Builds an app's messages from its groups, by name. Throws when two groups were made by
|
|
244
|
-
* different
|
|
250
|
+
* different configs.
|
|
245
251
|
*/
|
|
246
252
|
declare function sourceOf(groups: Record<string, unknown>): MessagesSource;
|
|
247
253
|
/**
|
package/dist/generate.mjs
CHANGED
|
@@ -1,22 +1,4 @@
|
|
|
1
|
-
import { a as
|
|
2
|
-
//#region src/config.ts
|
|
3
|
-
/**
|
|
4
|
-
* Declares where the messages are, and what `kizuna-i18n generate` writes from them.
|
|
5
|
-
*
|
|
6
|
-
* @example
|
|
7
|
-
* export default defineConfig({
|
|
8
|
-
* messages: './translations',
|
|
9
|
-
* targets: [
|
|
10
|
-
* typescriptTarget({
|
|
11
|
-
* output: './src/translations.generated.ts',
|
|
12
|
-
* }),
|
|
13
|
-
* ],
|
|
14
|
-
* });
|
|
15
|
-
*/
|
|
16
|
-
function defineConfig(config) {
|
|
17
|
-
return config;
|
|
18
|
-
}
|
|
19
|
-
//#endregion
|
|
1
|
+
import { a as formatProblems, c as toCatalog, i as defineConfig, n as sourceOf, o as problemsOf, s as shownLocaleOf, t as loadMessages } from "./load-EiVgmBB9.mjs";
|
|
20
2
|
//#region src/target.ts
|
|
21
3
|
/**
|
|
22
4
|
* Declares a target for a language: the options a config passes it, and how it turns the catalog
|
package/dist/index.cjs
CHANGED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_source = require("./source-B3RV-rox.cjs");
|
|
3
|
-
//#region src/define-i18n.ts
|
|
4
|
-
function buildParam(definition) {
|
|
5
|
-
return {
|
|
6
|
-
[require_source.paramSource]: definition,
|
|
7
|
-
example: (value) => buildParam({
|
|
8
|
-
...definition,
|
|
9
|
-
example: value
|
|
10
|
-
})
|
|
11
|
-
};
|
|
12
|
-
}
|
|
13
|
-
function buildMessage(declaration, translations) {
|
|
14
|
-
const definition = {
|
|
15
|
-
translations: { ...translations },
|
|
16
|
-
params: Object.fromEntries(Object.entries(declaration.params ?? {}).map(([name, param]) => [name, param[require_source.paramSource]])),
|
|
17
|
-
...declaration.description === void 0 ? {} : { description: declaration.description },
|
|
18
|
-
...declaration.maxLength === void 0 ? {} : { maxLength: declaration.maxLength }
|
|
19
|
-
};
|
|
20
|
-
return { [require_source.messageSource]: definition };
|
|
21
|
-
}
|
|
22
|
-
function messagesOf(tree, path) {
|
|
23
|
-
return Object.entries(tree).flatMap(([name, value]) => {
|
|
24
|
-
const definition = value[require_source.messageSource];
|
|
25
|
-
return definition === void 0 ? messagesOf(value, [...path, name]) : [{
|
|
26
|
-
path: [...path, name],
|
|
27
|
-
definition
|
|
28
|
-
}];
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
function checkFallbacks(locales, base) {
|
|
32
|
-
const names = locales.map((locale) => locale.name);
|
|
33
|
-
if (!names.includes(base)) throw new Error(`The base language '${base}' is not one of the languages.`);
|
|
34
|
-
for (const locale of locales) {
|
|
35
|
-
const seen = [locale.name];
|
|
36
|
-
let current = locale;
|
|
37
|
-
while (current?.fallback !== void 0) {
|
|
38
|
-
const next = current.fallback;
|
|
39
|
-
if (!names.includes(next)) throw new Error(`'${current.name}' falls back to '${next}', which is not one of the languages.`);
|
|
40
|
-
if (seen.includes(next)) throw new Error(`'${locale.name}' falls back in a circle: ${[...seen, next].join(", ")}.`);
|
|
41
|
-
seen.push(next);
|
|
42
|
-
current = locales.find((candidate) => candidate.name === next);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Declares your languages and names the base language, the one every other language is checked
|
|
48
|
-
* against. Returns `i18n`, which declares the messages.
|
|
49
|
-
*
|
|
50
|
-
* @example
|
|
51
|
-
* export const i18n = defineI18n({
|
|
52
|
-
* base: 'en',
|
|
53
|
-
* locales: {
|
|
54
|
-
* en: {},
|
|
55
|
-
* nb: {},
|
|
56
|
-
* },
|
|
57
|
-
* });
|
|
58
|
-
*/
|
|
59
|
-
function defineI18n(config) {
|
|
60
|
-
const base = config.base;
|
|
61
|
-
const locales = Object.entries(config.locales).map(([name, options]) => options.fallback === void 0 ? { name } : {
|
|
62
|
-
name,
|
|
63
|
-
fallback: options.fallback
|
|
64
|
-
});
|
|
65
|
-
checkFallbacks(locales, base);
|
|
66
|
-
return {
|
|
67
|
-
message: (declaration = {}) => ({ translations: (texts) => buildMessage(declaration, texts) }),
|
|
68
|
-
string: () => buildParam({ type: "string" }),
|
|
69
|
-
number: () => buildParam({ type: "number" }),
|
|
70
|
-
messages: (messages) => {
|
|
71
|
-
const group = {
|
|
72
|
-
base,
|
|
73
|
-
locales,
|
|
74
|
-
messages: messagesOf(messages, [])
|
|
75
|
-
};
|
|
76
|
-
return { [require_source.messagesSource]: group };
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
//#endregion
|
|
81
|
-
exports.defineI18n = defineI18n;
|
package/dist/index.d.cts
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-BE4-k_kg.cjs";
|
|
2
|
+
export { type I18n, type Message, type Messages };
|