How to apply a button that changes the language on the website with next-intl #532
Replies: 5 comments 13 replies
-
This page outlines the current recommendations for this situation: https://next-intl-docs.vercel.app/docs/environments/server-client-components Hope this helps! |
Beta Was this translation helpful? Give feedback.
-
i have the same issue (: how to solve? |
Beta Was this translation helpful? Give feedback.
-
@amannn , Hi, I am following this step guide: App Router setup without i18n routing import { headers } from "next/headers";
import { getRequestConfig } from "next-intl/server";
import { fetchTenant, fetchTranslationsForTenant } from "@/lib/fetchers";
export default getRequestConfig(async () => {
// Provide a static locale, fetch a user setting,
// read from `cookies()`, `headers()`, etc.
const domain = headers().get("host")?.replace(".localhost:3000", "") as string;
const tenant = await fetchTenant(domain);
const locale = tenant.locale;
const messages = await fetchTranslationsForTenant(domain);
return {
locale,
messages: messages[locale]
};
}); but how would I implement a locale switcher to change the languages based on the tenant supported locales? |
Beta Was this translation helpful? Give feedback.
-
The Link component's locale property does the work "next-intl": "^4.3.4", "use client";
import { Link, usePathname } from "@/i18n/navigation";
import { routing } from "@/i18n/routing";
import { Button, Menu } from "@mantine/core";
import { useLocale } from "next-intl";
export const LanguageSwitcher = () => {
const pathname = usePathname();
const locale = useLocale();
const otherLocales = routing.locales.filter((l) => l !== locale);
return (
<Menu>
<Menu.Target>
<Button>{locale}</Button>
</Menu.Target>
<Menu.Dropdown>
{otherLocales.map((l) => (
<Menu.Item key={l} component={Link} href={pathname} locale={l}>
{l.toUpperCase()}
</Menu.Item>
))}
</Menu.Dropdown>
</Menu>
);
}; |
Beta Was this translation helpful? Give feedback.
-
I had the same problem. After setting up all the files according to the doc. (i set everything up 100% like the doc) https://next-intl.dev/docs/getting-started/app-router
Here is my full code for the LangaugeSwitching component. Where I use Nextjs, next-intl, and Shadcn
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello, I am getting more and more familiar with the library and the developer experience is very positive. However I have a question where I do not know how to solve my issue:
I have the following component:
Here I have a button that expands when I click on it and provides two options, German and English. Now when one user clicks on english I want to display the website in english and the same with german. This component is imported into my NavBar that is a Server component. However this is a client component because I have useState here. Which I need to expand the window where the options are hidden.
Right now as expected I get an error:
I followed the initial setup and use the beta for server components. It works great. But I struggle a bit how to apply the translation function here? Can anyone help me please?
Beta Was this translation helpful? Give feedback.
All reactions