@verbaly/sveltekit 0.16.0 → 0.17.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 +9 -9
- package/dist/index.d.ts +1 -1
- package/dist/index.js +8 -8
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -63,20 +63,20 @@ declare namespace App {
|
|
|
63
63
|
export const load = ({ locals }) => ({ locale: locals.verbalyLocale });
|
|
64
64
|
```
|
|
65
65
|
|
|
66
|
-
**5. One instance per render, catalog awaited** — this is the no-FOUC guarantee:
|
|
66
|
+
**5. One instance per render, catalog awaited** — this is the no-FOUC guarantee, in one call:
|
|
67
67
|
|
|
68
68
|
```ts
|
|
69
69
|
// src/routes/+layout.ts
|
|
70
|
-
import {
|
|
71
|
-
|
|
72
|
-
export const load = async ({ data }) => {
|
|
73
|
-
|
|
74
|
-
await
|
|
75
|
-
|
|
76
|
-
return { ...data, verbaly };
|
|
77
|
-
};
|
|
70
|
+
import { createRequestInstance } from 'virtual:verbaly';
|
|
71
|
+
|
|
72
|
+
export const load = async ({ data }) => ({
|
|
73
|
+
...data,
|
|
74
|
+
verbaly: await createRequestInstance(data.locale), // catalog ready BEFORE render
|
|
75
|
+
});
|
|
78
76
|
```
|
|
79
77
|
|
|
78
|
+
(`createInstance(options)` remains available when you need custom options.)
|
|
79
|
+
|
|
80
80
|
```html
|
|
81
81
|
<!-- src/routes/+layout.svelte -->
|
|
82
82
|
<script>
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LOCALE_STORAGE_KEY, resolveRequestLocale } from "verbaly";
|
|
2
2
|
//#region src/index.ts
|
|
3
|
-
const LOCALE_COOKIE =
|
|
3
|
+
const LOCALE_COOKIE = LOCALE_STORAGE_KEY;
|
|
4
4
|
const LANG_PLACEHOLDER = "%verbaly.lang%";
|
|
5
5
|
const YEAR = 31536e3;
|
|
6
6
|
function verbalyHandle(options) {
|
|
@@ -8,12 +8,12 @@ function verbalyHandle(options) {
|
|
|
8
8
|
if (!Array.isArray(locales) || locales.length === 0) throw new Error("[verbaly] verbalyHandle needs `locales` — pass the `locales` export from 'virtual:verbaly' (requires @verbaly/vite ≥0.16) or list them yourself: verbalyHandle({ locales: [...] })");
|
|
9
9
|
const fallback = options.fallback ?? locales[0];
|
|
10
10
|
return ({ event, resolve }) => {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
const resolved = resolveRequestLocale({
|
|
12
|
+
supported: locales,
|
|
13
|
+
cookie: cookie ? event.cookies.get(cookie) : void 0,
|
|
14
|
+
header: event.request.headers.get("accept-language"),
|
|
15
|
+
fallback
|
|
16
|
+
});
|
|
17
17
|
event.locals.verbalyLocale = resolved;
|
|
18
18
|
return resolve(event, { transformPageChunk: ({ html }) => html.replaceAll(LANG_PLACEHOLDER, resolved) });
|
|
19
19
|
};
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import { LOCALE_STORAGE_KEY, resolveRequestLocale } from 'verbaly';\nimport type { Verbaly } from 'verbaly';\n\n// derived from core's localStorage key — one identity per user across channels\nexport const LOCALE_COOKIE: string = LOCALE_STORAGE_KEY;\nconst LANG_PLACEHOLDER = '%verbaly.lang%';\nconst YEAR = 31536000;\n\n// structural subset of @sveltejs/kit — no runtime or type dependency on kit\ninterface HandleEvent {\n request: Request;\n cookies: { get(name: string): string | undefined };\n locals: { verbalyLocale?: string };\n}\ninterface ResolveOptions {\n transformPageChunk?(input: { html: string; done: boolean }): string | undefined;\n}\ninterface HandleInput {\n event: HandleEvent;\n resolve(event: HandleEvent, opts?: ResolveOptions): Response | Promise<Response>;\n}\n\nexport interface VerbalyHandleOptions {\n /** supported locales — pass `locales` from 'virtual:verbaly' */\n locales: string[];\n /** default when nothing matches (defaults to the first locale) */\n fallback?: string;\n /** cookie read for the user's choice; `false` = Accept-Language only */\n cookie?: string | false;\n}\n\n// server hook: cookie → Accept-Language → fallback, per request.\n// Sets event.locals.verbalyLocale and fills %verbaly.lang% in app.html.\nexport function verbalyHandle(options: VerbalyHandleOptions) {\n const { locales, cookie = LOCALE_COOKIE } = options;\n if (!Array.isArray(locales) || locales.length === 0) {\n throw new Error(\n \"[verbaly] verbalyHandle needs `locales` — pass the `locales` export from 'virtual:verbaly' \" +\n '(requires @verbaly/vite ≥0.16) or list them yourself: verbalyHandle({ locales: [...] })',\n );\n }\n const fallback = options.fallback ?? locales[0]!;\n\n return ({ event, resolve }: HandleInput): Response | Promise<Response> => {\n const resolved = resolveRequestLocale({\n supported: locales,\n cookie: cookie ? event.cookies.get(cookie) : undefined,\n header: event.request.headers.get('accept-language'),\n fallback,\n });\n\n event.locals.verbalyLocale = resolved;\n return resolve(event, {\n transformPageChunk: ({ html }) => html.replaceAll(LANG_PLACEHOLDER, resolved),\n });\n };\n}\n\nexport interface SwitchLocaleOptions {\n /** cookie written so the next SSR request matches; `false` = don't persist */\n cookie?: string | false;\n /** cookie lifetime in seconds (default 1 year) */\n maxAge?: number;\n}\n\n// client-side switch: catalog first (no flash), then locale, then persistence\nexport async function switchLocale(\n instance: Pick<Verbaly, 'loadLocale' | 'setLocale'>,\n locale: string,\n options: SwitchLocaleOptions = {},\n): Promise<void> {\n const { cookie = LOCALE_COOKIE, maxAge = YEAR } = options;\n await instance.loadLocale(locale);\n instance.setLocale(locale);\n if (typeof document === 'undefined') return; // SSR-safe no-op\n if (cookie) {\n document.cookie = `${cookie}=${encodeURIComponent(locale)}; path=/; max-age=${maxAge}; samesite=lax`;\n }\n document.documentElement.lang = locale;\n}\n"],"mappings":";;AAIA,MAAa,gBAAwB;AACrC,MAAM,mBAAmB;AACzB,MAAM,OAAO;AA2Bb,SAAgB,cAAc,SAA+B;CAC3D,MAAM,EAAE,SAAS,SAAS,kBAAkB;CAC5C,IAAI,CAAC,MAAM,QAAQ,OAAO,KAAK,QAAQ,WAAW,GAChD,MAAM,IAAI,MACR,oLAEF;CAEF,MAAM,WAAW,QAAQ,YAAY,QAAQ;CAE7C,QAAQ,EAAE,OAAO,cAAyD;EACxE,MAAM,WAAW,qBAAqB;GACpC,WAAW;GACX,QAAQ,SAAS,MAAM,QAAQ,IAAI,MAAM,IAAI,KAAA;GAC7C,QAAQ,MAAM,QAAQ,QAAQ,IAAI,iBAAiB;GACnD;EACF,CAAC;EAED,MAAM,OAAO,gBAAgB;EAC7B,OAAO,QAAQ,OAAO,EACpB,qBAAqB,EAAE,WAAW,KAAK,WAAW,kBAAkB,QAAQ,EAC9E,CAAC;CACH;AACF;AAUA,eAAsB,aACpB,UACA,QACA,UAA+B,CAAC,GACjB;CACf,MAAM,EAAE,SAAS,eAAe,SAAS,SAAS;CAClD,MAAM,SAAS,WAAW,MAAM;CAChC,SAAS,UAAU,MAAM;CACzB,IAAI,OAAO,aAAa,aAAa;CACrC,IAAI,QACF,SAAS,SAAS,GAAG,OAAO,GAAG,mBAAmB,MAAM,EAAE,oBAAoB,OAAO;CAEvF,SAAS,gBAAgB,OAAO;AAClC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@verbaly/sveltekit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.17.0",
|
|
4
4
|
"description": "SvelteKit SSR integration for Verbaly — per-request locale negotiation and flash-free hydration.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"i18n",
|
|
@@ -40,13 +40,13 @@
|
|
|
40
40
|
"access": "public"
|
|
41
41
|
},
|
|
42
42
|
"peerDependencies": {
|
|
43
|
-
"verbaly": "^0.
|
|
43
|
+
"verbaly": "^0.17.0"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"@sveltejs/kit": "^2.69.2",
|
|
47
47
|
"happy-dom": "^20.10.6",
|
|
48
48
|
"svelte": "^5.56.4",
|
|
49
|
-
"verbaly": "0.
|
|
49
|
+
"verbaly": "0.17.0"
|
|
50
50
|
},
|
|
51
51
|
"scripts": {
|
|
52
52
|
"build": "tsdown",
|