@kizunajs/i18n 0.1.0 → 0.2.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 +74 -21
- package/dist/bin.cjs +1 -1
- package/dist/bin.mjs +1 -1
- package/dist/generate.cjs +95 -66
- package/dist/generate.d.cts +15 -10
- package/dist/generate.d.mts +15 -10
- package/dist/generate.mjs +95 -66
- package/dist/index.cjs +27 -18
- package/dist/index.d.cts +115 -51
- package/dist/index.d.mts +115 -51
- package/dist/index.mjs +27 -18
- package/dist/{load-CE9FYddD.mjs → load-CMmjOpu-.mjs} +21 -16
- package/dist/{load-dVzjsBK6.cjs → load-Ddl3iCSC.cjs} +21 -16
- package/dist/{source-CDwD6Wcn.cjs → source-B3RV-rox.cjs} +10 -0
- package/dist/{source-BrYHodVt.mjs → source-BWSeiZiF.mjs} +5 -1
- package/dist/source-C6_RixNW.d.cts +56 -0
- package/dist/source-Cu0aGXpT.d.mts +56 -0
- package/package.json +2 -3
- package/dist/source-CAF21cuM.d.cts +0 -35
- package/dist/source-DJMnQ2hS.d.mts +0 -35
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { a as messagesSource, i as messageSource, o as paramSource } from "./source-Cu0aGXpT.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/types.d.ts
|
|
4
4
|
/**
|
|
@@ -24,67 +24,101 @@ type TagsIn<Text> = Text extends `${string}<${infer Tag}>${infer Inner}</${infer
|
|
|
24
24
|
type RequiredLocalesOf<Locales> = { [Locale in keyof Locales]-?: Locales[Locale] extends {
|
|
25
25
|
fallback: string;
|
|
26
26
|
} ? never : Locale }[keyof Locales] & string;
|
|
27
|
-
type CheckText<Locale extends string, Text, Params extends string, Tags extends string> = Text extends string ? string extends Text ? Text : [Exclude<ParamsIn<Text>, Params>] extends [never] ? CheckLeftOut<Locale, Text, Params, Tags> : [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, Params extends string, Tags extends string> = [Exclude<Params, ParamsIn<Text>>] extends [never] ? [Exclude<TagsIn<Text>, Tags>] extends [never] ? [Exclude<Tags, TagsIn<Text>>] extends [never] ? Text : `${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>>}}`;
|
|
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;
|
|
29
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;
|
|
30
39
|
/**
|
|
31
|
-
* Checks every language of a message against the base language's
|
|
32
|
-
* becomes the sentence that says what is wrong, so the compiler
|
|
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.
|
|
33
43
|
*/
|
|
34
|
-
type CheckTexts<Texts, Locales, Base extends string> = { [Locale in keyof Texts]: Locale extends keyof Locales ? CheckText<Locale & string, Texts[Locale],
|
|
35
|
-
type LengthOf<Text extends string, Count extends unknown[] = []> = Text extends `${string}${infer Rest}` ? LengthOf<Rest, [...Count, unknown]> : Count['length'];
|
|
36
|
-
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;
|
|
37
|
-
type TooLong<Texts, Limit extends number> = { [Locale in keyof Texts]: Texts[Locale] extends string ? [ParamsIn<Texts[Locale]>] extends [never] ? IsLonger<Texts[Locale], Limit> extends true ? `${Locale & string} is ${LengthOf<Texts[Locale]>} characters, the limit is ${Limit}` : never : never : never }[keyof Texts];
|
|
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 };
|
|
38
45
|
/**
|
|
39
|
-
*
|
|
40
|
-
* params, with its example values filled in.
|
|
46
|
+
* A text param made by `i18n.string`.
|
|
41
47
|
*/
|
|
42
|
-
type
|
|
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
|
+
};
|
|
43
59
|
/**
|
|
44
|
-
* A
|
|
60
|
+
* A number param made by `i18n.number`.
|
|
45
61
|
*/
|
|
46
|
-
type
|
|
62
|
+
type NumberParam = {
|
|
47
63
|
/**
|
|
48
|
-
*
|
|
64
|
+
* Gives a sample value. The generated JSDoc shows it, and `kizuna-i18n check` fills it in to
|
|
65
|
+
* check the length.
|
|
49
66
|
*
|
|
50
67
|
* @example
|
|
51
|
-
* i18n
|
|
52
|
-
* .message({
|
|
53
|
-
* en: 'Your cart',
|
|
54
|
-
* nb: 'Handlekurven din',
|
|
55
|
-
* })
|
|
56
|
-
* .maxLength(16);
|
|
68
|
+
* i18n.number().example(3);
|
|
57
69
|
*/
|
|
58
|
-
|
|
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 = {
|
|
59
77
|
/**
|
|
60
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}`.
|
|
61
87
|
*
|
|
62
88
|
* @example
|
|
63
|
-
*
|
|
64
|
-
* .
|
|
65
|
-
*
|
|
66
|
-
* nb: 'Lagre',
|
|
67
|
-
* })
|
|
68
|
-
* .describe('Button that saves the order as a draft');
|
|
89
|
+
* params: {
|
|
90
|
+
* firstName: i18n.string().example('Ada'),
|
|
91
|
+
* },
|
|
69
92
|
*/
|
|
70
|
-
|
|
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> = {
|
|
71
99
|
/**
|
|
72
|
-
* Gives
|
|
73
|
-
* them in.
|
|
100
|
+
* Gives the message one text per language.
|
|
74
101
|
*
|
|
75
102
|
* @example
|
|
76
|
-
* i18n
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
* })
|
|
81
|
-
* .example({
|
|
82
|
-
* firstName: 'Ada',
|
|
83
|
-
* });
|
|
103
|
+
* i18n.message().translations({
|
|
104
|
+
* en: 'Your cart',
|
|
105
|
+
* nb: 'Handlekurven din',
|
|
106
|
+
* });
|
|
84
107
|
*/
|
|
85
|
-
|
|
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 = {
|
|
86
114
|
readonly [messageSource]: unknown;
|
|
87
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
|
+
};
|
|
88
122
|
/**
|
|
89
123
|
* A group of messages made by `i18n.messages`. `kizuna-i18n generate` reads every group the
|
|
90
124
|
* translations folder exports, named by its export.
|
|
@@ -97,25 +131,55 @@ type Messages = {
|
|
|
97
131
|
*/
|
|
98
132
|
type I18n<Locales extends Record<string, LocaleOptions>, Base extends keyof Locales & string> = {
|
|
99
133
|
/**
|
|
100
|
-
* Declares a message
|
|
134
|
+
* Declares a message. `.translations` then gives it one text per language.
|
|
101
135
|
*
|
|
102
136
|
* @example
|
|
103
|
-
* i18n
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
*
|
|
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
|
+
* });
|
|
107
148
|
*/
|
|
108
|
-
message: <const
|
|
149
|
+
message: <const Declaration extends MessageDeclaration = {}>(declaration?: Declaration) => DeclaredMessage<Declaration, Locales, Base>;
|
|
109
150
|
/**
|
|
110
|
-
*
|
|
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`.
|
|
111
167
|
*
|
|
112
168
|
* @example
|
|
113
169
|
* export const cart = i18n.messages({
|
|
114
|
-
* title
|
|
115
|
-
*
|
|
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
|
+
* },
|
|
116
180
|
* });
|
|
117
181
|
*/
|
|
118
|
-
messages: (messages:
|
|
182
|
+
messages: (messages: MessageTree) => Messages;
|
|
119
183
|
};
|
|
120
184
|
//#endregion
|
|
121
185
|
//#region src/define-i18n.d.ts
|
|
@@ -134,7 +198,7 @@ type I18n<Locales extends Record<string, LocaleOptions>, Base extends keyof Loca
|
|
|
134
198
|
*/
|
|
135
199
|
declare function defineI18n<const Locales extends Record<string, LocaleOptions>, const Base extends keyof Locales & string>(config: {
|
|
136
200
|
/**
|
|
137
|
-
* The base language. Every other language is checked against its
|
|
201
|
+
* The base language. Every other language is checked against its tags.
|
|
138
202
|
*/
|
|
139
203
|
base: Base;
|
|
140
204
|
/**
|
package/dist/index.mjs
CHANGED
|
@@ -1,25 +1,32 @@
|
|
|
1
|
-
import { n as messagesSource, t as messageSource } from "./source-
|
|
1
|
+
import { n as messagesSource, r as paramSource, t as messageSource } from "./source-BWSeiZiF.mjs";
|
|
2
2
|
//#region src/define-i18n.ts
|
|
3
|
-
function
|
|
3
|
+
function buildParam(definition) {
|
|
4
4
|
return {
|
|
5
|
-
[
|
|
6
|
-
|
|
5
|
+
[paramSource]: definition,
|
|
6
|
+
example: (value) => buildParam({
|
|
7
7
|
...definition,
|
|
8
|
-
|
|
9
|
-
}),
|
|
10
|
-
describe: (description) => buildMessage({
|
|
11
|
-
...definition,
|
|
12
|
-
description
|
|
13
|
-
}),
|
|
14
|
-
example: (values) => buildMessage({
|
|
15
|
-
...definition,
|
|
16
|
-
examples: {
|
|
17
|
-
...definition.examples,
|
|
18
|
-
...values
|
|
19
|
-
}
|
|
8
|
+
example: value
|
|
20
9
|
})
|
|
21
10
|
};
|
|
22
11
|
}
|
|
12
|
+
function buildMessage(declaration, translations) {
|
|
13
|
+
const definition = {
|
|
14
|
+
translations: { ...translations },
|
|
15
|
+
params: Object.fromEntries(Object.entries(declaration.params ?? {}).map(([name, param]) => [name, param[paramSource]])),
|
|
16
|
+
...declaration.description === void 0 ? {} : { description: declaration.description },
|
|
17
|
+
...declaration.maxLength === void 0 ? {} : { maxLength: declaration.maxLength }
|
|
18
|
+
};
|
|
19
|
+
return { [messageSource]: definition };
|
|
20
|
+
}
|
|
21
|
+
function messagesOf(tree, path) {
|
|
22
|
+
return Object.entries(tree).flatMap(([name, value]) => {
|
|
23
|
+
const definition = value[messageSource];
|
|
24
|
+
return definition === void 0 ? messagesOf(value, [...path, name]) : [{
|
|
25
|
+
path: [...path, name],
|
|
26
|
+
definition
|
|
27
|
+
}];
|
|
28
|
+
});
|
|
29
|
+
}
|
|
23
30
|
function checkFallbacks(locales, base) {
|
|
24
31
|
const names = locales.map((locale) => locale.name);
|
|
25
32
|
if (!names.includes(base)) throw new Error(`The base language '${base}' is not one of the languages.`);
|
|
@@ -56,12 +63,14 @@ function defineI18n(config) {
|
|
|
56
63
|
});
|
|
57
64
|
checkFallbacks(locales, base);
|
|
58
65
|
return {
|
|
59
|
-
message: (
|
|
66
|
+
message: (declaration = {}) => ({ translations: (texts) => buildMessage(declaration, texts) }),
|
|
67
|
+
string: () => buildParam({ type: "string" }),
|
|
68
|
+
number: () => buildParam({ type: "number" }),
|
|
60
69
|
messages: (messages) => {
|
|
61
70
|
const group = {
|
|
62
71
|
base,
|
|
63
72
|
locales,
|
|
64
|
-
messages:
|
|
73
|
+
messages: messagesOf(messages, [])
|
|
65
74
|
};
|
|
66
75
|
return { [messagesSource]: group };
|
|
67
76
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { n as messagesSource } from "./source-
|
|
1
|
+
import { n as messagesSource } from "./source-BWSeiZiF.mjs";
|
|
2
2
|
import { readdirSync } from "node:fs";
|
|
3
3
|
import { join, relative } from "node:path";
|
|
4
4
|
import { createJiti } from "jiti";
|
|
@@ -81,6 +81,7 @@ function renderedOf(segments, examples) {
|
|
|
81
81
|
}
|
|
82
82
|
const braced = (names) => names.map((name) => `{${name}}`).join(" and ");
|
|
83
83
|
const angled = (names) => names.map((name) => `<${name}>`).join(" and ");
|
|
84
|
+
const examplesOf = (definition) => Object.fromEntries(Object.entries(definition.params).flatMap(([name, param]) => param.example === void 0 ? [] : [[name, String(param.example)]]));
|
|
84
85
|
function checkText(text, definition, params, tags) {
|
|
85
86
|
const names = namesOf(parseText(text));
|
|
86
87
|
const extra = names.params.filter((name) => !params.includes(name));
|
|
@@ -94,7 +95,7 @@ function checkText(text, definition, params, tags) {
|
|
|
94
95
|
if (extraTags.length > 0) problems.push(`uses ${angled(extraTags)}, which the message does not have`);
|
|
95
96
|
if (missingTags.length > 0) problems.push(`leaves out ${angled(missingTags)}`);
|
|
96
97
|
if (definition.maxLength !== void 0) {
|
|
97
|
-
const length = lengthOf(renderedOf(parseText(text), definition
|
|
98
|
+
const length = lengthOf(renderedOf(parseText(text), examplesOf(definition)));
|
|
98
99
|
if (length > definition.maxLength) {
|
|
99
100
|
const filled = names.params.length > 0 ? " with the examples filled in" : "";
|
|
100
101
|
problems.push(`is ${length} characters${filled}, the limit is ${definition.maxLength}`);
|
|
@@ -117,18 +118,17 @@ function shownLocaleOf(source, translations, locale) {
|
|
|
117
118
|
}
|
|
118
119
|
return source.base;
|
|
119
120
|
}
|
|
120
|
-
const paramsOf = (definition, base) => namesOf(parseText(definition.translations[base] ?? "")).params;
|
|
121
121
|
const tagsOf = (definition, base) => namesOf(parseText(definition.translations[base] ?? "")).tags;
|
|
122
122
|
/**
|
|
123
|
-
* Lists every problem in the messages: a missing language, a
|
|
124
|
-
* length limit, and each language that shows its fallback's text.
|
|
123
|
+
* Lists every problem in the messages: a missing language, a param the message does not declare, a
|
|
124
|
+
* wrong tag, a text over its length limit, and each language that shows its fallback's text.
|
|
125
125
|
*/
|
|
126
126
|
function problemsOf(source) {
|
|
127
127
|
const names = source.locales.map((locale) => locale.name);
|
|
128
128
|
const problems = [];
|
|
129
|
-
for (const
|
|
130
|
-
const key =
|
|
131
|
-
const params =
|
|
129
|
+
for (const { path, definition } of source.messages) {
|
|
130
|
+
const key = path.join(".");
|
|
131
|
+
const params = Object.keys(definition.params);
|
|
132
132
|
const tags = tagsOf(definition, source.base);
|
|
133
133
|
const add = (locale, message, severity = "error") => problems.push({
|
|
134
134
|
key,
|
|
@@ -136,7 +136,6 @@ function problemsOf(source) {
|
|
|
136
136
|
message,
|
|
137
137
|
severity
|
|
138
138
|
});
|
|
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
139
|
for (const locale of Object.keys(definition.translations)) if (!names.includes(locale)) add(locale, "is not one of the languages");
|
|
141
140
|
for (const locale of source.locales) {
|
|
142
141
|
const text = definition.translations[locale.name];
|
|
@@ -166,15 +165,18 @@ function toCatalog(source) {
|
|
|
166
165
|
const errors = problemsOf(source).filter((problem) => problem.severity === "error");
|
|
167
166
|
if (errors.length > 0) throw new Error(`The messages have ${errors.length === 1 ? "a problem" : `${errors.length} problems`}:\n${formatProblems(errors)}`);
|
|
168
167
|
const locales = [...source.locales.filter((locale) => locale.name === source.base), ...source.locales.filter((locale) => locale.name !== source.base)];
|
|
169
|
-
const messages =
|
|
168
|
+
const messages = source.messages.map(({ path, definition }) => {
|
|
170
169
|
const segments = parseText(definition.translations[source.base] ?? "");
|
|
171
170
|
return {
|
|
172
|
-
key:
|
|
173
|
-
path
|
|
171
|
+
key: path.join("."),
|
|
172
|
+
path,
|
|
174
173
|
...definition.description === void 0 ? {} : { description: definition.description },
|
|
175
174
|
...definition.maxLength === void 0 ? {} : { maxLength: definition.maxLength },
|
|
176
|
-
|
|
177
|
-
|
|
175
|
+
params: Object.entries(definition.params).map(([name, param]) => ({
|
|
176
|
+
name,
|
|
177
|
+
...param
|
|
178
|
+
})),
|
|
179
|
+
tags: namesOf(segments).tags,
|
|
178
180
|
translations: Object.fromEntries(locales.filter((locale) => definition.translations[locale.name] !== void 0).map((locale) => {
|
|
179
181
|
const text = definition.translations[locale.name];
|
|
180
182
|
return [locale.name, {
|
|
@@ -183,7 +185,7 @@ function toCatalog(source) {
|
|
|
183
185
|
}];
|
|
184
186
|
}))
|
|
185
187
|
};
|
|
186
|
-
})
|
|
188
|
+
});
|
|
187
189
|
return {
|
|
188
190
|
format: 1,
|
|
189
191
|
base: source.base,
|
|
@@ -215,7 +217,10 @@ function sourceOf(groups) {
|
|
|
215
217
|
return {
|
|
216
218
|
base: first.base,
|
|
217
219
|
locales: first.locales,
|
|
218
|
-
|
|
220
|
+
messages: definitions.flatMap(([name, group]) => group.messages.map((message) => ({
|
|
221
|
+
path: [name, ...message.path],
|
|
222
|
+
definition: message.definition
|
|
223
|
+
})))
|
|
219
224
|
};
|
|
220
225
|
}
|
|
221
226
|
function filesIn(folder) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const require_source = require("./source-
|
|
1
|
+
const require_source = require("./source-B3RV-rox.cjs");
|
|
2
2
|
let node_fs = require("node:fs");
|
|
3
3
|
let node_path = require("node:path");
|
|
4
4
|
let jiti = require("jiti");
|
|
@@ -81,6 +81,7 @@ function renderedOf(segments, examples) {
|
|
|
81
81
|
}
|
|
82
82
|
const braced = (names) => names.map((name) => `{${name}}`).join(" and ");
|
|
83
83
|
const angled = (names) => names.map((name) => `<${name}>`).join(" and ");
|
|
84
|
+
const examplesOf = (definition) => Object.fromEntries(Object.entries(definition.params).flatMap(([name, param]) => param.example === void 0 ? [] : [[name, String(param.example)]]));
|
|
84
85
|
function checkText(text, definition, params, tags) {
|
|
85
86
|
const names = namesOf(parseText(text));
|
|
86
87
|
const extra = names.params.filter((name) => !params.includes(name));
|
|
@@ -94,7 +95,7 @@ function checkText(text, definition, params, tags) {
|
|
|
94
95
|
if (extraTags.length > 0) problems.push(`uses ${angled(extraTags)}, which the message does not have`);
|
|
95
96
|
if (missingTags.length > 0) problems.push(`leaves out ${angled(missingTags)}`);
|
|
96
97
|
if (definition.maxLength !== void 0) {
|
|
97
|
-
const length = lengthOf(renderedOf(parseText(text), definition
|
|
98
|
+
const length = lengthOf(renderedOf(parseText(text), examplesOf(definition)));
|
|
98
99
|
if (length > definition.maxLength) {
|
|
99
100
|
const filled = names.params.length > 0 ? " with the examples filled in" : "";
|
|
100
101
|
problems.push(`is ${length} characters${filled}, the limit is ${definition.maxLength}`);
|
|
@@ -117,18 +118,17 @@ function shownLocaleOf(source, translations, locale) {
|
|
|
117
118
|
}
|
|
118
119
|
return source.base;
|
|
119
120
|
}
|
|
120
|
-
const paramsOf = (definition, base) => namesOf(parseText(definition.translations[base] ?? "")).params;
|
|
121
121
|
const tagsOf = (definition, base) => namesOf(parseText(definition.translations[base] ?? "")).tags;
|
|
122
122
|
/**
|
|
123
|
-
* Lists every problem in the messages: a missing language, a
|
|
124
|
-
* length limit, and each language that shows its fallback's text.
|
|
123
|
+
* Lists every problem in the messages: a missing language, a param the message does not declare, a
|
|
124
|
+
* wrong tag, a text over its length limit, and each language that shows its fallback's text.
|
|
125
125
|
*/
|
|
126
126
|
function problemsOf(source) {
|
|
127
127
|
const names = source.locales.map((locale) => locale.name);
|
|
128
128
|
const problems = [];
|
|
129
|
-
for (const
|
|
130
|
-
const key =
|
|
131
|
-
const params =
|
|
129
|
+
for (const { path, definition } of source.messages) {
|
|
130
|
+
const key = path.join(".");
|
|
131
|
+
const params = Object.keys(definition.params);
|
|
132
132
|
const tags = tagsOf(definition, source.base);
|
|
133
133
|
const add = (locale, message, severity = "error") => problems.push({
|
|
134
134
|
key,
|
|
@@ -136,7 +136,6 @@ function problemsOf(source) {
|
|
|
136
136
|
message,
|
|
137
137
|
severity
|
|
138
138
|
});
|
|
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
139
|
for (const locale of Object.keys(definition.translations)) if (!names.includes(locale)) add(locale, "is not one of the languages");
|
|
141
140
|
for (const locale of source.locales) {
|
|
142
141
|
const text = definition.translations[locale.name];
|
|
@@ -166,15 +165,18 @@ function toCatalog(source) {
|
|
|
166
165
|
const errors = problemsOf(source).filter((problem) => problem.severity === "error");
|
|
167
166
|
if (errors.length > 0) throw new Error(`The messages have ${errors.length === 1 ? "a problem" : `${errors.length} problems`}:\n${formatProblems(errors)}`);
|
|
168
167
|
const locales = [...source.locales.filter((locale) => locale.name === source.base), ...source.locales.filter((locale) => locale.name !== source.base)];
|
|
169
|
-
const messages =
|
|
168
|
+
const messages = source.messages.map(({ path, definition }) => {
|
|
170
169
|
const segments = parseText(definition.translations[source.base] ?? "");
|
|
171
170
|
return {
|
|
172
|
-
key:
|
|
173
|
-
path
|
|
171
|
+
key: path.join("."),
|
|
172
|
+
path,
|
|
174
173
|
...definition.description === void 0 ? {} : { description: definition.description },
|
|
175
174
|
...definition.maxLength === void 0 ? {} : { maxLength: definition.maxLength },
|
|
176
|
-
|
|
177
|
-
|
|
175
|
+
params: Object.entries(definition.params).map(([name, param]) => ({
|
|
176
|
+
name,
|
|
177
|
+
...param
|
|
178
|
+
})),
|
|
179
|
+
tags: namesOf(segments).tags,
|
|
178
180
|
translations: Object.fromEntries(locales.filter((locale) => definition.translations[locale.name] !== void 0).map((locale) => {
|
|
179
181
|
const text = definition.translations[locale.name];
|
|
180
182
|
return [locale.name, {
|
|
@@ -183,7 +185,7 @@ function toCatalog(source) {
|
|
|
183
185
|
}];
|
|
184
186
|
}))
|
|
185
187
|
};
|
|
186
|
-
})
|
|
188
|
+
});
|
|
187
189
|
return {
|
|
188
190
|
format: 1,
|
|
189
191
|
base: source.base,
|
|
@@ -215,7 +217,10 @@ function sourceOf(groups) {
|
|
|
215
217
|
return {
|
|
216
218
|
base: first.base,
|
|
217
219
|
locales: first.locales,
|
|
218
|
-
|
|
220
|
+
messages: definitions.flatMap(([name, group]) => group.messages.map((message) => ({
|
|
221
|
+
path: [name, ...message.path],
|
|
222
|
+
definition: message.definition
|
|
223
|
+
})))
|
|
219
224
|
};
|
|
220
225
|
}
|
|
221
226
|
function filesIn(folder) {
|
|
@@ -7,6 +7,10 @@ const messageSource = Symbol.for("kizuna-i18n.message");
|
|
|
7
7
|
* Where a group made by `i18n.messages` keeps its messages.
|
|
8
8
|
*/
|
|
9
9
|
const messagesSource = Symbol.for("kizuna-i18n.messages");
|
|
10
|
+
/**
|
|
11
|
+
* Where a param made by `i18n.string` or `i18n.number` keeps its definition.
|
|
12
|
+
*/
|
|
13
|
+
const paramSource = Symbol.for("kizuna-i18n.param");
|
|
10
14
|
//#endregion
|
|
11
15
|
Object.defineProperty(exports, "messageSource", {
|
|
12
16
|
enumerable: true,
|
|
@@ -20,3 +24,9 @@ Object.defineProperty(exports, "messagesSource", {
|
|
|
20
24
|
return messagesSource;
|
|
21
25
|
}
|
|
22
26
|
});
|
|
27
|
+
Object.defineProperty(exports, "paramSource", {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
get: function() {
|
|
30
|
+
return paramSource;
|
|
31
|
+
}
|
|
32
|
+
});
|
|
@@ -7,5 +7,9 @@ const messageSource = Symbol.for("kizuna-i18n.message");
|
|
|
7
7
|
* Where a group made by `i18n.messages` keeps its messages.
|
|
8
8
|
*/
|
|
9
9
|
const messagesSource = Symbol.for("kizuna-i18n.messages");
|
|
10
|
+
/**
|
|
11
|
+
* Where a param made by `i18n.string` or `i18n.number` keeps its definition.
|
|
12
|
+
*/
|
|
13
|
+
const paramSource = Symbol.for("kizuna-i18n.param");
|
|
10
14
|
//#endregion
|
|
11
|
-
export { messagesSource as n, messageSource as t };
|
|
15
|
+
export { messagesSource as n, paramSource as r, messageSource as t };
|
|
@@ -0,0 +1,56 @@
|
|
|
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
|
+
export { messagesSource as a, messageSource as i, MessagesSource as n, paramSource as o, ParamDefinition as r, LocaleDefinition as t };
|
|
@@ -0,0 +1,56 @@
|
|
|
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
|
+
export { messagesSource as a, messageSource as i, MessagesSource as n, paramSource as o, ParamDefinition as r, LocaleDefinition as t };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kizunajs/i18n",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "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.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"kizunajs",
|
|
@@ -11,13 +11,12 @@
|
|
|
11
11
|
"codegen"
|
|
12
12
|
],
|
|
13
13
|
"license": "MIT",
|
|
14
|
-
"homepage": "https://
|
|
14
|
+
"homepage": "https://www.npmjs.com/package/@kizunajs/i18n",
|
|
15
15
|
"repository": {
|
|
16
16
|
"type": "git",
|
|
17
17
|
"url": "git+https://github.com/kizunajs/i18n.git",
|
|
18
18
|
"directory": "packages/i18n"
|
|
19
19
|
},
|
|
20
|
-
"bugs": "https://github.com/kizunajs/i18n/issues",
|
|
21
20
|
"type": "module",
|
|
22
21
|
"sideEffects": false,
|
|
23
22
|
"engines": {
|