# Language switcher

Picks the interface language: each option named in the current language and sorted by that name, with an optional mark (a flag, a code chip) supplied by the app.

- Family: action
- Import: `import { LanguageSwitcher, LanguageLabel } from "@senvenli/react/action";`
- Page: https://design.senvenli.com/docs/components/language-switcher
- JSON: https://design.senvenli.com/docs/components/language-switcher.json

## Example: The product's languages

Each named in the current language and sorted by that name.

```tsx
import { LanguageSwitcher } from "@senvenli/react/action";
import { useState } from "react";

const LOCALES = ["en", "de", "fr", "es", "ja"] as const;

export default function Example() {
  // In an app, `onSelect` changes the route or saves the preference.
  const [locale, setLocale] = useState<(typeof LOCALES)[number]>("en");
  return <LanguageSwitcher locales={LOCALES} value={locale} onChange={setLocale} />;
}
```

## Examples

### With a mark

A renderMark function puts a code chip (or a flag) before each name.

```tsx
import { Badge } from "@senvenli/react/display";
import { LanguageSwitcher } from "@senvenli/react/action";
import { useState } from "react";

const LOCALES = ["en", "de", "fr", "es"] as const;

export default function Example() {
  const [locale, setLocale] = useState<(typeof LOCALES)[number]>("en");
  return <LanguageSwitcher locales={LOCALES} value={locale} onChange={setLocale} renderMark={(code) => <Badge>{code.toUpperCase()}</Badge>} />;
}
```

### Compact and collapsed

The small size for dense bars, and the mark alone for a collapsed rail.

```tsx
import { Badge } from "@senvenli/react/display";
import { Stack } from "@senvenli/react/layout";
import { LanguageSwitcher } from "@senvenli/react/action";
import { useState } from "react";

const LOCALES = ["en", "de", "fr"] as const;

export default function Example() {
  const [locale, setLocale] = useState<(typeof LOCALES)[number]>("en");
  const mark = (code: string) => <Badge>{code.toUpperCase()}</Badge>;
  return (
    <Stack direction="horizontal" space="space-200">
      <LanguageSwitcher size="small" locales={LOCALES} value={locale} onChange={setLocale} />
      <LanguageSwitcher size="small" isCollapsed label="Interface language" locales={LOCALES} value={locale} onChange={setLocale} renderMark={mark} />
    </Stack>
  );
}
```

## Guidelines

### When to use it

- The header or sidebar of a product offered in several languages.

### Use something else when

- [select](https://design.senvenli.com/docs/components/select.md): The language is one form field among others.
- [menu](https://design.senvenli.com/docs/components/menu.md): The language sits among other account actions.

## Accessibility

Keyboard:

- Arrow up / down: Moves between languages.
- Enter: Chooses the focused language.

Built in:

- A listbox with the current language selected and checked.
- Names never fall back to the host's language.
- A mark is decorative; the name always tells options apart.

You provide:

- The product's locales, and names for languages Intl names wrongly (registerLanguageNames).

## API reference

### LanguageSwitcher

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| locales | `readonly Locale[]` |  | yes | Locales to offer, already narrowed to what this product enables. |
| value | `Locale` |  | yes | The active UI locale. |
| onChange | `(locale: Locale) => void` |  | yes | Called with the picked locale (routing, persistence). |
| label | `string` |  | no | The trigger's accessible name and tooltip; "Language" (the DS string) by default. |
| isLoading | `boolean` | `false` | no | Hold the disabled trigger while the product's locale list resolves. |
| variant | `"button" \| "sidebar"` | `"button"` | no | `button` (default): a trigger in a bar. `sidebar`: a full-width row whose list opens upward, for a panel sidebar. |
| isCollapsed | `boolean` | `false` | no | A collapsed rail: the mark alone (or the name, without one), kept for screen readers either way. |
| renderMark | `(locale: Locale) => ReactNode` |  | no | A mark before each language's name: a flag, an `IsoChip`, an icon. None by default: a language is not a country, and the name alone tells the options apart. |
| size | `"small" \| "medium"` | `"medium"` | no | Control height: `medium` (default) matches the DS controls, `small` is the compact step. |
| aria-describedby | `string` |  | no | Ids of elements that describe the trigger ("Changes the language of the whole panel"). It lands on the trigger button, the focusable control, also while it is disabled for `isLoading`; left out, nothing is added. While the trigger's tooltip is open it takes the place of the tooltip's id, which only repeats `label`. |

### LanguageLabel

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| locale | `string` |  | yes |  |
| uiLocale | `string` |  | yes |  |
| hideName | `boolean` | `false` | no |  |
| mark | `React.ReactNode` |  | no |  |

### Tokens it reads

`--ds-background-brand-bold`, `--ds-background-brand-bold-hovered`, `--ds-background-brand-bold-pressed`, `--ds-background-danger`, `--ds-background-danger-bold`, `--ds-background-danger-bold-hovered`, `--ds-background-disabled`, `--ds-background-neutral`, `--ds-background-neutral-hovered`, `--ds-background-neutral-pressed`, `--ds-background-neutral-subtle-hovered`, `--ds-background-selected`, `--ds-background-selected-hovered`, `--ds-background-selected-pressed`, `--ds-border`, `--ds-border-bold`, `--ds-border-focused`, `--ds-border-selected`, `--ds-easing-standard`, `--ds-link`, `--ds-link-pressed`, `--ds-radius-control-medium`, `--ds-radius-large`, `--ds-shadow-overlay`, `--ds-size-control`, `--ds-size-control-compact`, `--ds-size-control-large`, `--ds-size-icon`, `--ds-space-050`, `--ds-space-075`, `--ds-space-100`, `--ds-space-150`, `--ds-space-200`, `--ds-surface-overlay`, `--ds-text`, `--ds-text-danger`, `--ds-text-disabled`, `--ds-text-on-brand`, `--ds-text-on-danger`, `--ds-text-subtle`

## Related

- [badge](https://design.senvenli.com/docs/components/badge.md)
