Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions apps/web/src/layouts/Layout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ interface Props {
noindex?: boolean;
}

// Astro.currentLocale tracks the language code matched by the URL even when the route is rewritten via fallback.
// Locale resolution order (Vercel rewrites lose the prefix from Astro's view,
// so we look in three places, top-priority first):
// 1. ?_locale= query param injected by the vercel.json rewrites
// 2. Astro.currentLocale — set when Astro's own i18n routing matches
// 3. URL prefix (covers local dev with the Astro server)
const CURRENT_TO_LOCALE: Record<string, Locale> = {
en: "en",
"pt-br": "pt-BR",
Expand All @@ -16,8 +20,17 @@ const CURRENT_TO_LOCALE: Record<string, Locale> = {
es: "es",
fr: "fr",
};
const cl = Astro.currentLocale;
const locale: Locale = (cl && CURRENT_TO_LOCALE[cl]) || "en";

function resolveLocale(): Locale {
const fromQuery = Astro.url.searchParams.get("_locale");
if (fromQuery && CURRENT_TO_LOCALE[fromQuery]) return CURRENT_TO_LOCALE[fromQuery];
const cl = Astro.currentLocale;
if (cl && CURRENT_TO_LOCALE[cl]) return CURRENT_TO_LOCALE[cl];
const seg = Astro.url.pathname.split("/").filter(Boolean)[0]?.toLowerCase();
if (seg && CURRENT_TO_LOCALE[seg]) return CURRENT_TO_LOCALE[seg];
return "en";
}
const locale = resolveLocale();
setLocale(locale);

const {
Expand Down
11 changes: 11 additions & 0 deletions apps/web/vercel.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
{
"rewrites": [
{ "source": "/pt-br", "destination": "/?_locale=pt-BR" },
{ "source": "/pt-br/", "destination": "/?_locale=pt-BR" },
{ "source": "/pt-br/:path*", "destination": "/:path*?_locale=pt-BR" },
{ "source": "/es", "destination": "/?_locale=es" },
{ "source": "/es/", "destination": "/?_locale=es" },
{ "source": "/es/:path*", "destination": "/:path*?_locale=es" },
{ "source": "/fr", "destination": "/?_locale=fr" },
{ "source": "/fr/", "destination": "/?_locale=fr" },
{ "source": "/fr/:path*", "destination": "/:path*?_locale=fr" }
],
"headers": [
{
"source": "/(.*)",
Expand Down