Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

🐛 Use the route locale in the landing language switcher (#3648)

Luke Vella78bc44572e7b
diff --git a/apps/landing/src/app/[locale]/(main)/language-select.tsx b/apps/landing/src/app/[locale]/(main)/language-select.tsx
index 2f20cabf5..b75f31b98 100644
--- a/apps/landing/src/app/[locale]/(main)/language-select.tsx
+++ b/apps/landing/src/app/[locale]/(main)/language-select.tsx
@@ -1,6 +1,6 @@
"use client";
-import languages, { supportedLngs } from "@rallly/languages";
+import languages, { defaultLocale } from "@rallly/languages";
import {
Select,
SelectContent,
@@ -8,29 +8,30 @@ import {
SelectTrigger,
SelectValue,
} from "@rallly/ui/select";
-import { usePathname, useRouter } from "next/navigation";
+import { useParams, usePathname, useRouter } from "next/navigation";
import { useTranslation } from "@/i18n/client/use-translation";
export const LanguageSelect = () => {
const router = useRouter();
const pathname = usePathname() ?? "";
- const { t, i18n } = useTranslation();
+ const { t } = useTranslation();
+ const params = useParams<{ locale?: string }>();
+ const locale = params.locale ?? defaultLocale;
return (
<Select
items={languages}
- value={i18n.language}
+ value={locale}
onValueChange={(newLocale) => {
if (!newLocale) {
return;
}
- const isLocalizedPath = supportedLngs.some((lng) =>
- pathname?.startsWith(`/${lng}`),
- );
-
- const newPath = isLocalizedPath
- ? pathname.replace(new RegExp(`^/${i18n.language}`), "")
- : pathname;
+ // The default locale is served without a prefix, so only strip the
+ // current locale when it is actually the first path segment.
+ const newPath =
+ pathname === `/${locale}` || pathname.startsWith(`/${locale}/`)
+ ? pathname.slice(locale.length + 1)
+ : pathname;
router.replace(`/${newLocale}${newPath}`);
}}
diff --git a/apps/landing/src/app/[locale]/layout.tsx b/apps/landing/src/app/[locale]/layout.tsx
index ae791089c..ac92ef5cb 100644
--- a/apps/landing/src/app/[locale]/layout.tsx
+++ b/apps/landing/src/app/[locale]/layout.tsx
@@ -36,13 +36,10 @@ export default async function Root(props: {
const translations = i18n.store.data;
return (
- <html
- lang={i18n.resolvedLanguage}
- className={cn(sans.className, display.variable)}
- >
+ <html lang={locale} className={cn(sans.className, display.variable)}>
<body>
<LazyMotion features={domAnimation}>
- <I18nProvider locale={i18n.resolvedLanguage} resources={translations}>
+ <I18nProvider locale={locale} resources={translations}>
<PostHogInit>{children}</PostHogInit>
<Analytics />
</I18nProvider>