@sveltebase/i18n 0.3.4 → 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.
- package/dist/index.js +54 -20
- package/package.json +3 -5
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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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 =
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
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
|
-
|
|
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 =
|
|
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
|
+
"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.
|
|
22
|
-
"
|
|
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"
|