@zachhandley/ez-i18n 0.3.17 → 0.3.19

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
@@ -5,7 +5,7 @@ Cookie-based i18n for Astro. Ships the Astro integration plus the shared runtime
5
5
  ## Installation
6
6
 
7
7
  ```bash
8
- pnpm add @zachhandley/ez-i18n nanostores @nanostores/persistent
8
+ pnpm add @zachhandley/ez-i18n nanostores
9
9
  ```
10
10
 
11
11
  ## Astro Setup
package/dist/index.js CHANGED
@@ -593,36 +593,80 @@ export function tc(key, params) {
593
593
  }
594
594
  },
595
595
  configureServer(server) {
596
- const watchedDirs = /* @__PURE__ */ new Set();
596
+ const watchedDirToLocale = /* @__PURE__ */ new Map();
597
597
  if (config.translations) {
598
598
  if (typeof config.translations === "string") {
599
- watchedDirs.add(path2.resolve(viteConfig.root, config.translations));
599
+ const baseDir = path2.resolve(viteConfig.root, config.translations.replace(/\/$/, ""));
600
+ for (const locale of translationInfo.keys()) {
601
+ const localeDir = path2.join(baseDir, locale);
602
+ watchedDirToLocale.set(localeDir, locale);
603
+ }
600
604
  } else {
601
- for (const localePath of Object.values(config.translations)) {
605
+ for (const [locale, localePath] of Object.entries(config.translations)) {
602
606
  if (typeof localePath === "string") {
603
607
  const pathType = detectPathType(localePath);
604
608
  if (pathType === "folder") {
605
- watchedDirs.add(path2.resolve(viteConfig.root, localePath));
609
+ watchedDirToLocale.set(path2.resolve(viteConfig.root, localePath.replace(/\/$/, "")), locale);
606
610
  } else if (pathType === "glob") {
607
611
  const baseDir = localePath.split("*")[0].replace(/\/$/, "");
608
612
  if (baseDir) {
609
- watchedDirs.add(path2.resolve(viteConfig.root, baseDir));
613
+ watchedDirToLocale.set(path2.resolve(viteConfig.root, baseDir), locale);
610
614
  }
611
615
  }
612
616
  } else if (Array.isArray(localePath)) {
613
617
  for (const file of localePath) {
614
618
  const dir = path2.dirname(path2.resolve(viteConfig.root, file));
615
- watchedDirs.add(dir);
619
+ watchedDirToLocale.set(dir, locale);
616
620
  }
617
621
  }
618
622
  }
619
623
  }
620
624
  } else {
621
- watchedDirs.add(path2.resolve(viteConfig.root, "./public/i18n"));
625
+ const baseDir = path2.resolve(viteConfig.root, "./public/i18n");
626
+ for (const locale of translationInfo.keys()) {
627
+ const localeDir = path2.join(baseDir, locale);
628
+ watchedDirToLocale.set(localeDir, locale);
629
+ }
622
630
  }
623
- for (const dir of watchedDirs) {
631
+ for (const dir of watchedDirToLocale.keys()) {
624
632
  server.watcher.add(dir);
625
633
  }
634
+ server.watcher.on("add", (file) => {
635
+ if (!file.endsWith(".json")) return;
636
+ let locale;
637
+ for (const [dir, loc] of watchedDirToLocale) {
638
+ if (file.startsWith(dir + path2.sep) || file.startsWith(dir + "/")) {
639
+ locale = loc;
640
+ break;
641
+ }
642
+ }
643
+ if (!locale || !translationInfo.has(locale)) return;
644
+ const info = translationInfo.get(locale);
645
+ if (!info.files.includes(file)) {
646
+ info.files.push(file);
647
+ info.files.sort((a, b) => a.localeCompare(b));
648
+ }
649
+ const mod = server.moduleGraph.getModuleById(RESOLVED_PREFIX + VIRTUAL_TRANSLATIONS);
650
+ if (mod) {
651
+ server.moduleGraph.invalidateModule(mod);
652
+ server.ws.send({ type: "full-reload", path: "*" });
653
+ }
654
+ });
655
+ server.watcher.on("unlink", (file) => {
656
+ if (!file.endsWith(".json")) return;
657
+ for (const info of translationInfo.values()) {
658
+ const index = info.files.indexOf(file);
659
+ if (index !== -1) {
660
+ info.files.splice(index, 1);
661
+ const mod = server.moduleGraph.getModuleById(RESOLVED_PREFIX + VIRTUAL_TRANSLATIONS);
662
+ if (mod) {
663
+ server.moduleGraph.invalidateModule(mod);
664
+ server.ws.send({ type: "full-reload", path: "*" });
665
+ }
666
+ break;
667
+ }
668
+ }
669
+ });
626
670
  }
627
671
  };
628
672
  }
@@ -13,9 +13,11 @@ declare function getNestedValue(obj: Record<string, unknown>, path: string): unk
13
13
  */
14
14
  declare function interpolate(str: string, params?: Record<string, string | number>): string;
15
15
  /**
16
- * Client-side locale preference (persisted to localStorage)
16
+ * Client-side locale preference
17
+ * Note: Persistence is handled via cookies (set by middleware) and the hydration script
18
+ * that syncs localStorage. We use a regular atom to avoid proxy issues in Cloudflare Workers.
17
19
  */
18
- declare const localePreference: nanostores.WritableAtom<string>;
20
+ declare const localePreference: nanostores.PreinitializedWritableAtom<string> & object;
19
21
  /**
20
22
  * Effective locale - uses server locale if set, otherwise client preference
21
23
  */
@@ -1,6 +1,5 @@
1
1
  // src/runtime/store.ts
2
2
  import { atom, computed } from "nanostores";
3
- import { persistentAtom } from "@nanostores/persistent";
4
3
  function getNestedValue(obj, path) {
5
4
  const keys = path.split(".");
6
5
  let value = obj;
@@ -19,10 +18,7 @@ function interpolate(str, params) {
19
18
  });
20
19
  }
21
20
  var serverLocale = atom(null);
22
- var localePreference = persistentAtom("ez-locale", "en", {
23
- encode: (value) => value,
24
- decode: (value) => value
25
- });
21
+ var localePreference = atom("en");
26
22
  var effectiveLocale = computed(
27
23
  [serverLocale, localePreference],
28
24
  (server, client) => server ?? client
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zachhandley/ez-i18n",
3
- "version": "0.3.17",
3
+ "version": "0.3.19",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -56,12 +56,10 @@
56
56
  "tinyglobby": "^0.2.15"
57
57
  },
58
58
  "peerDependencies": {
59
- "@nanostores/persistent": "^0.10.0",
60
59
  "astro": "^4.0.0 || ^5.0.0",
61
60
  "nanostores": "^0.9.0 || ^0.10.0 || ^0.11.0"
62
61
  },
63
62
  "devDependencies": {
64
- "@nanostores/persistent": "^0.10.2",
65
63
  "@types/node": "^22.0.0",
66
64
  "astro": "^5.1.1",
67
65
  "nanostores": "^0.11.3",