LocaleSwitcher

Molecule

A language picker for public pages, drawn with the same DOM as kui-react. Options are links when they have an `href` (a native `<details>` menu, so it works without JavaScript) or buttons when they do not. Names are shown in their own language with `lang` and `dir` set, the current locale is marked in text, and a locale whose page falls back to another language says so. A small idempotent script (`modules/ui/LocaleSwitcher/scripts/locale-switcher.js`) closes the menu after a choice, on Escape (focus returns to the trigger) and on an outside press, and moves between options with the arrow keys, Home and End. React's `onChange` is the bubbling, cancelable DOM event `kui:locale-change` { code, option, originalEvent }: call `preventDefault()` to handle navigation yourself. Not the flag dropdown of the common domain: no flag assets and no i18n dependency.

Menu of links

Preview
Code
<%- include('modules/ui/LocaleSwitcher', { locales: locales, current: 'en' }) %>

Inline buttons

Preview

Pick a language.

Code
<%- include('modules/ui/LocaleSwitcher', { variant: 'inline', locales: localesWithoutHref, current: 'en' }) %>
<script>
  document.addEventListener('kui:locale-change', function (e) {
    // e.detail = { code: 'tr', option: { code, name, ... }, originalEvent }
    // e.preventDefault() stops a link from navigating
  });
</script>

Right to left

Preview
Code
<div dir="rtl"><%- include('modules/ui/LocaleSwitcher', { locales: locales, current: 'ar' }) %></div>
Source modules/ui/LocaleSwitcher.ejs