@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.
@@ -0,0 +1,253 @@
1
+ //#region src/source.d.ts
2
+ /**
3
+ * Where a message made by `i18n.message` keeps its definition.
4
+ */
5
+ declare const messageSource: unique symbol;
6
+ /**
7
+ * Where a group made by `i18n.messages` keeps its messages.
8
+ */
9
+ declare const messagesSource: unique symbol;
10
+ /**
11
+ * Where a param made by `i18n.string` or `i18n.number` keeps its definition.
12
+ */
13
+ declare const paramSource: unique symbol;
14
+ /**
15
+ * One param as `i18n.string` or `i18n.number` declares it.
16
+ */
17
+ type ParamDefinition = {
18
+ type: 'string';
19
+ example?: string;
20
+ } | {
21
+ type: 'number';
22
+ example?: number;
23
+ };
24
+ /**
25
+ * A language, and the language it falls back to.
26
+ */
27
+ type LocaleDefinition = {
28
+ name: string;
29
+ fallback?: string;
30
+ };
31
+ /**
32
+ * One message as `i18n.message` declares it and `.translations` fills it in.
33
+ */
34
+ type MessageDefinition = {
35
+ translations: Record<string, string>;
36
+ description?: string;
37
+ maxLength?: number;
38
+ params: Record<string, ParamDefinition>;
39
+ };
40
+ /**
41
+ * One message with its path, like `['cart', 'title']`.
42
+ */
43
+ type SourceMessage = {
44
+ path: string[];
45
+ definition: MessageDefinition;
46
+ };
47
+ /**
48
+ * Every message of an app, group by group, with the languages they were made for.
49
+ */
50
+ type MessagesSource = {
51
+ base: string;
52
+ locales: LocaleDefinition[];
53
+ messages: SourceMessage[];
54
+ };
55
+ //#endregion
56
+ //#region src/types.d.ts
57
+ /**
58
+ * What a language may set in `defineConfig`.
59
+ */
60
+ type LocaleOptions = {
61
+ /**
62
+ * The language whose text shows for a message this language leaves out.
63
+ */
64
+ fallback?: string;
65
+ };
66
+ /**
67
+ * The languages of an app, and its base language.
68
+ */
69
+ type Languages<Locales extends Record<string, LocaleOptions>, Base extends keyof Locales & string> = {
70
+ /**
71
+ * The base language. Every other language is checked against its tags.
72
+ */
73
+ base: Base;
74
+ /**
75
+ * Every language, by its BCP 47 tag, like `en` or `pt-BR`.
76
+ */
77
+ locales: Locales & { [Locale in keyof Locales]: Locale extends Base ? {
78
+ fallback?: never;
79
+ } : {
80
+ fallback?: Exclude<keyof Locales & string, Locale>;
81
+ } };
82
+ };
83
+ type Trim<Text extends string> = Text extends ` ${infer Rest}` ? Trim<Rest> : Text extends `${infer Rest} ` ? Trim<Rest> : Text;
84
+ type LastPart<Text extends string> = Text extends `${string}{${infer Rest}` ? LastPart<Rest> : Text;
85
+ type ParamName<Inside extends string> = Trim<LastPart<Inside>> extends infer Name extends string ? (Name extends '' | `${string} ${string}` ? never : Name) : never;
86
+ /**
87
+ * The `{param}` names in a text, as a union.
88
+ */
89
+ type ParamsIn<Text> = Text extends `${string}{${infer Inside}}${infer Rest}` ? ParamName<Inside> | ParamsIn<Rest> : never;
90
+ /**
91
+ * The `<tag>` names in a text, as a union, nested tags included.
92
+ */
93
+ type TagsIn<Text> = Text extends `${string}<${infer Tag}>${infer Inner}</${infer Tag}>${infer Rest}` ? Tag | TagsIn<Inner> | TagsIn<Rest> : never;
94
+ type RequiredLocalesOf<Locales> = { [Locale in keyof Locales]-?: Locales[Locale] extends {
95
+ fallback: string;
96
+ } ? never : Locale }[keyof Locales] & string;
97
+ type CheckText<Locale extends string, Text, Params extends string, Tags extends string, Limit extends number> = Text extends string ? string extends Text ? Text : [Exclude<ParamsIn<Text>, Params>] extends [never] ? CheckLeftOut<Locale, Text, Params, Tags, Limit> : [Exclude<Params, ParamsIn<Text>>] extends [never] ? `${Locale} uses {${Exclude<ParamsIn<Text>, Params>}}, which the message does not have` : `${Locale} uses {${Exclude<ParamsIn<Text>, Params>}}, the message has {${Params}}` : string;
98
+ type CheckLeftOut<Locale extends string, Text extends string, Params extends string, Tags extends string, Limit extends number> = [Exclude<Params, ParamsIn<Text>>] extends [never] ? [Exclude<TagsIn<Text>, Tags>] extends [never] ? [Exclude<Tags, TagsIn<Text>>] extends [never] ? CheckLength<Locale, Text, Limit> : `${Locale} leaves out <${Exclude<Tags, TagsIn<Text>>}>` : `${Locale} uses <${Exclude<TagsIn<Text>, Tags>}>, which the message does not have` : `${Locale} leaves out {${Exclude<Params, ParamsIn<Text>>}}`;
99
+ type LengthOf<Text extends string, Count extends unknown[] = []> = Text extends `${string}${infer Rest}` ? LengthOf<Rest, [...Count, unknown]> : Count['length'];
100
+ type IsLonger<Text extends string, Limit extends number, Count extends unknown[] = []> = Count['length'] extends Limit ? Text extends '' ? false : true : Text extends `${string}${infer Rest}` ? IsLonger<Rest, Limit, [...Count, unknown]> : false;
101
+ type CheckLength<Locale extends string, Text extends string, Limit extends number> = [Limit] extends [never] ? Text : number extends Limit ? Text : [ParamsIn<Text>] extends [never] ? IsLonger<Text, Limit> extends true ? `${Locale} is ${LengthOf<Text>} characters, the limit is ${Limit}` : Text : Text;
102
+ type BaseTextOf<Texts, Base extends string> = Base extends keyof Texts ? Texts[Base] : never;
103
+ type ParamsOf<Declaration> = Declaration extends {
104
+ params: infer Params;
105
+ } ? keyof Params & string : never;
106
+ type LimitOf<Declaration> = Declaration extends {
107
+ maxLength: infer Limit extends number;
108
+ } ? Limit : never;
109
+ /**
110
+ * Checks every language of a message against its declared params, the base language's tags and
111
+ * its length limit. A wrong text becomes the sentence that says what is wrong, so the compiler
112
+ * shows it on that text.
113
+ */
114
+ type CheckTexts<Texts, Locales, Base extends string, Params extends string, Limit extends number> = { [Locale in keyof Texts]: Locale extends keyof Locales ? CheckText<Locale & string, Texts[Locale], Params, TagsIn<BaseTextOf<Texts, Base>>, Limit> : `${Locale & string} is not one of the languages` } & { [Locale in RequiredLocalesOf<Locales>]: string };
115
+ /**
116
+ * A text param made by `i18n.string`.
117
+ */
118
+ type StringParam = {
119
+ /**
120
+ * Sets the value your editor shows in place of this param.
121
+ *
122
+ * @example
123
+ * i18n.string().example('Ada');
124
+ */
125
+ example: (value: string) => StringParam;
126
+ readonly [paramSource]: unknown;
127
+ };
128
+ /**
129
+ * A number param made by `i18n.number`.
130
+ */
131
+ type NumberParam = {
132
+ /**
133
+ * Sets the value your editor shows in place of this param.
134
+ *
135
+ * @example
136
+ * i18n.number().example(3);
137
+ */
138
+ example: (value: number) => NumberParam;
139
+ readonly [paramSource]: unknown;
140
+ };
141
+ /**
142
+ * What a message is: what it is for, its length limit and its params.
143
+ */
144
+ type MessageDeclaration = {
145
+ /**
146
+ * Tells whoever translates the message what it is for.
147
+ */
148
+ description?: string;
149
+ /**
150
+ * The most characters the message may have, in every language.
151
+ */
152
+ maxLength?: number;
153
+ /**
154
+ * The params every language uses, like `{firstName}`.
155
+ *
156
+ * @example
157
+ * params: {
158
+ * firstName: i18n.string().example('Ada'),
159
+ * },
160
+ */
161
+ params?: Record<string, StringParam | NumberParam>;
162
+ };
163
+ /**
164
+ * A message that is declared and waits for its texts.
165
+ */
166
+ type DeclaredMessage<Declaration, Locales, Base extends string> = {
167
+ /**
168
+ * Gives the message one text per language.
169
+ *
170
+ * @example
171
+ * i18n.message().translations({
172
+ * en: 'Your cart',
173
+ * nb: 'Handlekurven din',
174
+ * });
175
+ */
176
+ translations: <const Texts extends Record<string, string>>(texts: CheckTexts<Texts, Locales, Base, ParamsOf<Declaration>, LimitOf<Declaration>>) => Message;
177
+ };
178
+ /**
179
+ * A message made by `i18n.message`.
180
+ */
181
+ type Message = {
182
+ readonly [messageSource]: unknown;
183
+ };
184
+ /**
185
+ * Messages by name. A plain object inside is a nested group, like `t.checkout.form.email`.
186
+ */
187
+ type MessageTree = {
188
+ [name: string]: Message | MessageTree;
189
+ };
190
+ /**
191
+ * A group of messages made by `i18n.messages`. `kizuna-i18n generate` reads every group the
192
+ * translations folder exports, named by its export.
193
+ */
194
+ type Messages = {
195
+ readonly [messagesSource]: unknown;
196
+ };
197
+ /**
198
+ * What `defineConfig` returns, which declares the messages.
199
+ */
200
+ type I18n<Locales extends Record<string, LocaleOptions>, Base extends keyof Locales & string> = {
201
+ /**
202
+ * Declares a message. `.translations` then gives it one text per language.
203
+ *
204
+ * @example
205
+ * i18n
206
+ * .message({
207
+ * description: 'Shown above the cart to a customer who is logged in',
208
+ * params: {
209
+ * firstName: i18n.string().example('Ada'),
210
+ * },
211
+ * })
212
+ * .translations({
213
+ * en: 'Welcome back, {firstName}',
214
+ * nb: 'Velkommen tilbake, {firstName}',
215
+ * });
216
+ */
217
+ message: <const Declaration extends MessageDeclaration = {}>(declaration?: Declaration) => DeclaredMessage<Declaration, Locales, Base>;
218
+ /**
219
+ * Declares a text param.
220
+ *
221
+ * @example
222
+ * i18n.string().example('Ada');
223
+ */
224
+ string: () => StringParam;
225
+ /**
226
+ * Declares a number param.
227
+ *
228
+ * @example
229
+ * i18n.number().example(3);
230
+ */
231
+ number: () => NumberParam;
232
+ /**
233
+ * Groups messages. Each key names a message, like `t.cart.title`, and a plain object nests a
234
+ * group, like `t.cart.summary.total`.
235
+ *
236
+ * @example
237
+ * export const cart = i18n.messages({
238
+ * title: i18n.message().translations({
239
+ * en: 'Your cart',
240
+ * nb: 'Handlekurven din',
241
+ * }),
242
+ * summary: {
243
+ * total: i18n.message().translations({
244
+ * en: 'Total',
245
+ * nb: 'Totalt',
246
+ * }),
247
+ * },
248
+ * });
249
+ */
250
+ messages: (messages: MessageTree) => Messages;
251
+ };
252
+ //#endregion
253
+ export { Messages as a, ParamDefinition as c, Message as i, Languages as n, LocaleDefinition as o, LocaleOptions as r, MessagesSource as s, I18n as t };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kizunajs/i18n",
3
- "version": "0.1.1",
3
+ "version": "0.3.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",
@@ -1,11 +0,0 @@
1
- //#region src/source.ts
2
- /**
3
- * Where a message made by `i18n.message` keeps its definition.
4
- */
5
- const messageSource = Symbol.for("kizuna-i18n.message");
6
- /**
7
- * Where a group made by `i18n.messages` keeps its messages.
8
- */
9
- const messagesSource = Symbol.for("kizuna-i18n.messages");
10
- //#endregion
11
- export { messagesSource as n, messageSource as t };
@@ -1,35 +0,0 @@
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
- * A language, and the language it falls back to.
12
- */
13
- type LocaleDefinition = {
14
- name: string;
15
- fallback?: string;
16
- };
17
- /**
18
- * One message as `i18n.message` and its chained methods declare it.
19
- */
20
- type MessageDefinition = {
21
- translations: Record<string, string>;
22
- description?: string;
23
- maxLength?: number;
24
- examples?: Record<string, string>;
25
- };
26
- /**
27
- * Every group of an app's messages, by name, with the languages they were made for.
28
- */
29
- type MessagesSource = {
30
- base: string;
31
- locales: LocaleDefinition[];
32
- groups: Record<string, Record<string, MessageDefinition>>;
33
- };
34
- //#endregion
35
- export { messagesSource as i, MessagesSource as n, messageSource as r, LocaleDefinition as t };
@@ -1,22 +0,0 @@
1
- //#region src/source.ts
2
- /**
3
- * Where a message made by `i18n.message` keeps its definition.
4
- */
5
- const messageSource = Symbol.for("kizuna-i18n.message");
6
- /**
7
- * Where a group made by `i18n.messages` keeps its messages.
8
- */
9
- const messagesSource = Symbol.for("kizuna-i18n.messages");
10
- //#endregion
11
- Object.defineProperty(exports, "messageSource", {
12
- enumerable: true,
13
- get: function() {
14
- return messageSource;
15
- }
16
- });
17
- Object.defineProperty(exports, "messagesSource", {
18
- enumerable: true,
19
- get: function() {
20
- return messagesSource;
21
- }
22
- });
@@ -1,35 +0,0 @@
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
- * A language, and the language it falls back to.
12
- */
13
- type LocaleDefinition = {
14
- name: string;
15
- fallback?: string;
16
- };
17
- /**
18
- * One message as `i18n.message` and its chained methods declare it.
19
- */
20
- type MessageDefinition = {
21
- translations: Record<string, string>;
22
- description?: string;
23
- maxLength?: number;
24
- examples?: Record<string, string>;
25
- };
26
- /**
27
- * Every group of an app's messages, by name, with the languages they were made for.
28
- */
29
- type MessagesSource = {
30
- base: string;
31
- locales: LocaleDefinition[];
32
- groups: Record<string, Record<string, MessageDefinition>>;
33
- };
34
- //#endregion
35
- export { messagesSource as i, MessagesSource as n, messageSource as r, LocaleDefinition as t };