@zachhandley/ez-i18n 0.3.16 → 0.3.18

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 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
  }
@@ -51,6 +51,8 @@ declare function setLocale(locale: string, options?: string | {
51
51
  cookieName?: string;
52
52
  loadTranslations?: TranslationLoader;
53
53
  redirect?: boolean;
54
+ /** Async callback to run before redirect (e.g., save preferences to backend) */
55
+ beforeRedirect?: (locale: string) => Promise<void>;
54
56
  }): Promise<void>;
55
57
  /**
56
58
  * Get current locale value (non-reactive)
@@ -41,8 +41,11 @@ function setTranslations(trans) {
41
41
  }
42
42
  async function setLocale(locale, options = {}) {
43
43
  const opts = typeof options === "string" ? { cookieName: options } : options;
44
- const { cookieName = "ez-locale", loadTranslations, redirect } = opts;
44
+ const { cookieName = "ez-locale", loadTranslations, redirect, beforeRedirect } = opts;
45
45
  if (redirect && typeof window !== "undefined") {
46
+ if (beforeRedirect) {
47
+ await beforeRedirect(locale);
48
+ }
46
49
  const url = new URL(window.location.href);
47
50
  url.searchParams.set("lang", locale);
48
51
  window.location.href = url.toString();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zachhandley/ez-i18n",
3
- "version": "0.3.16",
3
+ "version": "0.3.18",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },