GooseUI

Components

Language Switcher

New

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

Popover

Opens the menu in the browser top layer, even before JavaScript.

Your browser:
Anchor positioning

Positions the menu by its trigger; JavaScript positions it when anchors are unavailable.

Your browser:

Preview

Installation

npx shadcn@latest add @gooseui/language-switcher
pnpm dlx shadcn@latest add @gooseui/language-switcher
yarn dlx shadcn@latest add @gooseui/language-switcher
bunx --bun shadcn@latest add @gooseui/language-switcher

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

PropTypeDefaultDescription
languages{ code: string; label: string; href?: string; flag?: ReactNode }[]—Languages: code, label, optional href and flag.
valuestring—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.
showCodebooleantrue (button)Show the language code. Default: true for button.
showLabelbooleantrue (select)Show the language name. Default: true for select.
align"start" | "center" | "end""end"Alignment of the menu along the trigger.
classNamestring—Additional CSS classes for the trigger.