@zachhandley/ez-i18n 0.3.5 → 0.3.6

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.
@@ -1,5 +1,15 @@
1
1
  // src/middleware.ts
2
2
  import { defineMiddleware } from "astro:middleware";
3
+ function getCookieDomain(hostname) {
4
+ if (hostname === "localhost" || hostname === "127.0.0.1") {
5
+ return void 0;
6
+ }
7
+ const parts = hostname.split(".");
8
+ if (parts.length >= 2) {
9
+ return "." + parts.slice(-2).join(".");
10
+ }
11
+ return void 0;
12
+ }
3
13
  function createT(translations) {
4
14
  return (key, params) => {
5
15
  const keys = key.split(".");
@@ -13,7 +23,7 @@ function createT(translations) {
13
23
  return value.replace(/\{(\w+)\}/g, (_, p) => String(params[p] ?? `{${p}}`));
14
24
  };
15
25
  }
16
- var onRequest = defineMiddleware(async ({ cookies, request, locals }, next) => {
26
+ var onRequest = defineMiddleware(async ({ cookies, request, locals, redirect }, next) => {
17
27
  const { locales, defaultLocale, cookieName } = await import("ez-i18n:config");
18
28
  const url = new URL(request.url);
19
29
  const langParam = url.searchParams.get("lang");
@@ -37,12 +47,17 @@ var onRequest = defineMiddleware(async ({ cookies, request, locals }, next) => {
37
47
  }
38
48
  locals.t = createT(locals.translations);
39
49
  if (langParam && langParam !== cookieValue && locales.includes(langParam)) {
50
+ const domain = getCookieDomain(url.hostname);
40
51
  cookies.set(cookieName, locale, {
41
52
  path: "/",
42
53
  maxAge: 60 * 60 * 24 * 365,
43
54
  // 1 year
44
- sameSite: "lax"
55
+ sameSite: "lax",
56
+ ...domain && { domain }
45
57
  });
58
+ const cleanUrl = new URL(url);
59
+ cleanUrl.searchParams.delete("lang");
60
+ return redirect(cleanUrl.toString());
46
61
  }
47
62
  return next();
48
63
  });
@@ -38,6 +38,7 @@ type TranslationLoader = () => Promise<{
38
38
  declare function setLocale(locale: string, options?: string | {
39
39
  cookieName?: string;
40
40
  loadTranslations?: TranslationLoader;
41
+ redirect?: boolean;
41
42
  }): Promise<void>;
42
43
  /**
43
44
  * Get current locale value (non-reactive)
@@ -24,7 +24,13 @@ function setTranslations(trans) {
24
24
  }
25
25
  async function setLocale(locale, options = {}) {
26
26
  const opts = typeof options === "string" ? { cookieName: options } : options;
27
- const { cookieName = "ez-locale", loadTranslations } = opts;
27
+ const { cookieName = "ez-locale", loadTranslations, redirect } = opts;
28
+ if (redirect && typeof window !== "undefined") {
29
+ const url = new URL(window.location.href);
30
+ url.searchParams.set("lang", locale);
31
+ window.location.href = url.toString();
32
+ return;
33
+ }
28
34
  localeLoading.set(true);
29
35
  try {
30
36
  if (loadTranslations) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zachhandley/ez-i18n",
3
- "version": "0.3.5",
3
+ "version": "0.3.6",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },