{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "language-switcher",
  "type": "registry:ui",
  "title": "Language Switcher",
  "description": "A menu for choosing the interface language: each language under its own name in its own script, the current one marked, no flags.",
  "categories": [
    "ui"
  ],
  "dependencies": [
    "lucide-react"
  ],
  "registryDependencies": [
    "@rivier-ui/theme",
    "@rivier-ui/button",
    "@rivier-ui/cn",
    "@rivier-ui/direction-provider",
    "@rivier-ui/dropdown-menu",
    "@rivier-ui/use-controllable-state"
  ],
  "files": [
    {
      "path": "registry/ui/language-switcher.tsx",
      "type": "registry:ui",
      "target": "components/ui/language-switcher.tsx",
      "content": "\"use client\";\n/**\n * @name language-switcher\n * @category ui\n * @description A menu for choosing the interface language: each language under its own name in its own script, the current one marked, no flags.\n * @when A product in more than one language: in the header or footer, and in settings. It only reports the choice (`onValueChange`): the project navigates to the translated page or stores the preference. A flag stands for a country, not a language, so none is shown. It is a menu of radio items and not a select, because choosing a language is an action that reloads or navigates: a select that changes the page as its value changes surprises keyboard and screen-reader users. For choosing a language as data in a form (a document's language) use select.\n * @example\n * <LanguageSwitcher value={locale} onValueChange={(next) => router.replace(pathname, { locale: next })} label=\"Language\" languages={[\"en\", \"fr\", \"ar\", \"ja\"]} />\n *\n * // With its own parts: names in the current language as a second line, and a name the project supplies\n * <LanguageSwitcher defaultValue=\"en\" label=\"Language\" displayLocale=\"en\">\n *   <LanguageSwitcherTrigger />\n *   <LanguageSwitcherContent align=\"end\">\n *     <LanguageSwitcherItem value=\"en\" />\n *     <LanguageSwitcherItem value=\"ar\" />\n *     <LanguageSwitcherItem value=\"dv\" name=\"ދިވެހި\" />\n *   </LanguageSwitcherContent>\n * </LanguageSwitcher>\n */\nimport { Globe } from \"lucide-react\";\nimport { createContext, use, type ComponentProps, type ReactNode } from \"react\";\nimport { buttonVariants } from \"@/components/ui/button\";\nimport { directionOf } from \"@/components/ui/direction-provider\";\nimport { DropdownMenu, DropdownMenuContent, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuTrigger } from \"@/components/ui/dropdown-menu\";\nimport { cn } from \"@/lib/cn\";\nimport { useControllableState } from \"@/lib/use-controllable-state\";\n\n/**\n * The name of a language: in that language itself by default (\"français\", \"العربية\"), or in another\n * (`languageName(\"fr\", \"en\")` is \"French\"). The first letter is capitalised by the rules of the language it\n * is written in. Falls back to the tag when the runtime does not know the language. Server and browser can\n * differ for rare languages: pass `name` to LanguageSwitcherItem for those.\n */\nexport function languageName(tag: string, inLocale: string = tag): string {\n  try {\n    const name = new Intl.DisplayNames([inLocale], { type: \"language\", fallback: \"none\" }).of(tag);\n    if (!name) return tag;\n    const first = name.codePointAt(0);\n    if (first === undefined) return tag;\n    const head = String.fromCodePoint(first);\n    return head.toLocaleUpperCase(inLocale) + name.slice(head.length);\n  } catch {\n    return tag;\n  }\n}\n\ntype LanguageContextValue = {\n  value: string;\n  setValue: (value: string) => void;\n  label: string;\n  displayLocale: string | undefined;\n  names: Record<string, string>;\n};\n\nconst LanguageContext = createContext<LanguageContextValue | null>(null);\n\nfunction useLanguage(part: string): LanguageContextValue {\n  const context = use(LanguageContext);\n  if (!context) throw new Error(`<${part}> must be used inside <LanguageSwitcher>.`);\n  return context;\n}\n\nexport type LanguageSwitcherProps = Omit<ComponentProps<typeof DropdownMenu>, \"children\"> & {\n  /** The current language, as a tag (\"en\", \"pt-BR\") (controlled). */\n  value?: string;\n  /** The language to begin with when uncontrolled. */\n  defaultValue?: string;\n  onValueChange?: (value: string) => void;\n  /**\n   * The control's name, in the current interface language (\"Language\"). It is read out before the current\n   * language's own name, and is not shown: the globe and the language's name are.\n   */\n  label: string;\n  /** The tags to offer, for the default parts. Order them the same way everywhere (not by popularity). */\n  languages?: string[];\n  /** Also show each language's name in this language, as a second line (\"French\" under \"Français\"). Usually the current interface language. */\n  displayLocale?: string;\n  /** Names to use instead of the runtime's, by tag, for languages it does not know or names the product prefers. */\n  names?: Record<string, string>;\n  /** The parts, when the default trigger and list are not what is wanted. */\n  children?: ReactNode;\n};\n\nexport function LanguageSwitcher({ value, defaultValue = \"en\", onValueChange, label, languages = [], displayLocale, names = {}, children, ...props }: LanguageSwitcherProps) {\n  const [current, setCurrent] = useControllableState({ value, defaultValue, onChange: onValueChange });\n  return (\n    <LanguageContext value={{ value: current, setValue: setCurrent, label, displayLocale, names }}>\n      <DropdownMenu {...props}>\n        {children ?? (\n          <>\n            <LanguageSwitcherTrigger />\n            <LanguageSwitcherContent>\n              {languages.map((tag) => (\n                <LanguageSwitcherItem key={tag} value={tag} />\n              ))}\n            </LanguageSwitcherContent>\n          </>\n        )}\n      </DropdownMenu>\n    </LanguageContext>\n  );\n}\n\nexport type LanguageSwitcherTriggerProps = ComponentProps<typeof DropdownMenuTrigger> & {\n  /** \"outline\" (default) or \"ghost\", as on button. */\n  variant?: \"outline\" | \"ghost\";\n  size?: \"sm\" | \"md\";\n};\n\n/** A globe and the current language's own name. Its accessible name is the label, then that name: \"Language: Français\". */\nexport function LanguageSwitcherTrigger({ className, variant = \"outline\", size = \"md\", children, ...props }: LanguageSwitcherTriggerProps) {\n  const { value, label, names } = useLanguage(\"LanguageSwitcherTrigger\");\n  return (\n    <DropdownMenuTrigger data-slot=\"language-switcher-trigger\" className={cn(buttonVariants({ variant, size }), \"max-w-full\", className)} {...props}>\n      {children ?? (\n        <>\n          <Globe aria-hidden=\"true\" strokeWidth={1.5} />\n          <span className=\"sr-only\">{label}: </span>\n          <span lang={value} className=\"min-w-0 truncate\">\n            {names[value] ?? languageName(value)}\n          </span>\n        </>\n      )}\n    </DropdownMenuTrigger>\n  );\n}\n\n/** The list. It scrolls when there are many languages. */\nexport function LanguageSwitcherContent({ className, children, align = \"end\", ...props }: ComponentProps<typeof DropdownMenuContent>) {\n  const { value, setValue, label } = useLanguage(\"LanguageSwitcherContent\");\n  return (\n    <DropdownMenuContent data-slot=\"language-switcher-content\" align={align} className={cn(\"min-w-48\", className)} {...props}>\n      <DropdownMenuRadioGroup value={value} onValueChange={setValue} aria-label={label}>\n        {children}\n      </DropdownMenuRadioGroup>\n    </DropdownMenuContent>\n  );\n}\n\nexport type LanguageSwitcherItemProps = Omit<ComponentProps<typeof DropdownMenuRadioItem>, \"children\"> & {\n  /** The language tag (\"en\", \"ar\", \"zh-Hant\"). */\n  value: string;\n  /** The language's name in itself, when the runtime's is missing or not the one wanted. */\n  name?: string;\n  /** Replaces the second line (the name in `displayLocale`). */\n  secondary?: ReactNode;\n};\n\n/**\n * One language. Its own name carries `lang` and `dir`, so it is pronounced and shaped as that language; the\n * second line, in the interface language, is for people looking for a language they cannot read. Typing a\n * letter jumps to a language by the name in the interface language when that line is shown (so \"J\" finds\n * 日本語), otherwise by its own name.\n */\nexport function LanguageSwitcherItem({ value, name, secondary, className, textValue, ...props }: LanguageSwitcherItemProps) {\n  const { displayLocale, names } = useLanguage(\"LanguageSwitcherItem\");\n  const own = name ?? names[value] ?? languageName(value);\n  const translated = secondary ?? (displayLocale && displayLocale !== value ? languageName(value, displayLocale) : null);\n  return (\n    <DropdownMenuRadioItem\n      data-slot=\"language-switcher-item\"\n      value={value}\n      textValue={textValue ?? (typeof translated === \"string\" ? translated : own)}\n      className={cn(\"min-h-10\", className)}\n      {...props}\n    >\n      <span className=\"flex min-w-0 flex-col text-start\">\n        {/* Sized to its words and placed by the menu's direction, so a right-to-left name still lines up with the rest. */}\n        <span lang={value} dir={directionOf(value)} className=\"max-w-full self-start truncate\">\n          {own}\n        </span>\n        {translated && translated !== own ? <span className=\"truncate text-xs text-muted-foreground\">{translated}</span> : null}\n      </span>\n    </DropdownMenuRadioItem>\n  );\n}\n"
    }
  ],
  "meta": {
    "category": "ui",
    "when": "A product in more than one language: in the header or footer, and in settings. It only reports the choice (`onValueChange`): the project navigates to the translated page or stores the preference. A flag stands for a country, not a language, so none is shown. It is a menu of radio items and not a select, because choosing a language is an action that reloads or navigates: a select that changes the page as its value changes surprises keyboard and screen-reader users. For choosing a language as data in a form (a document's language) use select.",
    "example": "<LanguageSwitcher value={locale} onValueChange={(next) => router.replace(pathname, { locale: next })} label=\"Language\" languages={[\"en\", \"fr\", \"ar\", \"ja\"]} />\n\n// With its own parts: names in the current language as a second line, and a name the project supplies\n<LanguageSwitcher defaultValue=\"en\" label=\"Language\" displayLocale=\"en\">\n  <LanguageSwitcherTrigger />\n  <LanguageSwitcherContent align=\"end\">\n    <LanguageSwitcherItem value=\"en\" />\n    <LanguageSwitcherItem value=\"ar\" />\n    <LanguageSwitcherItem value=\"dv\" name=\"ދިވެހި\" />\n  </LanguageSwitcherContent>\n</LanguageSwitcher>",
    "client": true
  }
}
