@zachhandley/ez-i18n 0.3.8 → 0.3.9
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/middleware.js +6 -141
- package/dist/runtime/index.js +13 -2
- package/package.json +1 -1
package/dist/middleware.js
CHANGED
|
@@ -1,139 +1,3 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
-
var __esm = (fn, res) => function __init() {
|
|
4
|
-
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
5
|
-
};
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
// src/runtime/store.ts
|
|
12
|
-
var store_exports = {};
|
|
13
|
-
__export(store_exports, {
|
|
14
|
-
effectiveLocale: () => effectiveLocale,
|
|
15
|
-
getLocale: () => getLocale,
|
|
16
|
-
getNestedValue: () => getNestedValue,
|
|
17
|
-
getTranslations: () => getTranslations,
|
|
18
|
-
initLocale: () => initLocale,
|
|
19
|
-
interpolate: () => interpolate,
|
|
20
|
-
localeLoading: () => localeLoading,
|
|
21
|
-
localePreference: () => localePreference,
|
|
22
|
-
setLocale: () => setLocale,
|
|
23
|
-
setTranslations: () => setTranslations,
|
|
24
|
-
t: () => t,
|
|
25
|
-
tc: () => tc,
|
|
26
|
-
translations: () => translations
|
|
27
|
-
});
|
|
28
|
-
import { atom, computed } from "nanostores";
|
|
29
|
-
import { persistentAtom } from "@nanostores/persistent";
|
|
30
|
-
function getNestedValue(obj, path) {
|
|
31
|
-
const keys = path.split(".");
|
|
32
|
-
let value = obj;
|
|
33
|
-
for (const key of keys) {
|
|
34
|
-
if (value == null || typeof value !== "object") {
|
|
35
|
-
return void 0;
|
|
36
|
-
}
|
|
37
|
-
value = value[key];
|
|
38
|
-
}
|
|
39
|
-
return value;
|
|
40
|
-
}
|
|
41
|
-
function interpolate(str, params) {
|
|
42
|
-
if (!params) return str;
|
|
43
|
-
return str.replace(/\{(\w+)\}/g, (match, key) => {
|
|
44
|
-
return key in params ? String(params[key]) : match;
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
function initLocale(locale, trans) {
|
|
48
|
-
serverLocale.set(locale);
|
|
49
|
-
localePreference.set(locale);
|
|
50
|
-
if (trans) {
|
|
51
|
-
translations.set(trans);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
function setTranslations(trans) {
|
|
55
|
-
translations.set(trans);
|
|
56
|
-
}
|
|
57
|
-
async function setLocale(locale, options = {}) {
|
|
58
|
-
const opts = typeof options === "string" ? { cookieName: options } : options;
|
|
59
|
-
const { cookieName = "ez-locale", loadTranslations, redirect } = opts;
|
|
60
|
-
if (redirect && typeof window !== "undefined") {
|
|
61
|
-
const url = new URL(window.location.href);
|
|
62
|
-
url.searchParams.set("lang", locale);
|
|
63
|
-
window.location.href = url.toString();
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
localeLoading.set(true);
|
|
67
|
-
try {
|
|
68
|
-
if (loadTranslations) {
|
|
69
|
-
const mod = await loadTranslations();
|
|
70
|
-
const trans = "default" in mod ? mod.default : mod;
|
|
71
|
-
translations.set(trans);
|
|
72
|
-
}
|
|
73
|
-
localePreference.set(locale);
|
|
74
|
-
serverLocale.set(locale);
|
|
75
|
-
if (typeof document !== "undefined") {
|
|
76
|
-
document.cookie = `${cookieName}=${locale}; path=/; max-age=31536000; samesite=lax`;
|
|
77
|
-
}
|
|
78
|
-
if (typeof document !== "undefined") {
|
|
79
|
-
document.dispatchEvent(
|
|
80
|
-
new CustomEvent("ez-i18n:locale-changed", {
|
|
81
|
-
detail: { locale },
|
|
82
|
-
bubbles: true
|
|
83
|
-
})
|
|
84
|
-
);
|
|
85
|
-
}
|
|
86
|
-
} finally {
|
|
87
|
-
localeLoading.set(false);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
function getLocale() {
|
|
91
|
-
return effectiveLocale.get();
|
|
92
|
-
}
|
|
93
|
-
function getTranslations() {
|
|
94
|
-
return translations.get();
|
|
95
|
-
}
|
|
96
|
-
function t(key, params) {
|
|
97
|
-
const trans = translations.get();
|
|
98
|
-
const value = getNestedValue(trans, key);
|
|
99
|
-
if (typeof value !== "string") {
|
|
100
|
-
if (typeof import.meta !== "undefined" && import.meta.env?.DEV) {
|
|
101
|
-
console.warn("[ez-i18n] Missing translation:", key);
|
|
102
|
-
}
|
|
103
|
-
return key;
|
|
104
|
-
}
|
|
105
|
-
return interpolate(value, params);
|
|
106
|
-
}
|
|
107
|
-
function tc(key, params) {
|
|
108
|
-
return computed(translations, (trans) => {
|
|
109
|
-
const value = getNestedValue(trans, key);
|
|
110
|
-
if (typeof value !== "string") {
|
|
111
|
-
if (typeof import.meta !== "undefined" && import.meta.env?.DEV) {
|
|
112
|
-
console.warn("[ez-i18n] Missing translation:", key);
|
|
113
|
-
}
|
|
114
|
-
return key;
|
|
115
|
-
}
|
|
116
|
-
return interpolate(value, params);
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
var serverLocale, localePreference, effectiveLocale, translations, localeLoading;
|
|
120
|
-
var init_store = __esm({
|
|
121
|
-
"src/runtime/store.ts"() {
|
|
122
|
-
"use strict";
|
|
123
|
-
serverLocale = atom(null);
|
|
124
|
-
localePreference = persistentAtom("ez-locale", "en", {
|
|
125
|
-
encode: (value) => value,
|
|
126
|
-
decode: (value) => value
|
|
127
|
-
});
|
|
128
|
-
effectiveLocale = computed(
|
|
129
|
-
[serverLocale, localePreference],
|
|
130
|
-
(server, client) => server ?? client
|
|
131
|
-
);
|
|
132
|
-
translations = atom({});
|
|
133
|
-
localeLoading = atom(false);
|
|
134
|
-
}
|
|
135
|
-
});
|
|
136
|
-
|
|
137
1
|
// src/middleware.ts
|
|
138
2
|
import { defineMiddleware } from "astro:middleware";
|
|
139
3
|
function getCookieDomain(hostname) {
|
|
@@ -146,10 +10,10 @@ function getCookieDomain(hostname) {
|
|
|
146
10
|
}
|
|
147
11
|
return void 0;
|
|
148
12
|
}
|
|
149
|
-
function createT(
|
|
13
|
+
function createT(translations) {
|
|
150
14
|
return (key, params) => {
|
|
151
15
|
const keys = key.split(".");
|
|
152
|
-
let value =
|
|
16
|
+
let value = translations;
|
|
153
17
|
for (const k of keys) {
|
|
154
18
|
if (value == null || typeof value !== "object") return key;
|
|
155
19
|
value = value[k];
|
|
@@ -178,9 +42,10 @@ var onRequest = defineMiddleware(async ({ cookies, request, locals, redirect },
|
|
|
178
42
|
try {
|
|
179
43
|
const { loadTranslations } = await import("ez-i18n:translations");
|
|
180
44
|
locals.translations = await loadTranslations(locale);
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
45
|
+
globalThis.__EZ_I18N_SSR__ = {
|
|
46
|
+
locale,
|
|
47
|
+
translations: locals.translations
|
|
48
|
+
};
|
|
184
49
|
} catch {
|
|
185
50
|
locals.translations = {};
|
|
186
51
|
}
|
package/dist/runtime/index.js
CHANGED
|
@@ -78,8 +78,18 @@ function getLocale() {
|
|
|
78
78
|
function getTranslations() {
|
|
79
79
|
return translations.get();
|
|
80
80
|
}
|
|
81
|
-
function
|
|
81
|
+
function getTranslationsWithSSRFallback() {
|
|
82
82
|
const trans = translations.get();
|
|
83
|
+
if (Object.keys(trans).length === 0) {
|
|
84
|
+
const ssrTrans = globalThis.__EZ_I18N_SSR__?.translations;
|
|
85
|
+
if (ssrTrans) {
|
|
86
|
+
return ssrTrans;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return trans;
|
|
90
|
+
}
|
|
91
|
+
function t(key, params) {
|
|
92
|
+
const trans = getTranslationsWithSSRFallback();
|
|
83
93
|
const value = getNestedValue(trans, key);
|
|
84
94
|
if (typeof value !== "string") {
|
|
85
95
|
if (typeof import.meta !== "undefined" && import.meta.env?.DEV) {
|
|
@@ -91,7 +101,8 @@ function t(key, params) {
|
|
|
91
101
|
}
|
|
92
102
|
function tc(key, params) {
|
|
93
103
|
return computed(translations, (trans) => {
|
|
94
|
-
const
|
|
104
|
+
const effectiveTrans = Object.keys(trans).length === 0 ? globalThis.__EZ_I18N_SSR__?.translations ?? trans : trans;
|
|
105
|
+
const value = getNestedValue(effectiveTrans, key);
|
|
95
106
|
if (typeof value !== "string") {
|
|
96
107
|
if (typeof import.meta !== "undefined" && import.meta.env?.DEV) {
|
|
97
108
|
console.warn("[ez-i18n] Missing translation:", key);
|