@softistx/alxia-i18n 0.0.0-stage → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +262 -2
- package/dist/catalogue.d.ts +44 -0
- package/dist/catalogue.d.ts.map +1 -0
- package/dist/chain.d.ts +33 -0
- package/dist/chain.d.ts.map +1 -0
- package/dist/codes.d.ts +9 -0
- package/dist/codes.d.ts.map +1 -0
- package/dist/coverage.d.ts +17 -0
- package/dist/coverage.d.ts.map +1 -0
- package/dist/i18n.d.ts +29 -0
- package/dist/i18n.d.ts.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +266 -0
- package/dist/index.js.map +16 -0
- package/dist/lookup.d.ts +18 -0
- package/dist/lookup.d.ts.map +1 -0
- package/dist/seeds.d.ts +35 -0
- package/dist/seeds.d.ts.map +1 -0
- package/dist/types.d.ts +106 -0
- package/dist/types.d.ts.map +1 -0
- package/docs/README.md +13 -0
- package/docs/guide.md +924 -0
- package/docs/roadmap.md +95 -0
- package/docs/troubleshooting.md +663 -0
- package/package.json +54 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Steve Tsala
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,263 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @softistx/alxia-i18n
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> Moved from `@alxia/i18n`: see the [migration guide](https://github.com/softistx/alxia/blob/develop/packages/core/docs/guide/migrating-from-alxia-scope.md).
|
|
4
|
+
|
|
5
|
+
Translations for [alxia](https://www.npmjs.com/package/@softistx/alxia), on
|
|
6
|
+
[`@softistx/i18n`](https://github.com/softistx/nxgt-core/tree/develop/packages/i18n): `t()` bound to the
|
|
7
|
+
request's language, keys typed by your catalogue, ICU messages — plurals,
|
|
8
|
+
selects, numbers.
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
bun add @softistx/alxia-i18n @softistx/alxia-language @softistx/i18n@^3.2 @softistx/alxia
|
|
12
|
+
bun add -d typescript
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
`@softistx/i18n` is public on npmjs: no scope entry or token is needed.
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { alxia } from '@softistx/alxia';
|
|
21
|
+
import { createI18n } from '@softistx/alxia-i18n';
|
|
22
|
+
import { resources as shared } from '@softistx/i18n';
|
|
23
|
+
|
|
24
|
+
const en = { ...shared.en, cart: { items: '{count, plural, =0 {No items} one {One item} other {# items}}' } };
|
|
25
|
+
const fr = { ...shared.fr, cart: { items: '{count, plural, =0 {Aucun article} one {Un article} other {# articles}}' } };
|
|
26
|
+
|
|
27
|
+
export const i18n = createI18n({ resources: { en, fr }, fallback: 'en' });
|
|
28
|
+
|
|
29
|
+
const app = alxia()
|
|
30
|
+
.use(i18n)
|
|
31
|
+
.get('/cart', ({ t, reply }) => reply(200, t('cart.items', { count: 3 }))); // '3 articles' in French
|
|
32
|
+
|
|
33
|
+
app.listen(3000);
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
- **The language** is `@softistx/alxia-language`'s, among the catalogues' languages:
|
|
37
|
+
the query, a cookie, `Accept-Language`, then `fallback`. Its options pass
|
|
38
|
+
through: `createI18n({ resources, fallback, order: ['path', 'header'] })`.
|
|
39
|
+
- **Keys** are typed by the fallback's catalogue in the `resources` form, and by
|
|
40
|
+
the type argument with an explicit `Key` (below): `t('cart.itmes')` is a
|
|
41
|
+
compile error. A key a language lacks answers itself, unless you give it a [chain](#a-key-missing-in-the-requests-language).
|
|
42
|
+
- **`@softistx/i18n`'s shared keys** — `errors.not-found`, `zod.*` — are yours by
|
|
43
|
+
spreading its `resources` into your catalogues.
|
|
44
|
+
|
|
45
|
+
## A key missing in the request's language
|
|
46
|
+
|
|
47
|
+
Give `t()` more languages to try, so that a key a catalogue lacks answers
|
|
48
|
+
in the next one instead of answering itself:
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
import { alxia } from '@softistx/alxia';
|
|
52
|
+
import { createI18n } from '@softistx/alxia-i18n';
|
|
53
|
+
|
|
54
|
+
const en = { home: { title: 'Welcome', soon: 'Coming soon' } };
|
|
55
|
+
const fr = { home: { title: 'Bienvenue' } };
|
|
56
|
+
const pt = { home: { title: 'Bem-vindo' } };
|
|
57
|
+
const ptBR = {};
|
|
58
|
+
|
|
59
|
+
export const i18n = createI18n({
|
|
60
|
+
resources: { en, fr, pt, 'pt-BR': ptBR },
|
|
61
|
+
fallback: 'en',
|
|
62
|
+
fallbacks: (language) => (language === 'pt-BR' ? ['pt'] : []), // pt-BR, then pt
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
alxia()
|
|
66
|
+
.use(i18n)
|
|
67
|
+
.derive(({ request }) => {
|
|
68
|
+
// once the app knows more, for one request: the school's language
|
|
69
|
+
i18n.fallBackTo(request.headers.get('x-tenant-language') ?? 'fr');
|
|
70
|
+
return {};
|
|
71
|
+
})
|
|
72
|
+
.get('/', ({ t, reply }) => reply(200, [t('home.title'), t('home.soon'), i18n.chain()]));
|
|
73
|
+
// ?lang=pt-BR, x-tenant-language: fr → ['Bienvenue', 'Coming soon', ['pt-BR', 'fr', 'pt', 'en']]
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
`t(key)` tries `[language, ...fallBackTo steps, ...fallbacks(language),
|
|
77
|
+
fallback]`, each once, and answers the key only when all miss.
|
|
78
|
+
`i18n.chain()` is that list, for data the app labels itself. Without
|
|
79
|
+
`fallbacks` and `fallBackTo`, `t()` reads the request's language alone, as
|
|
80
|
+
before; once `fallbacks` is given, or `fallBackTo()` called, `fallback` joins its lookup.
|
|
81
|
+
The chain is walked by `@softistx/alxia-i18n` alone: `@softistx/i18n` 3's own fallback
|
|
82
|
+
language (`getFallbackLanguage()`, `'en'` unless set) is never tried for `t()`,
|
|
83
|
+
so a key no language of the chain holds answers itself.
|
|
84
|
+
|
|
85
|
+
## Catalogues loaded at runtime
|
|
86
|
+
|
|
87
|
+
Name the keys with a type argument, and give the catalogues when you have
|
|
88
|
+
them; a request keeps the catalogues it started with:
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
import { createI18n } from '@softistx/alxia-i18n';
|
|
92
|
+
|
|
93
|
+
type Key = 'home.title' | 'cart.items';
|
|
94
|
+
|
|
95
|
+
export const i18n = createI18n<Key>({ fallback: 'en' });
|
|
96
|
+
// later, from a database or a file: all at once, `supported` follows
|
|
97
|
+
i18n.setResources({
|
|
98
|
+
en: { home: { title: 'Welcome' }, cart: { items: '{count} items' } },
|
|
99
|
+
fr: { home: { title: 'Bienvenue' }, cart: { items: '{count} articles' } },
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
i18n.supported; // ['en', 'fr']
|
|
103
|
+
i18n.t('home.title'); // 'Welcome'
|
|
104
|
+
i18n.t('home.titel'); // a compile error: not a Key
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
With a type argument, `Ctx` — what `resolve` reads — is no longer inferred:
|
|
108
|
+
name it third, `createI18n<Key, 'en' | 'fr', BaseContext & { user: User }>(…)`.
|
|
109
|
+
A `fallback` with no catalogue throws at start-up
|
|
110
|
+
([troubleshooting](https://github.com/softistx/alxia/blob/develop/packages/i18n/docs/troubleshooting.md#typeerror-createi18n-there-is-no-catalogue-for-the-fallback-de)).
|
|
111
|
+
|
|
112
|
+
## Reading the app's context
|
|
113
|
+
|
|
114
|
+
Annotate `resolve`'s parameter to speak the language a signed-in user saved.
|
|
115
|
+
The middleware then requires what it reads: an app that does not give `user`
|
|
116
|
+
before it cannot use it.
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
import { alxia, type BaseContext } from '@softistx/alxia';
|
|
120
|
+
import { createI18n } from '@softistx/alxia-i18n';
|
|
121
|
+
|
|
122
|
+
const en = { home: { title: 'Welcome' } };
|
|
123
|
+
const fr = { home: { title: 'Bienvenue' } };
|
|
124
|
+
|
|
125
|
+
const auth = alxia().derive(({ request }) => ({
|
|
126
|
+
user: request.headers.has('authorization') ? { language: 'fr' } : null, // your sign-in
|
|
127
|
+
}));
|
|
128
|
+
|
|
129
|
+
const byUser = createI18n({
|
|
130
|
+
resources: { en, fr },
|
|
131
|
+
fallback: 'en',
|
|
132
|
+
order: ['query'],
|
|
133
|
+
resolve: ({ user }: BaseContext & { user: { language: string } | null }) => user?.language,
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
alxia().plugin(auth).use(byUser); // auth derives user
|
|
137
|
+
alxia().use(byUser); // a compile error: this app gives no `user`
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
Unannotated, `resolve` reads the request alone and the middleware requires
|
|
141
|
+
nothing; annotated `any`, it is refused on every app.
|
|
142
|
+
|
|
143
|
+
## Anywhere
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
// a service, or an error's message: no language passed
|
|
147
|
+
export const describeCart = (count: number) => i18n.t('cart.items', { count });
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
`i18n.t()` translates in the language of the request it runs in — through
|
|
151
|
+
every `await`, in every middleware after `use(i18n)`, and in the answer to
|
|
152
|
+
an error — and in the fallback outside one, or before the language is
|
|
153
|
+
read: in a middleware declared before it, or a route declared before it
|
|
154
|
+
([troubleshooting](https://github.com/softistx/alxia/blob/develop/packages/i18n/docs/troubleshooting.md#i18nt-answers-in-the-fallback-before-the-language-is-read)).
|
|
155
|
+
`i18n.language()` says which.
|
|
156
|
+
|
|
157
|
+
### `@softistx/i18n`'s own `translate`
|
|
158
|
+
|
|
159
|
+
`createI18n()` registers the request's language as one of `@softistx/i18n`'s
|
|
160
|
+
language sources: its `getLanguage()` and `translate` — and every nxgt
|
|
161
|
+
package that translates through them, an error's message — speak the alxia
|
|
162
|
+
request's language too, for a language `@softistx/i18n` has a catalogue for
|
|
163
|
+
(`en`, `fr`, and what `registerCatalogue()` adds).
|
|
164
|
+
|
|
165
|
+
Upgrading from 0.4.x to 0.5.0: the peer moved from `@nxgt/i18n` to
|
|
166
|
+
`@softistx/i18n` `^3.2.1`. Replace one by the other in `package.json` and in
|
|
167
|
+
imports, bump every package that peers it together, and remove `@nxgt/i18n`:
|
|
168
|
+
two copies do not share catalogue state
|
|
169
|
+
([troubleshooting](https://github.com/softistx/alxia/blob/develop/packages/i18n/docs/troubleshooting.md#two-copies-of-the-i18n-core-after-the-move-to-softistxi18n)).
|
|
170
|
+
|
|
171
|
+
Upgrading from `@softistx/i18n` 2: bump it to `^3` in every package of the app; a
|
|
172
|
+
2.x copy does not share 3.x's catalogue state
|
|
173
|
+
([troubleshooting](https://github.com/softistx/alxia/blob/develop/packages/i18n/docs/troubleshooting.md#a-catalogue-registered-with-softistxi18n-is-not-seen-by-translate)).
|
|
174
|
+
|
|
175
|
+
## Cached responses
|
|
176
|
+
|
|
177
|
+
A response in the request's language varies by what decided it: give
|
|
178
|
+
`@softistx/alxia-cache` the same headers, `vary: ['accept-language', 'cookie']`.
|
|
179
|
+
|
|
180
|
+
## Messages from a database
|
|
181
|
+
|
|
182
|
+
For an app whose messages live in a translations collection (rows
|
|
183
|
+
`{ key, language, text }`). Nothing here talks to a database: you read the
|
|
184
|
+
rows, the package builds the catalogue, and it never depends on a client.
|
|
185
|
+
|
|
186
|
+
```ts
|
|
187
|
+
import { assertCoverage, catalogueOf, defineCodes, resolveCatalogue } from '@softistx/alxia-i18n';
|
|
188
|
+
|
|
189
|
+
// 1. Declare the codes: 'nodes.errors.not-found', 'nodes.errors.locked'.
|
|
190
|
+
export const nodeCodes = defineCodes('nodes', ['not-found', 'locked']);
|
|
191
|
+
|
|
192
|
+
// 2. Rows from your collection, e.g. `await db.collection('translations').find().toArray()`.
|
|
193
|
+
const rows = [
|
|
194
|
+
{ key: 'nodes.errors.not-found', language: 'en', text: 'Node not found' },
|
|
195
|
+
{ key: 'nodes.errors.not-found', language: 'fr', text: 'Nœud introuvable' },
|
|
196
|
+
{ key: 'nodes.errors.locked', language: 'en', text: 'Node locked' },
|
|
197
|
+
];
|
|
198
|
+
|
|
199
|
+
// 3. Refuse at load time a declared code the fallback language lacks.
|
|
200
|
+
// Throws, naming the codes; `{ onMissing: 'report' }` returns them instead.
|
|
201
|
+
assertCoverage(nodeCodes, catalogueOf(rows, 'en'), { language: 'en' });
|
|
202
|
+
|
|
203
|
+
// 4. A language's chain `[own, fallback]`, its messages and its stable version.
|
|
204
|
+
const catalogue = resolveCatalogue(rows, ['fr', 'en']);
|
|
205
|
+
catalogue.messages(nodeCodes); // locked falls back to English
|
|
206
|
+
catalogue.version; // 32 hex chars: same on every instance, changes with any text
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
`version` is the first 32 hex characters of the SHA-256 of the chain's key-sorted JSON, so row order and
|
|
210
|
+
key order do not change it. A key that is both a message and a branch
|
|
211
|
+
(`a` and `a.b`), a key with an empty segment (`a..b`) and a prototype name throw.
|
|
212
|
+
|
|
213
|
+
### Migration seeds
|
|
214
|
+
|
|
215
|
+
```ts
|
|
216
|
+
import { removeTexts, restoreTexts, upsertTexts, type TextStore } from '@softistx/alxia-i18n';
|
|
217
|
+
|
|
218
|
+
// Your adapter over the driver: two writes.
|
|
219
|
+
declare const store: TextStore;
|
|
220
|
+
|
|
221
|
+
const added = { 'nodes.errors.locked': { en: 'Node locked', fr: 'Nœud verrouillé' } };
|
|
222
|
+
const previous = { 'nodes.errors.not-found': { en: 'Not found', fr: 'Introuvable' } };
|
|
223
|
+
const reworded = { 'nodes.errors.not-found': { en: 'Node not found', fr: 'Nœud introuvable' } };
|
|
224
|
+
|
|
225
|
+
export const up = async () => {
|
|
226
|
+
await upsertTexts(store, added);
|
|
227
|
+
await upsertTexts(store, reworded);
|
|
228
|
+
};
|
|
229
|
+
export const down = async () => {
|
|
230
|
+
await removeTexts(store, added); // codes this migration CREATED: delete them
|
|
231
|
+
await restoreTexts(store, previous); // codes it REWORDED: write the old text back
|
|
232
|
+
};
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
Use `removeTexts` for codes the migration created and `restoreTexts` for a
|
|
236
|
+
rewording; `removeTexts` on a reworded code would delete a message that
|
|
237
|
+
existed before the migration. [More in the guide](docs/guide.md#messages-from-a-database).
|
|
238
|
+
|
|
239
|
+
## API
|
|
240
|
+
|
|
241
|
+
| export | |
|
|
242
|
+
| --- | --- |
|
|
243
|
+
| `createI18n({ resources, fallback, fallbacks?, …languageOptions })` | the middleware, given to `app.use` — what is after it reads `t` and `language` — with `t()`, `language()`, `supported`, `fallBackTo()`, `chain()` and `setResources()` |
|
|
244
|
+
| `createI18n<Key>({ languages?, fallback, resources?, … })` | the same with an explicit `Key` for the keys (and, third, `Ctx` when `resolve` reads the app's context), and catalogues that may come later, from `setResources()` |
|
|
245
|
+
| `i18n.fallBackTo(language)`, `i18n.chain()` | add a step to the current request's fallback chain; list the chain |
|
|
246
|
+
| `i18n.setResources(resources)` | swaps the catalogues at once; `supported` follows; a request running keeps its own |
|
|
247
|
+
| `I18nOptions`, `RuntimeI18nOptions` | its options: `resources`, `fallback`, `fallbacks`, and every `@softistx/alxia-language` option but `supported`; `resolve` may be annotated to read the app's context |
|
|
248
|
+
| `KeyOf<Catalogue>` | a catalogue's dotted keys, nine levels deep; a deeper section gives `section.${string}` |
|
|
249
|
+
| `Translate<Key>`, `Catalogues`, `I18nContext<Key>` | its types |
|
|
250
|
+
| `I18nMiddleware<Language, Key, Requires>` | what `createI18n()` returns: a middleware adding `language` and `t`, with `t()`, `language()`, `supported`, `fallBackTo()`, `chain()` and `setResources()` |
|
|
251
|
+
| `defineCodes(area, slugs)` | `<area>.errors.<slug>` literals with their union type |
|
|
252
|
+
| `catalogueOf(rows, language)`, `resolveCatalogue(rows, [own, fallback])` | a language's message tree from `{ key, language, text }` rows; its chain, `messages(codes)` and `version` |
|
|
253
|
+
| `catalogueVersion(chain)`, `messagesOf(chain, codes)`, `stableJson(value)` | the pieces behind them |
|
|
254
|
+
| `seedsOf`, `upsertTexts`, `removeTexts`, `restoreTexts`, `TextStore` | migration seeds over a two-method store |
|
|
255
|
+
| `assertCoverage(codes, fallbackTree, options?)`, `uncovered(codes, tree)` | the load-time check: throws, or reports, the declared codes with no text |
|
|
256
|
+
| `has(catalogue, key)`, `messageAt(catalogue, key)` | whether a catalogue has a message at a dotted key; the message, or `undefined` (a flat key is tried first) |
|
|
257
|
+
| `Code<Area, Slug>`, `TextRow`, `Tree`, `MessageView`, `ResolvedCatalogue`, `CoverageOptions`, `Texts` | the types: a `<area>.errors.<slug>` literal, a `{ key, language, text }` row, a nested message tree, a `{ code, message }` pair, what `resolveCatalogue` returns, the options of `assertCoverage`, and the code-first `{ code: { en, fr } }` texts of a migration |
|
|
258
|
+
|
|
259
|
+
## Documentation
|
|
260
|
+
|
|
261
|
+
- [Guide](https://github.com/softistx/alxia/tree/develop/packages/i18n/docs): the catalogues and every option, reading what an earlier middleware added, what the routes read, ICU messages and typed keys, `t()` outside a route, and `@softistx/i18n`'s own `translate`.
|
|
262
|
+
- [Troubleshooting](https://github.com/softistx/alxia/blob/develop/packages/i18n/docs/troubleshooting.md): an error, a key shown instead of a message, or a response in the wrong language, and what to do about it.
|
|
263
|
+
- [Roadmap](https://github.com/softistx/alxia/blob/develop/packages/i18n/docs/roadmap.md): what is coming, and what is not planned.
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/** A row of a translations collection. */
|
|
2
|
+
export interface TextRow {
|
|
3
|
+
key: string;
|
|
4
|
+
language: string;
|
|
5
|
+
text: string;
|
|
6
|
+
}
|
|
7
|
+
/** A catalogue as `createI18n` reads it: dotted keys as a nested tree. */
|
|
8
|
+
export type Tree = {
|
|
9
|
+
[key: string]: string | Tree;
|
|
10
|
+
};
|
|
11
|
+
export interface MessageView {
|
|
12
|
+
code: string;
|
|
13
|
+
message: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The message tree of one language: the rows that have a text in it
|
|
17
|
+
* (`auth.errors.forbidden` is `{ auth: { errors: { forbidden } } }`). A key
|
|
18
|
+
* that is both a message and the start of another key, that has an empty
|
|
19
|
+
* segment (`a..b`, ``) or that names a prototype member throws: the tree
|
|
20
|
+
* cannot hold it.
|
|
21
|
+
*/
|
|
22
|
+
export declare function catalogueOf(rows: readonly TextRow[], language: string): Tree;
|
|
23
|
+
/** Every code's message, each from the first tree of `chain` that has it; a code none has is left out. */
|
|
24
|
+
export declare function messagesOf(chain: readonly Tree[], codes: readonly string[]): MessageView[];
|
|
25
|
+
/** JSON with the keys of every object sorted: the same tree is the same string whatever the order it was built in. */
|
|
26
|
+
export declare function stableJson(value: unknown): string;
|
|
27
|
+
/**
|
|
28
|
+
* The version of what a language serves: 32 hex characters of the SHA-256 of
|
|
29
|
+
* its chain's key-sorted JSON. It changes with any text and is the same
|
|
30
|
+
* after a restart and on every instance.
|
|
31
|
+
*/
|
|
32
|
+
export declare function catalogueVersion(chain: readonly Tree[]): string;
|
|
33
|
+
export interface ResolvedCatalogue {
|
|
34
|
+
/** The first language of the chain. */
|
|
35
|
+
language: string;
|
|
36
|
+
/** One tree per language of the chain, own language first. */
|
|
37
|
+
chain: Tree[];
|
|
38
|
+
version: string;
|
|
39
|
+
/** The messages of `codes`, each from the first language that has it. */
|
|
40
|
+
messages(codes: readonly string[]): MessageView[];
|
|
41
|
+
}
|
|
42
|
+
/** Resolves `rows` for a language chain (`[own, fallback]`): its trees, its version and its messages. */
|
|
43
|
+
export declare function resolveCatalogue(rows: readonly TextRow[], languages: readonly [string, ...string[]]): ResolvedCatalogue;
|
|
44
|
+
//# sourceMappingURL=catalogue.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"catalogue.d.ts","sourceRoot":"","sources":["../src/catalogue.ts"],"names":[],"mappings":"AAGA,0CAA0C;AAC1C,MAAM,WAAW,OAAO;IACvB,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;CACb;AAED,0EAA0E;AAC1E,MAAM,MAAM,IAAI,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAA;CAAE,CAAC;AAEpD,MAAM,WAAW,WAAW;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;CAChB;AAOD;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,SAAS,OAAO,EAAE,EAAE,QAAQ,EAAE,MAAM,GAAG,IAAI,CAyB5E;AAED,0GAA0G;AAC1G,wBAAgB,UAAU,CACzB,KAAK,EAAE,SAAS,IAAI,EAAE,EACtB,KAAK,EAAE,SAAS,MAAM,EAAE,GACtB,WAAW,EAAE,CAWf;AAED,sHAAsH;AACtH,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAQjD;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,SAAS,IAAI,EAAE,GAAG,MAAM,CAK/D;AAED,MAAM,WAAW,iBAAiB;IACjC,uCAAuC;IACvC,QAAQ,EAAE,MAAM,CAAC;IACjB,8DAA8D;IAC9D,KAAK,EAAE,IAAI,EAAE,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,yEAAyE;IACzE,QAAQ,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,WAAW,EAAE,CAAC;CAClD;AAED,yGAAyG;AACzG,wBAAgB,gBAAgB,CAC/B,IAAI,EAAE,SAAS,OAAO,EAAE,EACxB,SAAS,EAAE,SAAS,CAAC,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC,GACvC,iBAAiB,CAQnB"}
|
package/dist/chain.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type TranslationContext } from '@softistx/i18n';
|
|
2
|
+
import type { Catalogues, Translate } from './types';
|
|
3
|
+
/** What a request keeps from the catalogues it started with. */
|
|
4
|
+
export interface Snapshot {
|
|
5
|
+
readonly resources: Catalogues;
|
|
6
|
+
readonly translator: (key: string, context?: TranslationContext, language?: string) => string;
|
|
7
|
+
}
|
|
8
|
+
/** One request's frame, opened around everything after the middleware. */
|
|
9
|
+
export interface Frame {
|
|
10
|
+
readonly snapshot: Snapshot;
|
|
11
|
+
/** `fallBackTo`'s steps, in order. */
|
|
12
|
+
readonly steps: string[];
|
|
13
|
+
language?: string;
|
|
14
|
+
}
|
|
15
|
+
/** The catalogues, ready to translate. */
|
|
16
|
+
export declare const snapshotOf: (resources: Catalogues) => Snapshot;
|
|
17
|
+
/** What decides a chain: the `fallback` and the `fallbacks` option. */
|
|
18
|
+
export interface Steps {
|
|
19
|
+
readonly fallback: string;
|
|
20
|
+
readonly fallbacks?: ((language: string) => readonly string[]) | undefined;
|
|
21
|
+
}
|
|
22
|
+
/** The chain of a request (none: outside one): its language, its steps, `fallbacks`, `fallback`. */
|
|
23
|
+
export declare function chainOf({ fallback, fallbacks }: Steps, frame: Frame | undefined): string[];
|
|
24
|
+
/**
|
|
25
|
+
* `t` for the frame `of` answers (none: outside a request, the `live`
|
|
26
|
+
* catalogues): its snapshot's catalogues, its chain. The language that holds
|
|
27
|
+
* the key is found here, in the chain's order, and handed to `@softistx/i18n`,
|
|
28
|
+
* whose own fallback lookup therefore never runs. Until the chain is used —
|
|
29
|
+
* no `fallbacks`, no step — only the request's language is tried, and a key it
|
|
30
|
+
* lacks answers itself.
|
|
31
|
+
*/
|
|
32
|
+
export declare function translateFor(steps: Steps, of: () => Frame | undefined, live: () => Snapshot): Translate<string>;
|
|
33
|
+
//# sourceMappingURL=chain.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chain.d.ts","sourceRoot":"","sources":["../src/chain.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,KAAK,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAE3E,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAErD,gEAAgE;AAChE,MAAM,WAAW,QAAQ;IACxB,QAAQ,CAAC,SAAS,EAAE,UAAU,CAAC;IAC/B,QAAQ,CAAC,UAAU,EAAE,CACpB,GAAG,EAAE,MAAM,EACX,OAAO,CAAC,EAAE,kBAAkB,EAC5B,QAAQ,CAAC,EAAE,MAAM,KACb,MAAM,CAAC;CACZ;AAED,0EAA0E;AAC1E,MAAM,WAAW,KAAK;IACrB,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAC5B,sCAAsC;IACtC,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,CAAC;IACzB,QAAQ,CAAC,EAAE,MAAM,CAAC;CAClB;AAWD,0CAA0C;AAC1C,eAAO,MAAM,UAAU,GAAI,WAAW,UAAU,KAAG,QAGjD,CAAC;AAEH,uEAAuE;AACvE,MAAM,WAAW,KAAK;IACrB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC,QAAQ,EAAE,MAAM,KAAK,SAAS,MAAM,EAAE,CAAC,GAAG,SAAS,CAAC;CAC3E;AAED,oGAAoG;AACpG,wBAAgB,OAAO,CACtB,EAAE,QAAQ,EAAE,SAAS,EAAE,EAAE,KAAK,EAC9B,KAAK,EAAE,KAAK,GAAG,SAAS,GACtB,MAAM,EAAE,CAQV;AAED;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAC3B,KAAK,EAAE,KAAK,EACZ,EAAE,EAAE,MAAM,KAAK,GAAG,SAAS,EAC3B,IAAI,EAAE,MAAM,QAAQ,GAClB,SAAS,CAAC,MAAM,CAAC,CAgBnB"}
|
package/dist/codes.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** An error code, `<area>.errors.<slug>`. */
|
|
2
|
+
export type Code<Area extends string, Slug extends string> = `${Area}.errors.${Slug}`;
|
|
3
|
+
/**
|
|
4
|
+
* An area's error codes as `<area>.errors.<slug>` literals, with their union
|
|
5
|
+
* type. Write the area and the slugs as literals, so that a tool reading
|
|
6
|
+
* the calls can find them: `defineCodes('nodes', ['not-found'])`.
|
|
7
|
+
*/
|
|
8
|
+
export declare function defineCodes<const Area extends string, const Slugs extends readonly string[]>(area: Area, slugs: Slugs): readonly Code<Area, Slugs[number]>[];
|
|
9
|
+
//# sourceMappingURL=codes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codes.d.ts","sourceRoot":"","sources":["../src/codes.ts"],"names":[],"mappings":"AAAA,6CAA6C;AAC7C,MAAM,MAAM,IAAI,CACf,IAAI,SAAS,MAAM,EACnB,IAAI,SAAS,MAAM,IAChB,GAAG,IAAI,WAAW,IAAI,EAAE,CAAC;AAE7B;;;;GAIG;AACH,wBAAgB,WAAW,CAC1B,KAAK,CAAC,IAAI,SAAS,MAAM,EACzB,KAAK,CAAC,KAAK,SAAS,SAAS,MAAM,EAAE,EACpC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,GAAG,SAAS,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,EAAE,CAEhE"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Tree } from './catalogue';
|
|
2
|
+
export interface CoverageOptions {
|
|
3
|
+
/** Named in the error, e.g. the fallback language. */
|
|
4
|
+
language?: string;
|
|
5
|
+
/** `'throw'` (default) refuses; `'report'` returns the missing codes. */
|
|
6
|
+
onMissing?: 'throw' | 'report';
|
|
7
|
+
}
|
|
8
|
+
/** The declared `codes` that have no message in `catalogue`, in order. */
|
|
9
|
+
export declare function uncovered(codes: readonly string[], catalogue: Tree): string[];
|
|
10
|
+
/**
|
|
11
|
+
* The load-time coverage check: every declared code needs a text in the
|
|
12
|
+
* fallback language's catalogue, or `catalogueOf`-built catalogues would
|
|
13
|
+
* silently leave it out. Throws naming the missing codes, or with
|
|
14
|
+
* `onMissing: 'report'` returns them. Returns `[]` when all are covered.
|
|
15
|
+
*/
|
|
16
|
+
export declare function assertCoverage(codes: readonly string[], fallback: Tree, options?: CoverageOptions): string[];
|
|
17
|
+
//# sourceMappingURL=coverage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coverage.d.ts","sourceRoot":"","sources":["../src/coverage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAGxC,MAAM,WAAW,eAAe;IAC/B,sDAAsD;IACtD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,yEAAyE;IACzE,SAAS,CAAC,EAAE,OAAO,GAAG,QAAQ,CAAC;CAC/B;AAED,0EAA0E;AAC1E,wBAAgB,SAAS,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,EAAE,SAAS,EAAE,IAAI,GAAG,MAAM,EAAE,CAE7E;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAC7B,KAAK,EAAE,SAAS,MAAM,EAAE,EACxB,QAAQ,EAAE,IAAI,EACd,OAAO,GAAE,eAAoB,GAC3B,MAAM,EAAE,CAUV"}
|
package/dist/i18n.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type BaseContext, type RequiresOf } from '@softistx/alxia';
|
|
2
|
+
import type { Catalogues, I18nMiddleware, I18nOptions, KeyOf, RuntimeI18nOptions } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Translations, as a middleware, on [`@softistx/i18n`](https://www.npmjs.com/package/@softistx/i18n):
|
|
5
|
+
* `@softistx/alxia-language` reads the request's language among the catalogues', and
|
|
6
|
+
* the routes declared after it read `language` and `t`, bound to it. Keys
|
|
7
|
+
* are typed by the fallback's catalogue; messages are ICU — plurals,
|
|
8
|
+
* selects, numbers — and a key missing in every language of the chain
|
|
9
|
+
* answers itself.
|
|
10
|
+
*
|
|
11
|
+
* Its own `t()` translates anywhere a request runs after it — a service,
|
|
12
|
+
* the answer to an error, a try/catch middleware's included — in that
|
|
13
|
+
* request's language, and in the fallback outside.
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* const i18n = createI18n({ resources: { en, fr }, fallback: 'en' });
|
|
17
|
+
* app.use(i18n).get('/', ({ t, reply }) => reply(200, t('home.title')));
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* Catalogues that load at runtime take their keys as a type argument:
|
|
21
|
+
* `createI18n<Key>({ fallback: 'en' })`, then `i18n.setResources(loaded)`.
|
|
22
|
+
*
|
|
23
|
+
* It registers the request's language as one of `@softistx/i18n`'s language
|
|
24
|
+
* sources, so its own `getLanguage()` and `translate` — and every nxgt
|
|
25
|
+
* package that translates through them — speak it too.
|
|
26
|
+
*/
|
|
27
|
+
export declare function createI18n<const C extends Catalogues, const Fallback extends keyof C & string, Ctx extends object = BaseContext>(options: I18nOptions<C, Fallback, Ctx>): I18nMiddleware<keyof C & string, KeyOf<C[Fallback]>, RequiresOf<Ctx, 'resolve'>>;
|
|
28
|
+
export declare function createI18n<Key extends string = never, Language extends string = string, Ctx extends object = BaseContext>(options: [Key] extends [never] ? 'name the keys with createI18n<Key>(…), or give resources whose fallback is one of their languages' : RuntimeI18nOptions<Language, Ctx>): I18nMiddleware<Language, Key, RequiresOf<Ctx, 'resolve'>>;
|
|
29
|
+
//# sourceMappingURL=i18n.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"i18n.d.ts","sourceRoot":"","sources":["../src/i18n.ts"],"names":[],"mappings":"AACA,OAAO,EACN,KAAK,WAAW,EAKhB,KAAK,UAAU,EAEf,MAAM,iBAAiB,CAAC;AAWzB,OAAO,KAAK,EACX,UAAU,EAEV,cAAc,EACd,WAAW,EACX,KAAK,EACL,kBAAkB,EAClB,MAAM,SAAS,CAAC;AAoDjB;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,UAAU,CACzB,KAAK,CAAC,CAAC,SAAS,UAAU,EAC1B,KAAK,CAAC,QAAQ,SAAS,MAAM,CAAC,GAAG,MAAM,EACvC,GAAG,SAAS,MAAM,GAAG,WAAW,EAEhC,OAAO,EAAE,WAAW,CAAC,CAAC,EAAE,QAAQ,EAAE,GAAG,CAAC,GACpC,cAAc,CAChB,MAAM,CAAC,GAAG,MAAM,EAChB,KAAK,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,EAClB,UAAU,CAAC,GAAG,EAAE,SAAS,CAAC,CAC1B,CAAC;AACF,wBAAgB,UAAU,CACzB,GAAG,SAAS,MAAM,GAAG,KAAK,EAC1B,QAAQ,SAAS,MAAM,GAAG,MAAM,EAChC,GAAG,SAAS,MAAM,GAAG,WAAW,EAEhC,OAAO,EAAE,CAAC,GAAG,CAAC,SAAS,CAAC,KAAK,CAAC,GAC3B,mGAAmG,GACnG,kBAAkB,CAAC,QAAQ,EAAE,GAAG,CAAC,GAClC,cAAc,CAAC,QAAQ,EAAE,GAAG,EAAE,UAAU,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type { MessageView, ResolvedCatalogue, TextRow, Tree, } from './catalogue';
|
|
2
|
+
export { catalogueOf, catalogueVersion, messagesOf, resolveCatalogue, stableJson, } from './catalogue';
|
|
3
|
+
export type { Code } from './codes';
|
|
4
|
+
export { defineCodes } from './codes';
|
|
5
|
+
export type { CoverageOptions } from './coverage';
|
|
6
|
+
export { assertCoverage, uncovered } from './coverage';
|
|
7
|
+
export { createI18n } from './i18n';
|
|
8
|
+
export { has, messageAt } from './lookup';
|
|
9
|
+
export type { TextStore, Texts } from './seeds';
|
|
10
|
+
export { removeTexts, restoreTexts, seedsOf, upsertTexts } from './seeds';
|
|
11
|
+
export type { Catalogues, I18nContext, I18nMiddleware, I18nOptions, KeyOf, RuntimeI18nOptions, Translate, } from './types';
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACX,WAAW,EACX,iBAAiB,EACjB,OAAO,EACP,IAAI,GACJ,MAAM,aAAa,CAAC;AACrB,OAAO,EACN,WAAW,EACX,gBAAgB,EAChB,UAAU,EACV,gBAAgB,EAChB,UAAU,GACV,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AACpC,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACtC,YAAY,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAClD,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACvD,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AACpC,OAAO,EAAE,GAAG,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAC1C,YAAY,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAC1E,YAAY,EACX,UAAU,EACV,WAAW,EACX,cAAc,EACd,WAAW,EACX,KAAK,EACL,kBAAkB,EAClB,SAAS,GACT,MAAM,SAAS,CAAC"}
|