LanguagePicker
LanguagePicker is a React Native field for choosing a language, such as the app's language setting or the language of a post. It shows each language with a flag, under its own name, such as 🇪🇸 Español or 🇯🇵 日本語, with its name in the user's language below, so people can find their language even when the app is in one they don't read. Search matches either name and the tag.
It's a SelectPicker with 86 languages built in, the ones the app stores and operating systems offer, so it takes the same props: a bottom sheet on phones, a dropdown on the web, multiple selection, a clear button and the iOS, Material or web design.
import { LanguagePicker } from '@elik56a/react-native-pickers';
<LanguagePicker label="App language" value={language} onChange={setLanguage} />
Try searching german, deutsch, 日本 or pt-br. The value is a BCP 47 tag, such as 'es', 'pt-BR' or 'zh-Hant', ready for your i18n library. Chinese and Portuguese come as their two written standards, which people choose between.
Try the props
presentationlocale<LanguagePicker
label="App language"
preferred={['en', 'es']}
value={value}
onChange={setValue}
locale="en"
/>
Flags
Each language shows the flag of the country it's most spoken in, such as 🇪🇸 for Español and 🇧🇷 for Português (Brasil). A language is spoken in more than one country, so change any flag with flags, such as 🇬🇧 for English in a British app, or hide them all with showFlag={false}. Flags are emoji: no images to add.
<LanguagePicker label="App language" flags={{ en: 'GB', ar: 'AE' }} value={language} onChange={setLanguage} />
The languages your app speaks
languages limits the list to your translations, and preferred pins some to the top under their own header, such as the device's languages.
<LanguagePicker label="App language" languages={TRANSLATED} preferred={['en', 'he']} preferredLabel="On this device" value={language} onChange={setLanguage} />
Pass languages, exclude and preferred from a constant, not a new array on every render, so the list is built once.
Right-to-left languages
isRtlLanguage(tag) tells whether a language is written right to left, for any BCP 47 tag, such as 'ar', 'he-IL' or 'fa_IR'. Use it to flip the layout when the user switches language:
import { I18nManager } from 'react-native';
import { isRtlLanguage } from '@elik56a/react-native-pickers';
const onChange = (language: LanguageCode) => {
i18n.changeLanguage(language);
I18nManager.forceRTL(isRtlLanguage(language)); // takes effect after a reload
};
Several languages
multiple turns the field into chips, as with SelectPicker, for the languages someone speaks or a post's translations.
<LanguagePicker label="Languages you speak" multiple value={spoken} onChange={setSpoken} />
Names in the user's language
The name under each language comes from Intl.DisplayNames in locale, the device locale by default, and the list is sorted by it. Where the JavaScript engine has no Intl.DisplayNames (Hermes), it falls back to English. The language's own name, on top, is always there.
<LanguagePicker label="Langue" placeholder="Choisir une langue" searchPlaceholder="Rechercher" locale="fr" value={language} onChange={setLanguage} />
Utilities
| Export | Description |
|---|---|
getLanguage(tag) | { code, name, nativeName, country, rtl } for a tag in any case, or undefined |
getAllLanguages() | Every Language, sorted by tag |
getLanguageName(language, locale?) | The language's name in locale, or its English name |
isRtlLanguage(tag) | Whether the language of any BCP 47 tag is written right to left |
getLanguage('he'); // { code: 'he', name: 'Hebrew', nativeName: 'עברית', country: 'IL', rtl: true }
Props
Every SelectPicker prop works, apart from the server search ones (options, loadOptions, onSearchChange, loading and the rest): the list is built in. value is a LanguageCode, or LanguageCode[] with multiple.
| Prop | Type | Default | Description |
|---|---|---|---|
languages | LanguageCode[] | all | Only these languages. |
exclude | LanguageCode[] | — | Leave these out. |
preferred | LanguageCode[] | — | Shown first, in this order, under their own header. |
showFlag | boolean | true | A flag before each language: the country it's most spoken in. |
flags | Partial<Record<LanguageCode, CountryCode>> | — | Other flags for some languages, such as { en: 'GB' }. |
preferredLabel | string | 'Suggested' | Header above preferred. |
allLanguagesLabel | string | 'All languages' | Header above the rest when preferred is set. |
locale | string | device locale | Language of the names under each language's own name. |
placeholder | string | 'Select a language' | Shown while nothing is selected. |
searchPlaceholder | string | 'Search languages' | Search field placeholder. |
sheet | SheetChromeProps | sheetProps.height: 0.8 | Sheet options. The sheet opens at 80% of the screen so it doesn't jump as search narrows the list. |
Types
type LanguageCode = 'af' | 'am' | 'ar' | … | 'pt-BR' | 'zh-Hans' | 'zh-Hant' | 'zu'; // BCP 47
interface Language {
code: LanguageCode;
name: string; // English
nativeName: string; // 'Español'
country: CountryCode; // the flag: 'ES'
rtl: boolean; // written right to left
}
Accessibility
Each row is named by the language's own name, such as "Deutsch", as the phone's language settings read it. Flags are hidden from screen readers. The field and the list have SelectPicker's accessibility.
Related
CountryPicker: countries with flags and dialing codesTimeZonePicker: time zones by city and offsetSelectPicker: any list of options