@softistx/alxia-i18n 0.0.0-stage → 0.7.1
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
|
@@ -0,0 +1,663 @@
|
|
|
1
|
+
# Troubleshooting
|
|
2
|
+
|
|
3
|
+
Each entry is headed by the text you see: the error `createI18n()` throws,
|
|
4
|
+
an error from `tsc`, a line in the log, or — for a trap that prints
|
|
5
|
+
nothing — what the response does that you did not expect.
|
|
6
|
+
|
|
7
|
+
**Install**
|
|
8
|
+
|
|
9
|
+
- [`404 / no version matching, or 401 from npm.softistx.com`](#404--no-version-matching-or-401-from-npmsoftistxcom)
|
|
10
|
+
- [Two copies of the i18n core after the move to `@softistx/i18n`](#two-copies-of-the-i18n-core-after-the-move-to-softistxi18n)
|
|
11
|
+
|
|
12
|
+
**At start-up**
|
|
13
|
+
|
|
14
|
+
- [`TypeError: setResources(): there is no catalogue for the fallback "en"`](#typeerror-setresources-there-is-no-catalogue-for-the-fallback-en)
|
|
15
|
+
- [`TypeError: createI18n(): there is no catalogue for the fallback "de"`](#typeerror-createi18n-there-is-no-catalogue-for-the-fallback-de)
|
|
16
|
+
|
|
17
|
+
**Types**
|
|
18
|
+
|
|
19
|
+
- [`Type '"de"' is not assignable to type '"en" | "fr"'`](#type-de-is-not-assignable-to-type-en--fr-ts2769)
|
|
20
|
+
- [`Argument of type '"cart.itmes"' is not assignable to parameter of type '"cart.items"'`](#argument-of-type-cartitmes-is-not-assignable-to-parameter-of-type-cartitems)
|
|
21
|
+
- [`Property 't' does not exist on type 'Context<Empty, "/", Empty>'`](#property-t-does-not-exist-on-type-contextempty--empty)
|
|
22
|
+
- [`this looks like a factory given uncalled: call it, as use(cors()) and not use(cors)`](#this-looks-like-a-factory-given-uncalled-call-it-as-usecors-and-not-usecors)
|
|
23
|
+
- [`Object literal may only specify known properties, and 'supported' does not exist in type 'I18nOptions<…>'`](#object-literal-may-only-specify-known-properties-and-supported-does-not-exist-in-type-i18noptions)
|
|
24
|
+
- [`t()` accepts any key, typos included](#t-accepts-any-key-typos-included)
|
|
25
|
+
- [`Property 'user' does not exist on type 'BaseContext'`](#property-user-does-not-exist-on-type-basecontext)
|
|
26
|
+
- [`user` is missing from the context: add a middleware that gives it before this one](#user-is-missing-from-the-context-add-a-middleware-that-gives-it-before-this-one)
|
|
27
|
+
- [`user` is in the context with another type than this middleware reads](#user-is-in-the-context-with-another-type-than-this-middleware-reads)
|
|
28
|
+
- [`the plugin's resolve reads its context as any: annotate what it reads, or leave it unannotated`](#the-plugins-resolve-reads-its-context-as-any-annotate-what-it-reads-or-leave-it-unannotated)
|
|
29
|
+
|
|
30
|
+
**Messages**
|
|
31
|
+
|
|
32
|
+
- [`The intl string context variable "name" was not provided to the string "Hello {name}"`](#the-intl-string-context-variable-name-was-not-provided-to-the-string-hello-name)
|
|
33
|
+
- [`SyntaxError: MISSING_OTHER_CLAUSE`](#syntaxerror-missing_other_clause)
|
|
34
|
+
- [The page shows `home.title` instead of a message](#the-page-shows-hometitle-instead-of-a-message)
|
|
35
|
+
- [`fallBackTo()` changes nothing](#fallbackto-changes-nothing)
|
|
36
|
+
- [A request still speaks the old catalogues after `setResources()`](#a-request-still-speaks-the-old-catalogues-after-setresources)
|
|
37
|
+
|
|
38
|
+
**Messages from a database**
|
|
39
|
+
|
|
40
|
+
- [`translations: "a.b" is both a message and a branch`](#translations-ab-is-both-a-message-and-a-branch)
|
|
41
|
+
- [`translations: "<key>" names a prototype member (…)`](#translations-key-names-a-prototype-member-__proto__-constructor-or-prototype)
|
|
42
|
+
- [`translations: "<key>" has an empty segment (…)`](#translations-key-has-an-empty-segment-a-key-is-dotted-names-like-areaerrorsslug)
|
|
43
|
+
- [`translation "<key>" has no <lang> text`](#translation-key-has-no-lang-text)
|
|
44
|
+
- [`translation "en" is not a code: codes are dotted`](#translation-en-is-not-a-code-codes-are-dotted)
|
|
45
|
+
- [`translations: N declared code(s) have no text in "<language>": …`](#translations-n-declared-codes-have-no-text-in-language-)
|
|
46
|
+
|
|
47
|
+
**Language**
|
|
48
|
+
|
|
49
|
+
- [`i18n.t()` answers in the fallback before the language is read](#i18nt-answers-in-the-fallback-before-the-language-is-read)
|
|
50
|
+
- [`@softistx/i18n`'s `translate` answers in English on a German request](#softistxi18ns-translate-answers-in-english-on-a-german-request)
|
|
51
|
+
- [A catalogue registered with `@softistx/i18n` is not seen by `translate`](#a-catalogue-registered-with-softistxi18n-is-not-seen-by-translate)
|
|
52
|
+
- [`getLanguage()` answers `en` although the fallback is `fr`](#getlanguage-answers-en-although-the-fallback-is-fr)
|
|
53
|
+
- [A cache serves one language to every visitor](#a-cache-serves-one-language-to-every-visitor)
|
|
54
|
+
|
|
55
|
+
## Install
|
|
56
|
+
|
|
57
|
+
### `404 / no version matching, or 401 from npm.softistx.com`
|
|
58
|
+
|
|
59
|
+
**When:** `bun add @softistx/i18n` or `bun install`: against npmjs with a range older than the versions below (404 / no version matching), or through a leftover `@softistx` scope entry pointing at `npm.softistx.com` without its read token (401).
|
|
60
|
+
|
|
61
|
+
**Why:** versions before 3.2.1 were never published on npmjs, which is the only registry of `@softistx/i18n`. The 401 comes from `npm.softistx.com` itself, which apps no longer hold a token for.
|
|
62
|
+
|
|
63
|
+
**Fix:** drop the `@softistx` scope entry from `bunfig.toml` (or `.npmrc`) and bump to `@softistx/i18n` `^3.2.1` or later.
|
|
64
|
+
|
|
65
|
+
### Two copies of the i18n core after the move to `@softistx/i18n`
|
|
66
|
+
|
|
67
|
+
**When:** after upgrading to 0.5.0, `registerCatalogue()` messages or a
|
|
68
|
+
language it added are missing from `translate`, with no error.
|
|
69
|
+
|
|
70
|
+
**Why:** the peer moved from `@nxgt/i18n` to `@softistx/i18n`. A leftover
|
|
71
|
+
`@nxgt/i18n`, asked for by another package, is a second copy with its own
|
|
72
|
+
catalogue state.
|
|
73
|
+
|
|
74
|
+
**Fix:** replace `@nxgt/i18n` by `@softistx/i18n` `^3.2.1` everywhere, then check
|
|
75
|
+
that only one remains:
|
|
76
|
+
|
|
77
|
+
```sh
|
|
78
|
+
bun remove @nxgt/i18n
|
|
79
|
+
bun pm ls | grep i18n
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## At start-up
|
|
83
|
+
|
|
84
|
+
### `TypeError: createI18n(): there is no catalogue for the fallback "de"`
|
|
85
|
+
|
|
86
|
+
**When:** `createI18n()` is called — when the module that builds it is
|
|
87
|
+
imported — with a `fallback` that is not one of `resources`' keys.
|
|
88
|
+
|
|
89
|
+
**Why:** the fallback is the language of every request that names none the
|
|
90
|
+
catalogues have, so it needs a catalogue. The types refuse it when they can
|
|
91
|
+
see the keys of `resources`; they cannot when `fallback` comes from the
|
|
92
|
+
environment through a cast, or `resources` is typed `Record<string, …>`.
|
|
93
|
+
With `languages` and no `resources` (`createI18n<Key>({ languages, fallback })`),
|
|
94
|
+
the message is `@softistx/alxia-language`'s instead, `language(): the fallback "de" is not supported`:
|
|
95
|
+
add the fallback to `languages`.
|
|
96
|
+
|
|
97
|
+
**Fix:** give the fallback a catalogue, and keep `resources` a literal so
|
|
98
|
+
the compiler checks it next time:
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
import { createI18n } from '@softistx/alxia-i18n';
|
|
102
|
+
|
|
103
|
+
const en = { home: { title: 'Welcome' } };
|
|
104
|
+
const de = { home: { title: 'Willkommen' } };
|
|
105
|
+
|
|
106
|
+
createI18n({ resources: { en, de }, fallback: 'de' });
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### `TypeError: setResources(): there is no catalogue for the fallback "en"`
|
|
110
|
+
|
|
111
|
+
**When:** `i18n.setResources({ fr })` is called with catalogues that lack
|
|
112
|
+
the `fallback` language.
|
|
113
|
+
|
|
114
|
+
**Why:** the fallback is the language of every request that names none, so
|
|
115
|
+
it must stay supported. Nothing changed: the previous catalogues are still
|
|
116
|
+
in force.
|
|
117
|
+
|
|
118
|
+
**Fix:** include the fallback's catalogue, or build the instance with the
|
|
119
|
+
fallback you will keep:
|
|
120
|
+
|
|
121
|
+
```ts
|
|
122
|
+
import { createI18n } from '@softistx/alxia-i18n';
|
|
123
|
+
|
|
124
|
+
const i18n = createI18n<'home.title'>({ fallback: 'en' });
|
|
125
|
+
|
|
126
|
+
i18n.setResources({ en: { home: { title: 'Welcome' } }, fr: { home: { title: 'Bienvenue' } } });
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Types
|
|
130
|
+
|
|
131
|
+
### `Type '"de"' is not assignable to type '"en" | "fr"'` (TS2769)
|
|
132
|
+
|
|
133
|
+
```text
|
|
134
|
+
error TS2769: No overload matches this call.
|
|
135
|
+
Overload 1 of 2, '(options: I18nOptions<…>): I18nMiddleware<…>', gave the following error.
|
|
136
|
+
Type '"de"' is not assignable to type '"en" | "fr"'.
|
|
137
|
+
Overload 2 of 2, '(options: "name the keys with createI18n<Key>(…), or give resources whose fallback is one of their languages"): …', gave the following error.
|
|
138
|
+
Argument of type '{ resources: { … }; fallback: string; }' is not assignable to parameter of type '"name the keys with createI18n<Key>(…), or give resources whose fallback is one of their languages"'.
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
**When:** `createI18n({ resources: { en, fr }, fallback: 'de' })`.
|
|
142
|
+
|
|
143
|
+
**Why:** `fallback` is typed as one of `resources`' keys. This is the
|
|
144
|
+
compile-time form of [the start-up error](#typeerror-createi18n-there-is-no-catalogue-for-the-fallback-de).
|
|
145
|
+
|
|
146
|
+
**Fix:** a fallback with a catalogue:
|
|
147
|
+
|
|
148
|
+
```ts
|
|
149
|
+
createI18n({ resources: { en, fr }, fallback: 'en' });
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### `Type instantiation is excessively deep and possibly infinite`
|
|
153
|
+
|
|
154
|
+
```text
|
|
155
|
+
error TS2589: Type instantiation is excessively deep and possibly infinite.
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
**When:** a function generic over its catalogues hands them to
|
|
159
|
+
`createI18n`, then uses the plugin, with `@softistx/alxia-i18n` 0.1.1 or earlier:
|
|
160
|
+
|
|
161
|
+
```ts
|
|
162
|
+
function translatedWith<const C extends Catalogues, const Fallback extends keyof C & string>(
|
|
163
|
+
resources: C,
|
|
164
|
+
fallback: Fallback,
|
|
165
|
+
) {
|
|
166
|
+
return alxia().use(createI18n({ resources, fallback }));
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
**Why:** `KeyOf` was `@softistx/i18n`'s `Path`, which recurses without a bound.
|
|
171
|
+
With `C` a type parameter, the keys stay deferred, and some checks then
|
|
172
|
+
unfold them forever. A tsconfig whose `types` leaves out `bun` is one
|
|
173
|
+
setting that triggers them.
|
|
174
|
+
|
|
175
|
+
**Fix:** upgrade `@softistx/alxia-i18n`: `KeyOf` now reads nine levels deep at most.
|
|
176
|
+
|
|
177
|
+
### `Argument of type '"cart.itmes"' is not assignable to parameter of type '"cart.items"'`
|
|
178
|
+
|
|
179
|
+
```text
|
|
180
|
+
error TS2345: Argument of type '"cart.itmes"' is not assignable to parameter of type '"cart.items"'.
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
**When:** `t('cart.itmes')` — a typo — or `t('cart.extra')`, a key that
|
|
184
|
+
only another language's catalogue has.
|
|
185
|
+
|
|
186
|
+
**Why:** `t` takes the dotted keys of the **fallback's** catalogue, and no
|
|
187
|
+
other. A key the fallback lacks would answer itself in the fallback's
|
|
188
|
+
language.
|
|
189
|
+
|
|
190
|
+
**Fix:** correct the key, or add it to the fallback's catalogue first:
|
|
191
|
+
|
|
192
|
+
```ts
|
|
193
|
+
const en = { cart: { items: '{count, plural, one {One item} other {# items}}', extra: 'Nothing' } };
|
|
194
|
+
const fr = { cart: { items: '{count, plural, one {Un article} other {# articles}}', extra: 'Rien' } };
|
|
195
|
+
|
|
196
|
+
const i18n = createI18n({ resources: { en, fr }, fallback: 'en' });
|
|
197
|
+
i18n.t('cart.extra');
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### `Property 't' does not exist on type 'Context<Empty, "/", Empty>'`
|
|
201
|
+
|
|
202
|
+
```text
|
|
203
|
+
error TS2339: Property 't' does not exist on type 'Context<Empty, "/", Empty>'.
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
**When:** a route reads `t` — or `language` — but is declared before
|
|
207
|
+
`.use(i18n)`, or in another app or group than the one that mounts it.
|
|
208
|
+
|
|
209
|
+
**Why:** the middleware applies to what is declared after it, at runtime
|
|
210
|
+
and in the types alike.
|
|
211
|
+
|
|
212
|
+
**Fix:** `use` it first:
|
|
213
|
+
|
|
214
|
+
```ts
|
|
215
|
+
const app = alxia()
|
|
216
|
+
.use(i18n)
|
|
217
|
+
.get('/', ({ t, reply }) => reply(200, t('home.title')));
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
### `this looks like a factory given uncalled: call it, as use(cors()) and not use(cors)`
|
|
221
|
+
|
|
222
|
+
```text
|
|
223
|
+
error TS2345: Argument of type '{ <const C extends Catalogues, …>(options: I18nOptions<C, Fallback, Ctx>): I18nMiddleware<…>; <Key extends string = never, …>(options: …): I18nMiddleware<…>; }' is not assignable to parameter of type '{ … } & ((ctx: …, next: NextFunction) => … & "this looks like a factory given uncalled: call it, as use(cors()) and not use(cors)")'.
|
|
224
|
+
…
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
**When:** `app.use(createI18n)`, the factory given uncalled. The message names
|
|
228
|
+
`cors` as its example, whichever factory it is. It also throws where it is
|
|
229
|
+
declared, since alxia 0.5, rather than answering each request with a 500:
|
|
230
|
+
|
|
231
|
+
```text
|
|
232
|
+
TypeError: use(): argument 1 looks like a factory (createI18n): call it, use(createI18n())
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
**Why:** `createI18n` makes the middleware; it is not the middleware. A
|
|
236
|
+
function that returns a function is no middleware, and `createI18n` is marked
|
|
237
|
+
as a factory, so `use`, a route and `plugin` refuse it.
|
|
238
|
+
|
|
239
|
+
**Fix:** call it once, in a module of its own, and use what it returns:
|
|
240
|
+
|
|
241
|
+
```ts
|
|
242
|
+
export const i18n = createI18n({ resources: { en, fr }, fallback: 'en' });
|
|
243
|
+
|
|
244
|
+
const app = alxia().use(i18n);
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
### `Object literal may only specify known properties, and 'supported' does not exist in type 'I18nOptions<…>'`
|
|
248
|
+
|
|
249
|
+
```text
|
|
250
|
+
error TS2353: Object literal may only specify known properties, and 'supported' does not exist in type 'I18nOptions<{ readonly en: { cart: { items: string; }; }; readonly fr: { cart: { items: string; extra: string; }; }; }, "en", BaseContext>'.
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
**When:** `createI18n({ resources, fallback: 'en', supported: ['en'] })`,
|
|
254
|
+
the way `@softistx/alxia-language` is called.
|
|
255
|
+
|
|
256
|
+
**Why:** the languages supported are `resources`' keys; `createI18n()`
|
|
257
|
+
passes them to `@softistx/alxia-language` itself. Through a cast, `supported` is
|
|
258
|
+
ignored.
|
|
259
|
+
|
|
260
|
+
**Fix:** leave it out. To support fewer languages, pass fewer catalogues:
|
|
261
|
+
|
|
262
|
+
```ts
|
|
263
|
+
createI18n({ resources: { en }, fallback: 'en' });
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
### `t()` accepts any key, typos included
|
|
267
|
+
|
|
268
|
+
**When:** the catalogues are built at runtime, read with
|
|
269
|
+
`await Bun.file(…).json()`, or annotated
|
|
270
|
+
`Record<string, Record<string, string>>`.
|
|
271
|
+
|
|
272
|
+
**Why:** the keys are read from the fallback catalogue's type. A catalogue
|
|
273
|
+
typed `Record<string, …>` has none to read, so `t` takes any string — and
|
|
274
|
+
a wrong one [answers itself](#the-page-shows-hometitle-instead-of-a-message).
|
|
275
|
+
|
|
276
|
+
**Fix:** declare the catalogues as literals, or import them from JSON
|
|
277
|
+
files, which keeps their keys in the type (`resolveJsonModule` in your
|
|
278
|
+
`tsconfig.json`), or name the keys yourself with
|
|
279
|
+
[`createI18n<Key>()`](guide.md#catalogues-loaded-at-runtime):
|
|
280
|
+
|
|
281
|
+
```ts
|
|
282
|
+
import en from './locales/en.json';
|
|
283
|
+
import fr from './locales/fr.json';
|
|
284
|
+
|
|
285
|
+
export const i18n = createI18n({ resources: { en, fr }, fallback: 'en' });
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
### `Property 'user' does not exist on type 'BaseContext'`
|
|
289
|
+
|
|
290
|
+
```text
|
|
291
|
+
error TS2339: Property 'user' does not exist on type 'BaseContext'.
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
**When:** `resolve` reads something a `derive` or a middleware before the
|
|
295
|
+
i18n middleware added — a user, a session — and its parameter is not
|
|
296
|
+
annotated: `resolve: (ctx) => ctx.user.language`.
|
|
297
|
+
|
|
298
|
+
**Why:** an unannotated `resolve` is typed with the request's `BaseContext`
|
|
299
|
+
— `request`, `url`, `ip`, `pathParams`, `set` — not with what other middlewares
|
|
300
|
+
added. The middleware is built before it is used, so it cannot see the app it
|
|
301
|
+
will be used on.
|
|
302
|
+
|
|
303
|
+
**Fix:** annotate the parameter with what it reads; the middleware then
|
|
304
|
+
requires it of the app, before it:
|
|
305
|
+
|
|
306
|
+
```ts
|
|
307
|
+
const i18n = createI18n({
|
|
308
|
+
resources: { en, fr },
|
|
309
|
+
fallback: 'en',
|
|
310
|
+
resolve: ({ user }: BaseContext & { user: User | null }) => user?.language ?? undefined,
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
alxia().plugin(auth).use(i18n); // auth derives user
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
See [Reading the app's context](guide.md#reading-the-apps-context).
|
|
317
|
+
|
|
318
|
+
### `user` is missing from the context: add a middleware that gives it before this one
|
|
319
|
+
|
|
320
|
+
```text
|
|
321
|
+
error TS2345: Argument of type 'I18nMiddleware<"en" | "fr", "home.title", { user: User | null; }>' is not assignable to parameter of type 'Middleware<{ user: User | null; }, …> & { … } & ((ctx: MiddlewareContext<…>, next: NextFunction) => … & "`user` is missing from the context: add a middleware that gives it before this one")'.
|
|
322
|
+
…
|
|
323
|
+
Type 'Promise<…>' is not assignable to type '"`user` is missing from the context: add a middleware that gives it before this one"'.
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
**When:** `resolve` is annotated to read `user` —
|
|
327
|
+
`({ user }: BaseContext & { user: User | null }) => …` — and the middleware is
|
|
328
|
+
used on an app, or in a group, whose context has no `user` at that point:
|
|
329
|
+
`alxia().use(i18n)`, or `use(i18n)` before `plugin(auth)`.
|
|
330
|
+
|
|
331
|
+
**Why:** an annotated `resolve` makes the middleware require what it reads, and
|
|
332
|
+
`app.use` checks the app's context against it, so `resolve` never runs without
|
|
333
|
+
it. An app whose `user` has another type is refused too, with
|
|
334
|
+
`user` is in the context with another type than this middleware reads.
|
|
335
|
+
|
|
336
|
+
**Fix:** mount what adds `user` first, with the type `resolve`
|
|
337
|
+
reads:
|
|
338
|
+
|
|
339
|
+
```ts
|
|
340
|
+
alxia().plugin(auth).use(i18n);
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
More on this message in
|
|
344
|
+
[`@softistx/alxia-language`'s troubleshooting](https://github.com/softistx/alxia/blob/develop/packages/language/docs/troubleshooting.md#property-user-is-missing-in-type-basecontext--empty-but-required-in-type--user-user--null-).
|
|
345
|
+
|
|
346
|
+
### `user` is in the context with another type than this middleware reads
|
|
347
|
+
|
|
348
|
+
```text
|
|
349
|
+
error TS2345: Argument of type 'I18nMiddleware<"en" | "fr", "home.title", { user: User; }>' is not assignable to parameter of type 'Middleware<{ user: User; }, …> & { … } & ((ctx: MiddlewareContext<…>, next: NextFunction) => … & "`user` is in the context with another type than this middleware reads")'.
|
|
350
|
+
…
|
|
351
|
+
Type 'Promise<…>' is not assignable to type '"`user` is in the context with another type than this middleware reads"'.
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
**When:** the app gives a `user`, but of a type that does not fit the one
|
|
355
|
+
`resolve`'s parameter is annotated with: a `User | null` where `resolve`
|
|
356
|
+
reads `User`, or a user of another shape.
|
|
357
|
+
|
|
358
|
+
**Why:** `app.use` checks each key the middleware reads against the app's context;
|
|
359
|
+
a narrower type passes, a wider or different one does not.
|
|
360
|
+
|
|
361
|
+
**Fix:** annotate `resolve` with the type the app gives, and handle it
|
|
362
|
+
inside:
|
|
363
|
+
|
|
364
|
+
```ts
|
|
365
|
+
resolve: ({ user }: { user: User | null }) => user?.language ?? undefined,
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
More on this message in
|
|
369
|
+
[`@softistx/alxia-language`'s troubleshooting](https://github.com/softistx/alxia/blob/develop/packages/language/docs/troubleshooting.md#types-of-property-user-are-incompatible).
|
|
370
|
+
|
|
371
|
+
### `the plugin's resolve reads its context as any: annotate what it reads, or leave it unannotated`
|
|
372
|
+
|
|
373
|
+
```text
|
|
374
|
+
error TS2769: No overload matches this call.
|
|
375
|
+
…
|
|
376
|
+
Type 'BaseContext & Empty' is not assignable to type 'MiddlewareContext<{ readonly '~any': "the plugin's resolve reads its context as any: annotate what it reads, or leave it unannotated"; }>'.
|
|
377
|
+
Property ''~any'' is missing in type 'BaseContext & Empty' but required in type '{ readonly '~any': "the plugin's resolve reads its context as any: annotate what it reads, or leave it unannotated"; }'.
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
**When:** `resolve`'s parameter is annotated `any` —
|
|
381
|
+
`resolve: (ctx: any) => ctx.user.language` — or `Record<string, any>`, and
|
|
382
|
+
the middleware is used, on any app, whatever its context gives.
|
|
383
|
+
|
|
384
|
+
**Why:** an `any` parameter reads any key and says nothing of what it
|
|
385
|
+
reads, so the middleware would require nothing, and an app without a `user`
|
|
386
|
+
would be accepted, and throw on every request. It is refused
|
|
387
|
+
instead.
|
|
388
|
+
|
|
389
|
+
**Fix:** annotate what `resolve` reads —
|
|
390
|
+
`({ user }: BaseContext & { user: User | null }) => user?.language ?? undefined` —
|
|
391
|
+
and add the plugin or middleware that gives it first; or leave it unannotated when it
|
|
392
|
+
reads only the request.
|
|
393
|
+
|
|
394
|
+
More on this message in
|
|
395
|
+
[`@softistx/alxia`'s troubleshooting](https://github.com/softistx/alxia/blob/develop/packages/core/docs/troubleshooting.md#the-plugins--reads-its-context-as-any-annotate-what-it-reads-or-leave-it-unannotated).
|
|
396
|
+
|
|
397
|
+
## Messages
|
|
398
|
+
|
|
399
|
+
### `The intl string context variable "name" was not provided to the string "Hello {name}"`
|
|
400
|
+
|
|
401
|
+
**When:** in the log, with the response holding the message unformatted:
|
|
402
|
+
`Hello {name}`. `t('greeting')` was called without the values its message
|
|
403
|
+
names.
|
|
404
|
+
|
|
405
|
+
**Why:** the values of `t` are not typed by the message. A message that
|
|
406
|
+
cannot be formatted is logged with `console.error` — this one with
|
|
407
|
+
`code: "MISSING_VALUE"` — and answered as it is; `t` never throws.
|
|
408
|
+
|
|
409
|
+
**Fix:** pass every value the message names:
|
|
410
|
+
|
|
411
|
+
```ts
|
|
412
|
+
t('greeting', { name: 'Ada' }); // 'Hello Ada'
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
### `SyntaxError: MISSING_OTHER_CLAUSE`
|
|
416
|
+
|
|
417
|
+
**When:** in the log, with the response holding the message unformatted —
|
|
418
|
+
`{count, plural, one {One item}}` — or, for an unclosed brace,
|
|
419
|
+
`SyntaxError: EXPECT_ARGUMENT_CLOSING_BRACE` and `Hello {name`.
|
|
420
|
+
|
|
421
|
+
**Why:** the catalogue holds a message that is not valid ICU. Every
|
|
422
|
+
`plural` and `select` needs an `other` case; every `{` its `}`. It is
|
|
423
|
+
logged and answered as it is, at every call.
|
|
424
|
+
|
|
425
|
+
**Fix:** correct the catalogue:
|
|
426
|
+
|
|
427
|
+
```ts
|
|
428
|
+
const en = { cart: { items: '{count, plural, one {One item} other {# items}}' } };
|
|
429
|
+
```
|
|
430
|
+
|
|
431
|
+
### The page shows `home.title` instead of a message
|
|
432
|
+
|
|
433
|
+
**When:** a response holds a key — `home.title`, `errors.not-found` —
|
|
434
|
+
where a message should be, in some languages and not in others.
|
|
435
|
+
|
|
436
|
+
**Why:** a key the request's catalogue lacks answers **itself**, not the
|
|
437
|
+
fallback's message. Either that catalogue is partial — a language added
|
|
438
|
+
before every message was translated — or it lacks `@softistx/i18n`'s shared
|
|
439
|
+
keys, which exist only in English and French.
|
|
440
|
+
|
|
441
|
+
**Fix:** give `t()` a [chain](guide.md#the-fallback-chain) —
|
|
442
|
+
`fallbacks: () => ['en']` — so that a missing key answers the next
|
|
443
|
+
language's message; or fill a partial catalogue from the fallback's, nested
|
|
444
|
+
([Guide](guide.md#messages) shows a `fill` helper), and give a language
|
|
445
|
+
`@softistx/i18n` lacks the English shared keys:
|
|
446
|
+
|
|
447
|
+
```ts
|
|
448
|
+
import { createI18n } from '@softistx/alxia-i18n';
|
|
449
|
+
import { resources as shared } from '@softistx/i18n';
|
|
450
|
+
|
|
451
|
+
const en = { ...shared.en, home: { title: 'Welcome' } };
|
|
452
|
+
const de = { ...shared.en, home: { title: 'Willkommen' } }; // shared messages in English
|
|
453
|
+
|
|
454
|
+
export const i18n = createI18n({ resources: { en, de }, fallback: 'en' });
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
### `fallBackTo()` changes nothing
|
|
458
|
+
|
|
459
|
+
**When:** `i18n.fallBackTo('fr')` is called and `t()` still answers the key,
|
|
460
|
+
or `i18n.chain()` lacks the language.
|
|
461
|
+
|
|
462
|
+
**Why:** the step lives in the frame of the request running, opened by
|
|
463
|
+
`use(i18n)`. Called outside a request — at start-up, in a job — or in a
|
|
464
|
+
request that `use(i18n)` did not run on (a route declared before it), it
|
|
465
|
+
does nothing. It also affects only the `t()` calls after it, and only the
|
|
466
|
+
instance it is called on: two `createI18n()` have two chains.
|
|
467
|
+
|
|
468
|
+
**Fix:** call it from a middleware, a `derive` or the GraphQL `context` option declared
|
|
469
|
+
after `app.use(i18n)`, before the `t()` it should affect:
|
|
470
|
+
|
|
471
|
+
```ts
|
|
472
|
+
import { alxia } from '@softistx/alxia';
|
|
473
|
+
import { createI18n } from '@softistx/alxia-i18n';
|
|
474
|
+
|
|
475
|
+
const en = { home: { title: 'Welcome' } };
|
|
476
|
+
const fr = { home: { soon: 'Bientôt' } };
|
|
477
|
+
const i18n = createI18n({ resources: { en, fr }, fallback: 'en' });
|
|
478
|
+
|
|
479
|
+
alxia()
|
|
480
|
+
.use(i18n)
|
|
481
|
+
.use((_ctx, next) => {
|
|
482
|
+
i18n.fallBackTo('fr');
|
|
483
|
+
return next();
|
|
484
|
+
})
|
|
485
|
+
.get('/', ({ t, reply }) => reply(200, t('home.soon')));
|
|
486
|
+
```
|
|
487
|
+
|
|
488
|
+
### A request still speaks the old catalogues after `setResources()`
|
|
489
|
+
|
|
490
|
+
**When:** `i18n.setResources(next)` ran, and a response still holds the
|
|
491
|
+
previous messages.
|
|
492
|
+
|
|
493
|
+
**Why:** a request keeps the catalogues it started with; only the requests
|
|
494
|
+
that start after the call read the new ones. `supported` and
|
|
495
|
+
`i18n.t()` outside a request change at once.
|
|
496
|
+
|
|
497
|
+
**Fix:** nothing to fix for a request in flight; it ends with the messages it
|
|
498
|
+
began with. If requests that started after the call are stale, the call
|
|
499
|
+
ran on another `createI18n()` instance than the one the app uses, or
|
|
500
|
+
a cache serves the old response ([cache](#a-cache-serves-one-language-to-every-visitor)).
|
|
501
|
+
|
|
502
|
+
## Language
|
|
503
|
+
|
|
504
|
+
### `i18n.t()` answers in the fallback before the language is read
|
|
505
|
+
|
|
506
|
+
**When:** a request is in French, but `i18n.t()`, `i18n.language()` or
|
|
507
|
+
`@softistx/i18n`'s `translate` answers in the fallback's language in a
|
|
508
|
+
middleware declared before `.use(i18n)`, or in a route declared before it.
|
|
509
|
+
|
|
510
|
+
**Why:** the middleware reads the request's language when the request
|
|
511
|
+
reaches it, and holds it for what runs after it, the answer to an error
|
|
512
|
+
included. What is declared before it runs with no language to answer in.
|
|
513
|
+
|
|
514
|
+
**Fix:** translate after the middleware: declare the middlewares and the
|
|
515
|
+
routes that translate after `.use(i18n)`, and move what a middleware before
|
|
516
|
+
it renders into a middleware declared after it:
|
|
517
|
+
|
|
518
|
+
```ts
|
|
519
|
+
import { alxia, defineMiddleware } from '@softistx/alxia';
|
|
520
|
+
|
|
521
|
+
alxia()
|
|
522
|
+
.use(i18n)
|
|
523
|
+
.use(
|
|
524
|
+
defineMiddleware(({ request, reply }, next) =>
|
|
525
|
+
request.headers.has('x-busy')
|
|
526
|
+
? reply(503, { error: i18n.t('errors.service-unavailable') })
|
|
527
|
+
: next(),
|
|
528
|
+
),
|
|
529
|
+
);
|
|
530
|
+
```
|
|
531
|
+
|
|
532
|
+
### `@softistx/i18n`'s `translate` answers in English on a German request
|
|
533
|
+
|
|
534
|
+
**When:** your catalogues add a language — `de` — and `@softistx/i18n`'s
|
|
535
|
+
`getLanguage()` answers `'en'` on a request in it, and its `translate` and
|
|
536
|
+
the packages that translate through it answer in English.
|
|
537
|
+
|
|
538
|
+
**Why:** `@softistx/i18n` only speaks the languages it has a catalogue for: `en`
|
|
539
|
+
and `fr`, and what `registerCatalogue()` added. It skips a source that
|
|
540
|
+
answers another, and falls back to its own fallback language, `'en'` unless
|
|
541
|
+
`setFallbackLanguage()` changed it.
|
|
542
|
+
|
|
543
|
+
**Fix:** register the language with `@softistx/i18n` (`registerCatalogue('de',
|
|
544
|
+
catalogue)`, deep-merged over what it has), or translate what the response
|
|
545
|
+
shows with `t`, whose catalogues are yours:
|
|
546
|
+
|
|
547
|
+
```ts
|
|
548
|
+
const de = { ...shared.en, errors: { ...shared.en.errors, 'not-found': 'Nicht gefunden.' } };
|
|
549
|
+
|
|
550
|
+
const i18n = createI18n({ resources: { en: shared.en, de }, fallback: 'en' });
|
|
551
|
+
|
|
552
|
+
alxia()
|
|
553
|
+
.use(i18n)
|
|
554
|
+
.get('/', ({ t, reply }) => reply(404, { error: t('errors.not-found') })); // ?lang=de → 'Nicht gefunden.'
|
|
555
|
+
```
|
|
556
|
+
|
|
557
|
+
### A catalogue registered with `@softistx/i18n` is not seen by `translate`
|
|
558
|
+
|
|
559
|
+
**When:** after upgrading, `registerCatalogue()` messages or a language it
|
|
560
|
+
added are missing from `translate`, with no error.
|
|
561
|
+
|
|
562
|
+
**Why:** `@softistx/i18n` 3 keeps its catalogue state under its own key
|
|
563
|
+
(`@softistx/i18n/catalogues@3`); a 2.x copy installed beside it, by a package that
|
|
564
|
+
still asks for `^2`, registers into a different state.
|
|
565
|
+
|
|
566
|
+
**Fix:** one copy, on 3: `bun pm ls @softistx/i18n`, then bump every package that
|
|
567
|
+
depends on it to `^3.0.0`.
|
|
568
|
+
|
|
569
|
+
### `getLanguage()` answers `en` although the fallback is `fr`
|
|
570
|
+
|
|
571
|
+
**When:** outside a request — at start-up, in a timer, a queue consumer —
|
|
572
|
+
or in a middleware [before the language is read](#i18nt-answers-in-the-fallback-before-the-language-is-read),
|
|
573
|
+
`@softistx/i18n`'s `getLanguage()` answers `'en'` while `i18n.language()`
|
|
574
|
+
answers `'fr'`.
|
|
575
|
+
|
|
576
|
+
**Why:** with no request to read, `@softistx/i18n` falls back to its own
|
|
577
|
+
fallback, `'en'`. Your `fallback` is `createI18n()`'s, and only
|
|
578
|
+
`i18n.t()` and `i18n.language()` use it.
|
|
579
|
+
|
|
580
|
+
**Fix:** call `i18n.t()` rather than `translate` where there is no request,
|
|
581
|
+
pass `translate` the language, or set `@softistx/i18n`'s fallback to yours
|
|
582
|
+
(`setFallbackLanguage('fr')`, which needs a catalogue for `fr`):
|
|
583
|
+
|
|
584
|
+
```ts
|
|
585
|
+
import { translate } from '@softistx/i18n';
|
|
586
|
+
|
|
587
|
+
translate('errors.not-found', undefined, 'fr');
|
|
588
|
+
```
|
|
589
|
+
|
|
590
|
+
### A cache serves one language to every visitor
|
|
591
|
+
|
|
592
|
+
**When:** behind a CDN, a reverse proxy, or `@softistx/alxia-cache`, the first
|
|
593
|
+
visitor's language is served to everyone after.
|
|
594
|
+
|
|
595
|
+
**Why:** the response depends on `Accept-Language` and the language
|
|
596
|
+
cookie. The middleware says so in `Vary`; a cache keyed on the URL alone
|
|
597
|
+
ignores it.
|
|
598
|
+
|
|
599
|
+
**Fix:** give the cache the same headers:
|
|
600
|
+
|
|
601
|
+
```ts
|
|
602
|
+
alxia()
|
|
603
|
+
.use(i18n)
|
|
604
|
+
.use(cache({ ttl: 60, vary: ['accept-language', 'cookie'] }));
|
|
605
|
+
```
|
|
606
|
+
|
|
607
|
+
A middleware that sets `Vary` with `headers.set` replaces the
|
|
608
|
+
middleware's: see `@softistx/alxia-language`'s [troubleshooting](https://github.com/softistx/alxia/blob/develop/packages/language/docs/troubleshooting.md#a-cache-serves-one-language-to-every-visitor),
|
|
609
|
+
which also covers a response in the wrong language — `curl` getting the
|
|
610
|
+
fallback, a `?lang=` that does not stick, a `404` on `/fr/products`.
|
|
611
|
+
|
|
612
|
+
## Messages from a database
|
|
613
|
+
|
|
614
|
+
### `translations: "a.b" is both a message and a branch`
|
|
615
|
+
|
|
616
|
+
**When:** `catalogueOf()` or `resolveCatalogue()` gets rows where one key is a
|
|
617
|
+
message and the start of another (`a` and `a.b`).
|
|
618
|
+
|
|
619
|
+
**Why:** the tree is nested, and a node cannot be a string and an object.
|
|
620
|
+
|
|
621
|
+
**Fix:** rename one key in the collection so no key is a prefix of another.
|
|
622
|
+
|
|
623
|
+
### `translations: "<key>" names a prototype member (__proto__, constructor or prototype)`
|
|
624
|
+
|
|
625
|
+
**When:** a row's key has a segment `__proto__`, `constructor` or `prototype`.
|
|
626
|
+
|
|
627
|
+
**Fix:** rename the key in the collection; those names are refused so a row cannot reach the tree's prototype.
|
|
628
|
+
|
|
629
|
+
### `translations: "<key>" has an empty segment (a key is dotted names, like "area.errors.slug")`
|
|
630
|
+
|
|
631
|
+
**When:** a row's key is empty or has an empty segment (`a..b`, `.a`, `a.`).
|
|
632
|
+
|
|
633
|
+
**Fix:** keys are dotted names, `area.errors.slug`; correct the row.
|
|
634
|
+
|
|
635
|
+
### `translation "<key>" has no <lang> text`
|
|
636
|
+
|
|
637
|
+
**When:** `seedsOf()`, `upsertTexts()` or `restoreTexts()` gets a code that
|
|
638
|
+
lacks a language another code of the same call has.
|
|
639
|
+
|
|
640
|
+
**Fix:** give every code every language, or split the migration by language:
|
|
641
|
+
|
|
642
|
+
```ts
|
|
643
|
+
await upsertTexts(store, { 'a.errors.x': { en: 'X', fr: 'Xf' } });
|
|
644
|
+
```
|
|
645
|
+
|
|
646
|
+
### `translation "en" is not a code: codes are dotted`
|
|
647
|
+
|
|
648
|
+
**When:** the texts of a migration are language first, `{ en: { 'a.b': '…' } }`;
|
|
649
|
+
TypeScript refuses the literal too.
|
|
650
|
+
|
|
651
|
+
**Fix:** write code first, `{ 'a.b': { en: '…', fr: '…' } }`. A language that
|
|
652
|
+
contains a `.` (`looks like a code`) is the same mistake.
|
|
653
|
+
|
|
654
|
+
### `translations: N declared code(s) have no text in "<language>": …`
|
|
655
|
+
|
|
656
|
+
**When:** `assertCoverage()` finds declared codes the catalogue lacks.
|
|
657
|
+
|
|
658
|
+
**Fix:** seed the missing texts (`assertCoverage` throws by default), or pass `{ onMissing: 'report' }` to get the
|
|
659
|
+
list instead of an error:
|
|
660
|
+
|
|
661
|
+
```ts
|
|
662
|
+
const missing = assertCoverage(codes, catalogueOf(rows, 'en'), { onMissing: 'report' });
|
|
663
|
+
```
|