@zachhandley/ez-i18n 0.3.7 → 0.3.8

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 (2) hide show
  1. package/dist/middleware.js +141 -2
  2. package/package.json +1 -1
@@ -1,3 +1,139 @@
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
+
1
137
  // src/middleware.ts
2
138
  import { defineMiddleware } from "astro:middleware";
3
139
  function getCookieDomain(hostname) {
@@ -10,10 +146,10 @@ function getCookieDomain(hostname) {
10
146
  }
11
147
  return void 0;
12
148
  }
13
- function createT(translations) {
149
+ function createT(translations2) {
14
150
  return (key, params) => {
15
151
  const keys = key.split(".");
16
- let value = translations;
152
+ let value = translations2;
17
153
  for (const k of keys) {
18
154
  if (value == null || typeof value !== "object") return key;
19
155
  value = value[k];
@@ -42,6 +178,9 @@ var onRequest = defineMiddleware(async ({ cookies, request, locals, redirect },
42
178
  try {
43
179
  const { loadTranslations } = await import("ez-i18n:translations");
44
180
  locals.translations = await loadTranslations(locale);
181
+ const { initLocale: initLocale2, setTranslations: setTranslations2 } = await Promise.resolve().then(() => (init_store(), store_exports));
182
+ initLocale2(locale, locals.translations);
183
+ setTranslations2(locals.translations);
45
184
  } catch {
46
185
  locals.translations = {};
47
186
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zachhandley/ez-i18n",
3
- "version": "0.3.7",
3
+ "version": "0.3.8",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },