@zachhandley/ez-i18n 0.3.15 → 0.3.17

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.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { AstroIntegration } from 'astro';
2
- import { E as EzI18nConfig } from './types-Cg77gLzO.js';
3
- export { T as TranslateFunction } from './types-Cg77gLzO.js';
2
+ import { E as EzI18nConfig } from './types-CE7B9s_c.js';
3
+ export { T as TranslateFunction } from './types-CE7B9s_c.js';
4
4
 
5
5
  /**
6
6
  * Comprehensive locale database for ez-i18n
package/dist/index.js CHANGED
@@ -966,7 +966,7 @@ function ezI18n(config) {
966
966
  import { initLocale, setTranslations } from '@zachhandley/ez-i18n/runtime';
967
967
 
968
968
  document.addEventListener('astro:after-swap', () => {
969
- const initData = globalThis.__EZ_I18N_INIT__;
969
+ const initData = globalThis.__EZ_I18N__;
970
970
  if (initData) {
971
971
  initLocale(initData.locale, initData.translations);
972
972
  setTranslations(initData.translations);
@@ -46,7 +46,7 @@ var onRequest = defineMiddleware(async ({ cookies, request, locals, redirect },
46
46
  try {
47
47
  const { loadTranslations } = await import("ez-i18n:translations");
48
48
  locals.translations = await loadTranslations(locale);
49
- globalThis.__EZ_I18N_SSR__ = {
49
+ globalThis.__EZ_I18N__ = {
50
50
  locale,
51
51
  translations: locals.translations
52
52
  };
@@ -1,5 +1,6 @@
1
1
  import * as nanostores from 'nanostores';
2
2
  import { ReadableAtom } from 'nanostores';
3
+ import '../types-CE7B9s_c.js';
3
4
 
4
5
  /**
5
6
  * Get nested value from object using dot notation
@@ -50,6 +51,8 @@ declare function setLocale(locale: string, options?: string | {
50
51
  cookieName?: string;
51
52
  loadTranslations?: TranslationLoader;
52
53
  redirect?: boolean;
54
+ /** Async callback to run before redirect (e.g., save preferences to backend) */
55
+ beforeRedirect?: (locale: string) => Promise<void>;
53
56
  }): Promise<void>;
54
57
  /**
55
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();
@@ -81,7 +84,7 @@ function getTranslations() {
81
84
  function getTranslationsWithSSRFallback() {
82
85
  const trans = translations.get();
83
86
  if (Object.keys(trans).length === 0) {
84
- const ssrTrans = globalThis.__EZ_I18N_SSR__?.translations;
87
+ const ssrTrans = globalThis.__EZ_I18N__?.translations;
85
88
  if (ssrTrans) {
86
89
  return ssrTrans;
87
90
  }
@@ -101,7 +104,7 @@ function t(key, params) {
101
104
  }
102
105
  function tc(key, params) {
103
106
  return computed(translations, (trans) => {
104
- const effectiveTrans = Object.keys(trans).length === 0 ? globalThis.__EZ_I18N_SSR__?.translations ?? trans : trans;
107
+ const effectiveTrans = Object.keys(trans).length === 0 ? globalThis.__EZ_I18N__?.translations ?? trans : trans;
105
108
  const value = getNestedValue(effectiveTrans, key);
106
109
  if (typeof value !== "string") {
107
110
  if (typeof import.meta !== "undefined" && import.meta.env?.DEV) {
@@ -84,6 +84,10 @@ type TranslateFunction = (key: string, params?: Record<string, string | number>)
84
84
  /**
85
85
  * Augment Astro's locals type
86
86
  */
87
+ interface EzI18nContext {
88
+ locale: string;
89
+ translations: Record<string, unknown>;
90
+ }
87
91
  declare global {
88
92
  namespace App {
89
93
  interface Locals {
@@ -95,6 +99,10 @@ declare global {
95
99
  t: TranslateFunction;
96
100
  }
97
101
  }
102
+ var __EZ_I18N__: EzI18nContext | undefined;
103
+ var __EZ_I18N_ASSETS__: {
104
+ fetch: (req: Request | URL | string) => Promise<Response>;
105
+ } | undefined;
98
106
  }
99
107
 
100
108
  export type { EzI18nConfig as E, LocaleTranslationPath as L, TranslateFunction as T, TranslationsConfig as a, TranslationCache as b };
@@ -1,4 +1,4 @@
1
- import { L as LocaleTranslationPath, a as TranslationsConfig, b as TranslationCache } from '../types-Cg77gLzO.js';
1
+ import { L as LocaleTranslationPath, a as TranslationsConfig, b as TranslationCache } from '../types-CE7B9s_c.js';
2
2
 
3
3
  type PathType = 'file' | 'folder' | 'glob' | 'array';
4
4
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zachhandley/ez-i18n",
3
- "version": "0.3.15",
3
+ "version": "0.3.17",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -44,7 +44,7 @@ const serializedTranslations = JSON.stringify(translations);
44
44
  const translations = JSON.parse(script.dataset.ezI18nTranslations || '{}');
45
45
 
46
46
  // Store for runtime initialization (globalThis is SSR-safe and equals window in browsers)
47
- globalThis.__EZ_I18N_INIT__ = { locale, translations };
47
+ globalThis.__EZ_I18N__ = { locale, translations };
48
48
  })();
49
49
  </script>
50
50
 
@@ -53,9 +53,9 @@ const serializedTranslations = JSON.stringify(translations);
53
53
  import { initLocale, setTranslations } from '@zachhandley/ez-i18n/runtime';
54
54
 
55
55
  // Get initialization data from inline script
56
- // Note: Don't delete __EZ_I18N_INIT__ here - other bundles (Vue, React)
56
+ // Note: Don't delete __EZ_I18N__ here - other bundles (Vue, React)
57
57
  // may need to read it to initialize their own store instances
58
- const initData = (globalThis as any).__EZ_I18N_INIT__;
58
+ const initData = globalThis.__EZ_I18N__;
59
59
  if (initData) {
60
60
  initLocale(initData.locale, initData.translations);
61
61
  setTranslations(initData.translations);