@sveltekit-i18n/parser-curly 3.1.0 → 3.1.2
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 +26 -8
- package/dist/index.d.ts +5 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@ npm install @sveltekit-i18n/parser-curly
|
|
|
14
14
|
|
|
15
15
|
This parser is included by default in [sveltekit-i18n](https://github.com/sveltekit-i18n/lib).
|
|
16
16
|
|
|
17
|
-
**Requirements:** Node.js 22, Bun 1.2 or Deno 2, or newer. Version 3 is ESM-only and expects [`@sveltekit-i18n/base`](https://github.com/sveltekit-i18n/base) v3 as a peer dependency.
|
|
17
|
+
**Requirements:** Node.js 22, Bun 1.2 or Deno 2, or newer. Version 3 is ESM-only and expects [`@sveltekit-i18n/base`](https://github.com/sveltekit-i18n/base) v3 as a peer dependency. The examples use base 3.1's loader spelling — `namespace`, and one descriptor listing several locales; on base 3.0 write `key` and one descriptor per locale, whose loader names its own file, since 3.0 passes it no `namespace`: `{ locale: 'en', key: 'common', loader: async () => (await import('./en/common.json')).default }`.
|
|
18
18
|
|
|
19
19
|
## Usage
|
|
20
20
|
|
|
@@ -24,16 +24,16 @@ This parser is included by default in [sveltekit-i18n](https://github.com/svelte
|
|
|
24
24
|
import { I18n } from '@sveltekit-i18n/base';
|
|
25
25
|
import parser from '@sveltekit-i18n/parser-curly';
|
|
26
26
|
|
|
27
|
-
const config = {
|
|
27
|
+
export const config = {
|
|
28
28
|
parser: parser({
|
|
29
29
|
// Where diagnostics go; `null` states that they go nowhere.
|
|
30
30
|
onReport: null,
|
|
31
31
|
}),
|
|
32
32
|
loaders: [
|
|
33
33
|
{
|
|
34
|
-
locale: 'en',
|
|
35
|
-
|
|
36
|
-
loader: async () => (await import(
|
|
34
|
+
locale: ['en', 'cs'],
|
|
35
|
+
namespace: 'common',
|
|
36
|
+
loader: async ({ locale, namespace }) => (await import(`./${locale}/${namespace}.json`)).default,
|
|
37
37
|
},
|
|
38
38
|
],
|
|
39
39
|
};
|
|
@@ -46,7 +46,7 @@ export const i18n = new I18n(config);
|
|
|
46
46
|
```javascript
|
|
47
47
|
import { I18n } from 'sveltekit-i18n';
|
|
48
48
|
|
|
49
|
-
const config = {
|
|
49
|
+
export const config = {
|
|
50
50
|
// parser-curly is already included
|
|
51
51
|
loaders: [/* ... */],
|
|
52
52
|
};
|
|
@@ -54,7 +54,11 @@ const config = {
|
|
|
54
54
|
export const i18n = new I18n(config);
|
|
55
55
|
```
|
|
56
56
|
|
|
57
|
-
|
|
57
|
+
### In a SvelteKit app
|
|
58
|
+
|
|
59
|
+
A SvelteKit app wires its config through the `/kit` subpath, new in 3.1, rather than through an instance of its own: `defineI18n(config, { preferredLocale })` returns `handle` for `hooks.server.js`, one `load` for both root layout files, `use()` for the root layout and `get()` for every component below it. The server builds an instance per request, so no visitor sees another visitor's locale. `sveltekit-i18n` users import it from `sveltekit-i18n/kit`, which fills the parser in; a base app imports it from `@sveltekit-i18n/base/kit` and hands it the config above. The core's [SvelteKit guide](https://github.com/sveltekit-i18n/base/blob/master/docs/README.md#sveltekit) walks through the setup.
|
|
60
|
+
|
|
61
|
+
However the instance is built, `i18n.t(key, payload?, props?)` takes the values the placeholders name and the per-call formatting options; the examples below use it.
|
|
58
62
|
|
|
59
63
|
## Syntax
|
|
60
64
|
|
|
@@ -385,7 +389,9 @@ i18n.t('common.welcome', { aplicationName: 'My app' })
|
|
|
385
389
|
// → type error: typo caught
|
|
386
390
|
```
|
|
387
391
|
|
|
388
|
-
`Config<Payload, Props>` types the payload and the props `i18n.t` accepts; left bare, `Config` accepts any payload key and the built-in modifiers' props. A custom modifier types its own props through `Modifier.T<OwnProps>`. The factory's type arguments check the parser options the same way: with `Props` spelled as the second, `parser<Payload, Props>({ ... })`, a `modifierDefaults` entry for a custom modifier is checked; a modifier written inline reads typed `props` once the modifier names are spelled as the third argument too, `parser<Payload, Props, 'truncate'>({ ... })`. `Parser` holds the option and parameter types (`Parser.Options`, `Parser.OnReport`, `Parser.Params`, `Parser.Payload`), `Modifier` the modifier and wrapper types (`Modifier.T`, `Modifier.Wrapper`, `Modifier.Props`), `Cst` the [tree](#describing-a-message) node types, and `Report` is the report.
|
|
392
|
+
`Config<Payload, Props>` types the payload and the props `i18n.t` accepts; left bare, `Config` accepts any payload key and the built-in modifiers' props. A custom modifier types its own props through `Modifier.T<OwnProps>`. The factory's type arguments check the parser options the same way: with `Props` spelled as the second, `parser<Payload, Props>({ ... })`, a `modifierDefaults` entry for a custom modifier is checked; a modifier written inline reads typed `props` once the modifier names are spelled as the third argument too, `parser<Payload, Props, 'truncate'>({ ... })`. `Parser` holds the option and parameter types (`Parser.Options`, `Parser.OnReport`, `Parser.Params`, `Parser.Payload`) and the migration aid's (`Parser.OnSuspectValue`, `Parser.Suspect`, `Parser.SuspectKind`), `Modifier` the modifier and wrapper types (`Modifier.T`, `Modifier.Wrapper`, `Modifier.Props`), `Cst` the [tree](#describing-a-message) node types, and `Report` is the report.
|
|
393
|
+
|
|
394
|
+
`onSuspectValue` receives a `Parser.Suspect` for every value a placeholder read that version 1 of the format would have read as syntax: `found`, the `Parser.SuspectKind`s the value holds (`placeholder` where it holds `{{`, `escape` where it holds a backslash); `placeholder`, the placeholder that read it, as the message spells it; `id`, the message's id, where the call passed one; and `text`, the value truncated with its line terminators escaped. Unlike a report's `text`, that is payload text, so a host writing it somewhere writes what its payload holds.
|
|
389
395
|
|
|
390
396
|
## Extracting Parameters
|
|
391
397
|
|
|
@@ -421,6 +427,18 @@ Build the extractor from the same options `parser()` is built from: a custom mod
|
|
|
421
427
|
|
|
422
428
|
Only the text of a message is scanned. A translation leaf that is not text names no parameters rather than throwing, and a placeholder a payload value carries is not one the message itself names — a value is data, so nothing reads it as source. A placeholder the message writes inside another is named beside the one holding it, in the order the message writes them.
|
|
423
429
|
|
|
430
|
+
This is what types `t` and `l` by key and payload: [`@sveltekit-i18n/typegen`](https://github.com/sveltekit-i18n/typegen), a Vite plugin, runs the config the module it is pointed at exports as `config`, reads every message through the extractor and writes the schema, which it registers for every config that states no [`schema`](https://github.com/sveltekit-i18n/base/blob/master/docs/README.md#schema) of its own, so on base 3.1 there is nothing to wire ([an older core](https://github.com/sveltekit-i18n/typegen#3-nothing-to-wire-from-310-next2-on) takes a cast). `sveltekit-i18n` re-exports this extractor, so its users name that package; a base app names this one:
|
|
431
|
+
|
|
432
|
+
```javascript
|
|
433
|
+
// vite.config.js, with sveltekit-i18n
|
|
434
|
+
typegen({ config: 'src/lib/i18n.js', extractParams: { from: 'sveltekit-i18n' } })
|
|
435
|
+
|
|
436
|
+
// vite.config.js, with @sveltekit-i18n/base
|
|
437
|
+
typegen({ config: 'src/lib/i18n.js', extractParams: { from: '@sveltekit-i18n/parser-curly' } })
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+
The plugin hands `extractParams.options` to the factory as JSON, so no function reaches the extractor. `customModifiers` — the one option extraction reads — cannot be passed that way, and a custom modifier registered under a built-in name that changes the `kind` a parameter is reported with is not reflected: the schema types the parameter by the built-in modifier.
|
|
441
|
+
|
|
424
442
|
## Describing a Message
|
|
425
443
|
|
|
426
444
|
`cst` describes a message as the parts it is written from: where its placeholders are, what each is made of, and where every escape sequence falls. It is the format's own describer, a named export for the same reason `extractParamsFactory` is one — resolution never calls it, so a bundle that never reaches it drops it.
|
package/dist/index.d.ts
CHANGED
|
@@ -14,6 +14,11 @@ declare namespace Parser {
|
|
|
14
14
|
onReport: OnReport | null | undefined;
|
|
15
15
|
};
|
|
16
16
|
type OnReport = Parser$1.OnReport;
|
|
17
|
+
/**
|
|
18
|
+
* What a value holds that version 1 of the format would have read as syntax:
|
|
19
|
+
* `placeholder` where it holds `{{`, `escape` where it holds a backslash.
|
|
20
|
+
*/
|
|
21
|
+
type SuspectKind = Parser$1.SuspectKind;
|
|
17
22
|
/**
|
|
18
23
|
* A value a placeholder read that version 1 of the format would have read as
|
|
19
24
|
* syntax. It is not a report — the placeholder resolved to exactly the text
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sveltekit-i18n/parser-curly",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.2",
|
|
4
4
|
"description": "Curly Message Format parser for the sveltekit-i18n library.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"homepage": "https://sveltekit-i18n.github.io",
|
|
51
51
|
"funding": "https://github.com/sponsors/sveltekit-i18n",
|
|
52
52
|
"peerDependencies": {
|
|
53
|
-
"@sveltekit-i18n/base": "^3.0.0"
|
|
53
|
+
"@sveltekit-i18n/base": "^3.0.0 || ^3.1.0-next.0"
|
|
54
54
|
},
|
|
55
55
|
"dependencies": {
|
|
56
56
|
"@curly-message/parser": "^3.0.0"
|