@verbaly/svelte 0.79.0 → 0.81.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -3
- package/dist/index.d.ts +1 -1
- package/dist/index.js +7 -2
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -16,13 +16,17 @@ Idiomatic Svelte 5 stores over the [Verbaly](https://github.com/AronSoto/verbaly
|
|
|
16
16
|
## 🚀 Install
|
|
17
17
|
|
|
18
18
|
```bash
|
|
19
|
-
pnpm add @verbaly/svelte
|
|
19
|
+
pnpm add verbaly @verbaly/svelte @verbaly/vite
|
|
20
|
+
pnpm add -D @verbaly/compiler # the verbaly command: init, extract, check
|
|
21
|
+
npx verbaly init
|
|
20
22
|
```
|
|
21
23
|
|
|
24
|
+
Add `verbaly()` to the plugins in `vite.config` (the [`@verbaly/vite`](https://www.npmjs.com/package/@verbaly/vite) README shows it). It writes `verbaly/app`, the module these stores read. In SvelteKit, follow [`@verbaly/sveltekit`](https://www.npmjs.com/package/@verbaly/sveltekit), which renders each request in its own language.
|
|
25
|
+
|
|
22
26
|
## ⚡ Usage
|
|
23
27
|
|
|
24
28
|
```html
|
|
25
|
-
<!-- +layout.svelte -->
|
|
29
|
+
<!-- App.svelte, or +layout.svelte in SvelteKit -->
|
|
26
30
|
<script>
|
|
27
31
|
import { provideVerbaly } from '@verbaly/svelte';
|
|
28
32
|
import { verbaly } from 'verbaly/app';
|
|
@@ -48,7 +52,21 @@ pnpm add @verbaly/svelte
|
|
|
48
52
|
</select>
|
|
49
53
|
```
|
|
50
54
|
|
|
51
|
-
**`provideVerbaly` is optional in the browser.** With none above it, a hook reads the instance `verbaly/app` creates, so a single-page app can skip it and a component test renders with no wrapper. On a server it is required, since every request has its own instance.
|
|
55
|
+
**`provideVerbaly` is optional in the browser.** With none above it, a hook reads the instance `verbaly/app` creates, so a single-page app can skip it and a component test renders with no wrapper. The Vite plugin and unplugin load `verbaly/app` in every component that imports `@verbaly/svelte`, so that instance is there before the first render. On a server it is required, since every request has its own instance.
|
|
56
|
+
|
|
57
|
+
**Switching languages.** Setting `$locale` switches every component, and the page follows: `<html lang>`, `<html dir>` and the choice, saved in the browser. Where a server renders the page, as in SvelteKit, switch with `switchLocale` from `verbaly/app` instead: it also sets the cookie the server reads. A single-page app starts in its source language, so read that choice back before mounting:
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
// main.ts: the saved choice, else the browser's, loaded before anything renders
|
|
61
|
+
import { mount } from 'svelte';
|
|
62
|
+
import { resolveLocale } from 'verbaly';
|
|
63
|
+
import { locales, setLocale } from 'verbaly/app';
|
|
64
|
+
import App from './App.svelte';
|
|
65
|
+
|
|
66
|
+
setLocale(resolveLocale({ supported: locales, path: false })).then(() =>
|
|
67
|
+
mount(App, { target: document.getElementById('app')! }),
|
|
68
|
+
);
|
|
69
|
+
```
|
|
52
70
|
|
|
53
71
|
**Keys are typed.** `$t` from `useT()` checks every key and its params against your source catalog, through the types Verbaly generates (`src/verbaly.d.ts`): a key no catalog has does not compile. A key that comes from data, like `$t(item.key)` or a variable typed as a union of keys, passes as it is, and its params are left open; declare it with `defineKeys` from `verbaly/app` when you want `check` to verify it exists.
|
|
54
72
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Readable, Writable } from "svelte/store";
|
|
2
1
|
import { DictionaryInput, Params, TFunction, Verbaly } from "verbaly";
|
|
2
|
+
import { Readable, Writable } from "svelte/store";
|
|
3
3
|
export declare function provideVerbaly<D extends DictionaryInput>(instance: Verbaly<D>): Verbaly<D>;
|
|
4
4
|
export declare function useVerbaly<D extends DictionaryInput = DictionaryInput>(): Verbaly<D>;
|
|
5
5
|
export declare function tStore<D extends DictionaryInput>(instance: Verbaly<D>): Readable<Verbaly<D>['t']>;
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { getContext, setContext } from "svelte";
|
|
2
|
+
import { persistLocale } from "verbaly";
|
|
2
3
|
// src/index.ts
|
|
3
4
|
const KEY = {};
|
|
4
5
|
function provideVerbaly(instance) {
|
|
@@ -22,13 +23,17 @@ function tStore(instance) {
|
|
|
22
23
|
} };
|
|
23
24
|
}
|
|
24
25
|
function localeStore(instance) {
|
|
26
|
+
const set = (locale) => {
|
|
27
|
+
instance.setLocale(locale);
|
|
28
|
+
persistLocale(locale);
|
|
29
|
+
};
|
|
25
30
|
return {
|
|
26
31
|
subscribe(run) {
|
|
27
32
|
run(instance.locale);
|
|
28
33
|
return instance.subscribe(() => run(instance.locale));
|
|
29
34
|
},
|
|
30
|
-
set
|
|
31
|
-
update: (fn) =>
|
|
35
|
+
set,
|
|
36
|
+
update: (fn) => set(fn(instance.locale))
|
|
32
37
|
};
|
|
33
38
|
}
|
|
34
39
|
function useT() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@verbaly/svelte",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.81.0",
|
|
4
4
|
"description": "Svelte bindings for Verbaly: stores over the reactive core.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"i18n",
|
|
@@ -49,13 +49,13 @@
|
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
51
|
"svelte": "^5.0.0",
|
|
52
|
-
"verbaly": "^0.
|
|
52
|
+
"verbaly": "^0.81.0"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@sveltejs/vite-plugin-svelte": "^7.3.1",
|
|
56
56
|
"happy-dom": "^21.0.4",
|
|
57
57
|
"svelte": "^5.57.3",
|
|
58
|
-
"verbaly": "^0.
|
|
58
|
+
"verbaly": "^0.81.0"
|
|
59
59
|
},
|
|
60
60
|
"scripts": {
|
|
61
61
|
"build": "tsdown && node scripts/copy-svelte.mjs",
|