@sveltebase/i18n 0.3.2 → 0.3.4

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 CHANGED
@@ -64,12 +64,28 @@ export const i18n = createI18n(languages, "locale");
64
64
 
65
65
  Then initialize it before using translations or formatters.
66
66
 
67
- If you already have the request cookies available during the initial render, pass them to `init(cookies)` immediately. That lets the package restore the saved locale before your UI reads translations, so the correct language loads right away instead of first rendering with the fallback locale and switching later.
67
+ In SvelteKit, the recommended quick start is to load all cookies in `+layout.server.ts`, pass them to `+layout.svelte`, and initialize i18n there. That lets the package restore the saved locale before your UI reads translations, so the correct language loads right away instead of first rendering with the fallback locale and switching later.
68
+
69
+ In `+layout.server.ts`:
68
70
 
69
71
  ```ts
70
- import { i18n } from "./i18n";
72
+ export function load({ cookies }) {
73
+ return {
74
+ cookies: cookies.getAll()
75
+ };
76
+ }
77
+ ```
71
78
 
72
- i18n.init(cookies);
79
+ Then in `+layout.svelte`:
80
+
81
+ ```svelte
82
+ <script lang="ts">
83
+ import { i18n } from "./i18n";
84
+
85
+ let { data } = $props();
86
+
87
+ i18n.init(() => data.cookies);
88
+ </script>
73
89
  ```
74
90
 
75
91
  ## Basic usage in a Svelte component
@@ -146,7 +162,7 @@ i18n.init();
146
162
 
147
163
  ### With cookies in SvelteKit for faster first render
148
164
 
149
- In SvelteKit, the recommended approach is to load all cookies in `+layout.server.ts`, return them from `load`, and then pass that data to `i18n.init(...)` in `+layout.svelte`.
165
+ In SvelteKit, the recommended approach is to load all cookies in `+layout.server.ts`, return them from `load`, and then pass that data to `i18n.init(() => data.cookies)` in `+layout.svelte`.
150
166
 
151
167
  This lets `@sveltebase/i18n` restore the saved locale before your UI reads translations, so the correct language is available immediately and you avoid rendering the fallback language first.
152
168
 
@@ -168,13 +184,13 @@ Then in `+layout.svelte`:
168
184
 
169
185
  let { data } = $props();
170
186
 
171
- i18n.init(data.cookies);
187
+ i18n.init(() => data.cookies);
172
188
  </script>
173
189
 
174
190
  <slot />
175
191
  ```
176
192
 
177
- The value passed to `i18n.init(...)` should be an array of cookie objects in this shape:
193
+ The function passed to `i18n.init(...)` should return an array of cookie objects in this shape:
178
194
 
179
195
  ```ts
180
196
  type Cookie = {
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { AppConfig } from 'use-intl/core';
2
+ export { MaybeGetter } from '@sveltebase/state';
2
3
 
3
4
  type MessageValue = string | {
4
5
  [key: string]: MessageValue;
@@ -20,6 +21,7 @@ type Cookie = {
20
21
  name: string;
21
22
  value: string;
22
23
  };
24
+ type MaybeGetter<T> = T | (() => T);
23
25
  type LocaleCode<TLanguages extends readonly LanguageDefinition[]> = TLanguages[number]["code"];
24
26
  type CurrentLanguage<TLanguages extends readonly LanguageDefinition[]> = TLanguages[number];
25
27
  type TranslationValues = Record<string, string | number | Date>;
@@ -38,7 +40,7 @@ interface I18nInstance<TLanguages extends readonly LanguageDefinition[]> {
38
40
  readonly languages: TLanguages;
39
41
  locale: LocaleCode<TLanguages>;
40
42
  readonly currentLanguage: CurrentLanguage<TLanguages>;
41
- init(cookies?: Cookie[]): void;
43
+ init(cookies?: MaybeGetter<Cookie[] | undefined>): void;
42
44
  }
43
45
  declare function getTranslations(): Translate;
44
46
  declare function getFormat(): Format;
package/dist/index.js CHANGED
@@ -236,6 +236,7 @@ function createI18n(languages, localeStorageKey = DEFAULT_LOCALE_STORAGE_KEY) {
236
236
  localeStorageKey,
237
237
  localeSchema
238
238
  );
239
+ let initialized = false;
239
240
  const i18n = {
240
241
  get languages() {
241
242
  return languages;
@@ -254,11 +255,16 @@ function createI18n(languages, localeStorageKey = DEFAULT_LOCALE_STORAGE_KEY) {
254
255
  );
255
256
  },
256
257
  init(cookies) {
257
- if (cookies) {
258
- localeState.init(cookies);
258
+ if (initialized) {
259
+ return;
260
+ }
261
+ const resolvedCookies = typeof cookies === "function" ? cookies() : cookies;
262
+ if (resolvedCookies) {
263
+ localeState.init(resolvedCookies);
259
264
  }
260
265
  const { set } = ensureContext();
261
266
  set(i18n);
267
+ initialized = true;
262
268
  }
263
269
  };
264
270
  i18nInternals.set(i18n, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sveltebase/i18n",
3
- "version": "0.3.2",
3
+ "version": "0.3.4",
4
4
  "type": "module",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -18,7 +18,7 @@
18
18
  }
19
19
  },
20
20
  "dependencies": {
21
- "@sveltebase/state": "0.3.2",
21
+ "@sveltebase/state": "0.3.4",
22
22
  "date-fns": "^4.1.0",
23
23
  "use-intl": "^4.4.0",
24
24
  "zod": "^4.1.11"