@sveltebase/i18n 0.3.3 → 0.4.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.
Files changed (3) hide show
  1. package/README.md +22 -6
  2. package/dist/index.js +54 -20
  3. package/package.json +3 -5
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.js CHANGED
@@ -1,19 +1,49 @@
1
1
  // src/create-i18n.ts
2
2
  import { createContext } from "svelte";
3
3
  import { PersistentState } from "@sveltebase/state";
4
- import { z } from "zod";
5
4
 
6
5
  // src/utils.ts
7
6
  import { createFormatter, createTranslator } from "use-intl/core";
8
7
  import { SvelteDate } from "svelte/reactivity";
9
- import {
10
- differenceInMinutes,
11
- format as formatTimeWithDateFns,
12
- isThisWeek,
13
- isThisYear,
14
- isToday,
15
- isYesterday
16
- } from "date-fns";
8
+ function startOfDay(value) {
9
+ return new SvelteDate(value.getFullYear(), value.getMonth(), value.getDate());
10
+ }
11
+ function differenceInMinutes(later, earlier) {
12
+ return Math.floor((later.getTime() - earlier.getTime()) / 6e4);
13
+ }
14
+ function isSameDay(left, right) {
15
+ return left.getFullYear() === right.getFullYear() && left.getMonth() === right.getMonth() && left.getDate() === right.getDate();
16
+ }
17
+ function isToday(value) {
18
+ return isSameDay(value, new SvelteDate());
19
+ }
20
+ function isYesterday(value) {
21
+ const yesterday = startOfDay(new SvelteDate());
22
+ yesterday.setDate(yesterday.getDate() - 1);
23
+ return isSameDay(value, yesterday);
24
+ }
25
+ function isThisYear(value) {
26
+ return value.getFullYear() === new SvelteDate().getFullYear();
27
+ }
28
+ function isThisWeek(value, options) {
29
+ const weekStartsOn = options?.weekStartsOn ?? 0;
30
+ const now = new SvelteDate();
31
+ const currentDay = now.getDay();
32
+ const diffToWeekStart = (currentDay - weekStartsOn + 7) % 7;
33
+ const weekStart = startOfDay(now);
34
+ weekStart.setDate(now.getDate() - diffToWeekStart);
35
+ const weekEnd = startOfDay(weekStart);
36
+ weekEnd.setDate(weekStart.getDate() + 7);
37
+ return value >= weekStart && value < weekEnd;
38
+ }
39
+ function formatTimeWithDateFns(value, format) {
40
+ if (format !== "HH:mm") {
41
+ throw new Error(`Unsupported format: ${format}`);
42
+ }
43
+ const hours = String(value.getHours()).padStart(2, "0");
44
+ const minutes = String(value.getMinutes()).padStart(2, "0");
45
+ return `${hours}:${minutes}`;
46
+ }
17
47
  var UZ_WEEKDAYS = [
18
48
  "Yakshanba",
19
49
  "Dushanba",
@@ -221,17 +251,21 @@ function createI18n(languages, localeStorageKey = DEFAULT_LOCALE_STORAGE_KEY) {
221
251
  ensureContext();
222
252
  const localeCodes = getLocaleCodes(languages);
223
253
  const fallbackLocale = getDefaultLocale(languages);
224
- const localeSchema = z.string().optional().transform((value, context) => {
225
- const nextLocale = value ?? fallbackLocale;
226
- if (localeCodes.includes(nextLocale)) {
227
- return nextLocale;
254
+ const localeSchema = {
255
+ "~standard": {
256
+ version: 1,
257
+ vendor: "@sveltebase/i18n",
258
+ validate(value) {
259
+ const nextLocale = value == null ? fallbackLocale : typeof value === "string" ? value : null;
260
+ if (nextLocale && localeCodes.includes(nextLocale)) {
261
+ return { value: nextLocale };
262
+ }
263
+ return {
264
+ issues: [{ message: `Invalid locale "${String(value)}"` }]
265
+ };
266
+ }
228
267
  }
229
- context.addIssue({
230
- code: "custom",
231
- message: `Invalid locale "${String(value)}"`
232
- });
233
- return z.NEVER;
234
- });
268
+ };
235
269
  const localeState = new PersistentState(
236
270
  localeStorageKey,
237
271
  localeSchema
@@ -245,7 +279,7 @@ function createI18n(languages, localeStorageKey = DEFAULT_LOCALE_STORAGE_KEY) {
245
279
  return localeState.current;
246
280
  },
247
281
  set locale(nextLocale) {
248
- localeState.current = localeSchema.parse(nextLocale);
282
+ localeState.current = nextLocale;
249
283
  },
250
284
  get currentLanguage() {
251
285
  return getLanguage(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sveltebase/i18n",
3
- "version": "0.3.3",
3
+ "version": "0.4.0",
4
4
  "type": "module",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -18,10 +18,8 @@
18
18
  }
19
19
  },
20
20
  "dependencies": {
21
- "@sveltebase/state": "0.3.3",
22
- "date-fns": "^4.1.0",
23
- "use-intl": "^4.4.0",
24
- "zod": "^4.1.11"
21
+ "@sveltebase/state": "0.4.0",
22
+ "use-intl": "^4.4.0"
25
23
  },
26
24
  "peerDependencies": {
27
25
  "svelte": "^5.0.0"