Language Switcher is a dropdown for choosing the site language. Items are plain links, so switching works without JavaScript; with JavaScript you can handle the choice yourself. Built on Dropdown Menu, keyboard accessible.
Browser support
Opens the menu in the browser top layer, even before JavaScript.
Positions the menu by its trigger; JavaScript positions it when anchors are unavailable.
Preview
Installation
Usage
With next-intl
Pass a link to the same page for every locale, and change the language without a reload in onValueChange.
"use client"
import { useLocale } from "next-intl"
import { getPathname, usePathname, useRouter } from "@/i18n/navigation"
import { routing } from "@/i18n/routing"
import { LanguageSwitcher } from "@/components/ui/language-switcher"
const NAMES = { en: "English", ru: "Русский" }
export function SiteLanguageSwitcher() {
const locale = useLocale()
const pathname = usePathname()
const router = useRouter()
return (
<LanguageSwitcher
value={locale}
languages={routing.locales.map((code) => ({
code,
label: NAMES[code],
href: getPathname({ href: pathname, locale: code }),
}))}
onValueChange={(code) => router.replace(pathname, { locale: code })}
/>
)
}Links without JavaScript
With href and no onValueChange the items are ordinary links. Try it in the preview below with JavaScript disabled: the menu still opens, and a click follows the link.
import { LanguageSwitcher } from "@/components/ui/language-switcher"
<LanguageSwitcher
value="en"
showLabel
languages={[
{ code: "en", label: "English", href: "/" },
{ code: "ru", label: "Русский", href: "/ru" },
]}
/>Examples
With flags
Any node in flag shows before the name, here emoji.
<LanguageSwitcher
value="en"
showLabel
onValueChange={setLanguage}
languages={[
{ code: "en", label: "English", flag: <span aria-hidden>🇬🇧</span> },
{ code: "ru", label: "Русский", flag: <span aria-hidden>🇷🇺</span> },
{ code: "de", label: "Deutsch", flag: <span aria-hidden>🇩🇪</span> },
{ code: "fr", label: "Français", flag: <span aria-hidden>🇫🇷</span> },
{ code: "es", label: "Español", flag: <span aria-hidden>🇪🇸</span> },
]}
/>Select variant
Full width with the language name: fits a mobile menu or a form.
<LanguageSwitcher variant="select" value="en" languages={languages} />Buttons instead of links
Without href the items are buttons and call onValueChange.
const [language, setLanguage] = useState("en")
<LanguageSwitcher
value={language}
onValueChange={setLanguage}
languages={[
{ code: "en", label: "English" },
{ code: "ru", label: "Русский" },
]}
/>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| languages | { code: string; label: string; href?: string; flag?: ReactNode }[] | — | Languages: code, label, optional href and flag. |
| value | string | — | Code of the current language. |
| onValueChange | (code: string) => void | — | Called on choice. For items with href it replaces the page load. |
| labels | { language?: string } | { language: "Language" } | Texts: aria-label of the trigger (language). |
| size | "sm" | "default" | "lg" | "default" | Trigger size. |
| variant | "button" | "select" | "button" | button is compact, select is full width. |
| showCode | boolean | true (button) | Show the language code. Default: true for button. |
| showLabel | boolean | true (select) | Show the language name. Default: true for select. |
| align | "start" | "center" | "end" | "end" | Alignment of the menu along the trigger. |
| className | string | — | Additional CSS classes for the trigger. |