@verbaly/svelte 0.21.0 → 0.23.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 +11 -8
- package/dist/Trans.svelte +18 -16
- package/dist/Trans.svelte.d.ts +4 -2
- package/dist/TransNodes.svelte +2 -4
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
<img src="https://raw.githubusercontent.com/AronSoto/verbaly/develop/assets/logo.png" alt="Verbaly" width="300" />
|
|
3
3
|
</p>
|
|
4
4
|
|
|
5
|
-
<p align="center"><em>Svelte bindings for Verbaly
|
|
5
|
+
<p align="center"><em>Svelte bindings for Verbaly: stores over the reactive core.</em></p>
|
|
6
6
|
|
|
7
7
|
<p align="center">
|
|
8
8
|
<a href="https://www.npmjs.com/package/@verbaly/svelte"><img src="https://img.shields.io/npm/v/@verbaly/svelte?logo=npm&color=cb3837" alt="npm version" /></a>
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
|
|
12
12
|
---
|
|
13
13
|
|
|
14
|
-
Idiomatic Svelte stores
|
|
14
|
+
Idiomatic Svelte 5 stores over the [Verbaly](https://github.com/AronSoto/verbaly) core: the `$` auto-subscription just works, re-rendering on every locale switch.
|
|
15
15
|
|
|
16
16
|
## 🚀 Install
|
|
17
17
|
|
|
@@ -38,7 +38,7 @@ pnpm add @verbaly/svelte
|
|
|
38
38
|
import { useT, useLocale } from '@verbaly/svelte';
|
|
39
39
|
|
|
40
40
|
const t = useT();
|
|
41
|
-
const locale = useLocale(); // writable
|
|
41
|
+
const locale = useLocale(); // writable, bind it to a select
|
|
42
42
|
</script>
|
|
43
43
|
|
|
44
44
|
<p>{$t('inbox', { count: 3 })}</p>
|
|
@@ -48,7 +48,7 @@ pnpm add @verbaly/svelte
|
|
|
48
48
|
</select>
|
|
49
49
|
```
|
|
50
50
|
|
|
51
|
-
Or skip the keys entirely
|
|
51
|
+
Or skip the keys entirely: write the source text in place and the compiler extracts it, right in your `.svelte` files (script and markup):
|
|
52
52
|
|
|
53
53
|
```html
|
|
54
54
|
<h1>{$t`Hello ${name}, you have ${count} messages`}</h1>
|
|
@@ -65,9 +65,9 @@ export const t = tStore(verbaly);
|
|
|
65
65
|
export const locale = localeStore(verbaly);
|
|
66
66
|
```
|
|
67
67
|
|
|
68
|
-
## ✨ `<Trans
|
|
68
|
+
## ✨ `<Trans>`: rich text
|
|
69
69
|
|
|
70
|
-
Messages with tags (`'The <em>build</em> gate'`) render as real elements
|
|
70
|
+
Messages with tags (`'The <em>build</em> gate'`) render as real elements, same phrasing-tag whitelist as `data-verbaly-rich`, unknown tags unwrap to inert text, XSS-safe:
|
|
71
71
|
|
|
72
72
|
```html
|
|
73
73
|
<script>
|
|
@@ -79,13 +79,16 @@ Messages with tags (`'The <em>build</em> gate'`) render as real elements — sam
|
|
|
79
79
|
|
|
80
80
|
<!-- message: 'Read the <docs>guide</docs>' → a real <a>; hrefs from you, never from messages -->
|
|
81
81
|
<Trans id="cta" links={{ docs: { href: '/docs', target: '_blank', rel: 'noopener' } }} />
|
|
82
|
+
|
|
83
|
+
<!-- or map a tag to your own component; it receives the tag content as children -->
|
|
84
|
+
<Trans id="cta" components={{ docs: DocsLink }} />
|
|
82
85
|
```
|
|
83
86
|
|
|
84
|
-
Uses the instance from `provideVerbaly` (or pass `instance={verbaly}` explicitly; `richTags` overrides the whitelist, `links` maps tag names to hrefs
|
|
87
|
+
Uses the instance from `provideVerbaly` (or pass `instance={verbaly}` explicitly; `richTags` overrides the whitelist, `links` maps tag names to hrefs with `javascript:` blocked, `components` wins over both). Alternatively the core's DOM interpreter works in any Svelte app: mark elements with `data-verbaly`/`data-verbaly-rich` and call `bindDom`.
|
|
85
88
|
|
|
86
89
|
📖 Docs: **https://verbaly-web.vercel.app/docs/frameworks**
|
|
87
90
|
|
|
88
|
-
> ⚠️ Early development (`0.x`)
|
|
91
|
+
> ⚠️ Early development (`0.x`): API not stable yet.
|
|
89
92
|
|
|
90
93
|
## License
|
|
91
94
|
|
package/dist/Trans.svelte
CHANGED
|
@@ -4,26 +4,28 @@
|
|
|
4
4
|
import { useVerbaly } from '@verbaly/svelte';
|
|
5
5
|
import TransNodes from './TransNodes.svelte';
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
export let values = undefined;
|
|
9
|
-
export let instance = undefined;
|
|
10
|
-
export let richTags = undefined;
|
|
11
|
-
export let links = undefined;
|
|
7
|
+
let { id, values, instance, components, richTags, links } = $props();
|
|
12
8
|
|
|
9
|
+
// the instance is fixed at mount on purpose
|
|
10
|
+
// svelte-ignore state_referenced_locally
|
|
13
11
|
const v = instance ?? useVerbaly();
|
|
14
|
-
let version = v.version;
|
|
15
|
-
|
|
16
|
-
onDestroy(unsubscribe);
|
|
12
|
+
let version = $state(v.version);
|
|
13
|
+
onDestroy(v.subscribe(() => (version = v.version)));
|
|
17
14
|
|
|
18
|
-
|
|
15
|
+
const allowed = $derived(new Set(richTags ?? RICH_TAGS));
|
|
19
16
|
// normalize + sanitize hrefs once (never from messages)
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
17
|
+
const linkDefs = $derived(
|
|
18
|
+
links
|
|
19
|
+
? Object.fromEntries(
|
|
20
|
+
Object.entries(links).map(([name, link]) => [name, normalizeLink(link)]),
|
|
21
|
+
)
|
|
22
|
+
: undefined,
|
|
23
|
+
);
|
|
25
24
|
// version keeps this reactive to locale/catalog changes
|
|
26
|
-
|
|
25
|
+
const nodes = $derived.by(() => {
|
|
26
|
+
void version;
|
|
27
|
+
return parseTags(v.t(id, values));
|
|
28
|
+
});
|
|
27
29
|
</script>
|
|
28
30
|
|
|
29
|
-
<TransNodes {nodes} {allowed} links={linkDefs} />
|
|
31
|
+
<TransNodes {nodes} {allowed} {components} links={linkDefs} />
|
package/dist/Trans.svelte.d.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { Component, Snippet } from 'svelte';
|
|
2
2
|
import type { Params, RichLink, Verbaly } from 'verbaly';
|
|
3
3
|
|
|
4
4
|
export interface TransProps {
|
|
5
5
|
id: string;
|
|
6
6
|
values?: Params;
|
|
7
7
|
instance?: Verbaly;
|
|
8
|
+
components?: Record<string, Component<{ children?: Snippet }>>;
|
|
8
9
|
richTags?: string[];
|
|
9
10
|
links?: Record<string, RichLink>;
|
|
10
11
|
}
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
declare const Trans: Component<TransProps>;
|
|
14
|
+
export default Trans;
|
package/dist/TransNodes.svelte
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
<script>
|
|
2
2
|
import TransNodes from './TransNodes.svelte';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
export let allowed;
|
|
6
|
-
export let links = undefined;
|
|
4
|
+
let { nodes = [], allowed, components, links } = $props();
|
|
7
5
|
</script>
|
|
8
6
|
|
|
9
|
-
{#each nodes as node}{#if typeof node === 'string'}{node}{:else if links && links[node.name]}<a href={links[node.name].href} target={links[node.name].target} rel={links[node.name].rel}><TransNodes nodes={node.children} {allowed} {links} /></a>{:else if allowed.has(node.name)}<svelte:element this={node.name}><TransNodes nodes={node.children} {allowed} {links} /></svelte:element>{:else}<TransNodes nodes={node.children} {allowed} {links} />{/if}{/each}
|
|
7
|
+
{#each nodes as node}{#if typeof node === 'string'}{node}{:else if components && components[node.name]}{@const Component = components[node.name]}<Component><TransNodes nodes={node.children} {allowed} {components} {links} /></Component>{:else if links && links[node.name]}<a href={links[node.name].href} target={links[node.name].target} rel={links[node.name].rel}><TransNodes nodes={node.children} {allowed} {components} {links} /></a>{:else if allowed.has(node.name)}<svelte:element this={node.name}><TransNodes nodes={node.children} {allowed} {components} {links} /></svelte:element>{:else}<TransNodes nodes={node.children} {allowed} {components} {links} />{/if}{/each}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@verbaly/svelte",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Svelte bindings for Verbaly
|
|
3
|
+
"version": "0.23.0",
|
|
4
|
+
"description": "Svelte bindings for Verbaly: stores over the reactive core.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"i18n",
|
|
7
7
|
"svelte",
|
|
@@ -44,14 +44,14 @@
|
|
|
44
44
|
"access": "public"
|
|
45
45
|
},
|
|
46
46
|
"peerDependencies": {
|
|
47
|
-
"svelte": "^
|
|
48
|
-
"verbaly": "^0.
|
|
47
|
+
"svelte": "^5.0.0",
|
|
48
|
+
"verbaly": "^0.23.0"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"@sveltejs/vite-plugin-svelte": "^7.2.0",
|
|
52
52
|
"happy-dom": "^20.10.6",
|
|
53
53
|
"svelte": "^5.56.4",
|
|
54
|
-
"verbaly": "0.
|
|
54
|
+
"verbaly": "0.23.0"
|
|
55
55
|
},
|
|
56
56
|
"scripts": {
|
|
57
57
|
"build": "tsdown && node scripts/copy-svelte.mjs",
|