One Logo Pool Ball
wwwwwwwwwwwwwwwwwww

Localization

Read preferred locales and calendars, and follow system changes with React hooks

One.Localization reads the user’s language and formatting preferences. getLocales() and getCalendars() are synchronous snapshots. The named useLocales and useCalendars hooks read the same values and update when the platform reports a locale or time zone change. No permission is required.

Usage

import { Text } from 'react-native'
import { One, useCalendars, useLocales } from 'one'
const locales = One.Localization.getLocales()
const calendars = One.Localization.getCalendars()
function LocalTime({ when }: { when: Date }) {
const [locale] = useLocales()
const [calendar] = useCalendars()
const label = new Intl.DateTimeFormat(locale.languageTag, {
...(calendar.timeZone ? { timeZone: calendar.timeZone } : {}),
hour: 'numeric',
minute: '2-digit',
}).format(when)
return <Text>{label}</Text>
}

Both reads always return at least one entry, so the first entry can be destructured directly. Hooks are imports from one; they are not methods on the namespace. Your app supplies translations; this API supplies the preferences used to choose and format them.

API reference

One.LocalizationTypeDescription
getLocales() => [Locale, ...Locale[]]Read preferred locales in priority order, with at least one entry.
() => [Locale, ...Locale[]]Read preferred locales in priority order, with at least one entry.
getCalendars() => [Calendar, ...Calendar[]]Read calendar preferences, with at least one entry.
() => [Calendar, ...Calendar[]]Read calendar preferences, with at least one entry.
HookReturns
useLocales()[Locale, ...Locale[]], refreshed on platform changes
useCalendars()[Calendar, ...Calendar[]], refreshed on platform changes
LocaleTypeDescription
languageTagstringBCP 47 language tag, such as en-US or zh-Hant-TW.
stringBCP 47 language tag, such as en-US or zh-Hant-TW.
languageCodestring | nullLanguage subtag, such as en or zh.
string | nullLanguage subtag, such as en or zh.
languageScriptCodestring | nullExplicit script subtag, such as Hant; null when absent.
string | nullExplicit script subtag, such as Hant; null when absent.
regionCodestring | nullFormatting region selected by the device.
string | nullFormatting region selected by the device.
languageRegionCodestring | nullRegion associated with this preferred language.
string | nullRegion associated with this preferred language.
currencyCodestring | nullCurrency code for the formatting region.
string | nullCurrency code for the formatting region.
currencySymbolstring | nullCurrency symbol for the formatting region.
string | nullCurrency symbol for the formatting region.
languageCurrencyCodestring | nullCurrency code associated with this language locale.
string | nullCurrency code associated with this language locale.
languageCurrencySymbolstring | nullCurrency symbol associated with this language locale.
string | nullCurrency symbol associated with this language locale.
decimalSeparatorstring | nullSeparator used between whole and fractional digits.
string | nullSeparator used between whole and fractional digits.
digitGroupingSeparatorstring | nullSeparator used between groups of digits.
string | nullSeparator used between groups of digits.
textDirection'ltr' | 'rtl'Writing direction for the language.
'ltr' | 'rtl'Writing direction for the language.
measurementSystem'metric' | 'us' | 'uk' | nullMeasurement system selected by the device.
'metric' | 'us' | 'uk' | nullMeasurement system selected by the device.
temperatureUnit'celsius' | 'fahrenheit' | nullTemperature unit selected by the device.
'celsius' | 'fahrenheit' | nullTemperature unit selected by the device.
CalendarTypeDescription
calendarstring | nullUnicode calendar type, such as gregory or buddhist.
string | nullUnicode calendar type, such as gregory or buddhist.
timeZonestring | nullSystem time zone identifier, such as Pacific/Honolulu.
string | nullSystem time zone identifier, such as Pacific/Honolulu.
uses24hourClockboolean | nullWhether the device uses a 24-hour clock.
boolean | nullWhether the device uses a 24-hour clock.
firstWeekdaynumber | nullFirst day of the week, with Sunday 1 and Saturday 7.
number | nullFirst day of the week, with Sunday 1 and Saturday 7.

Unknown optional values are null. Locale preferences can differ from language preferences: on iOS, number separators, currency, measurement, and temperature follow the device’s formatting settings even when its first preferred language names a different region.

Platform differences

iOSAndroidWeb
ReadsSystem locale and calendarSystem locale and calendarIntl and navigator.languages
Hook updatesLocale and time zone notificationsLocale and time zone broadcastsBrowser languagechange event
Currency and unitsSystem preferencesLocale-derived valuesNull
Clock and first weekdaySystem preferencesSystem preferencesNull when the browser does not expose them

System settings may restart an app when its language changes. While the app is running, hooks subscribe to the platform’s change notifications and remove their listeners on unmount. Snapshot reads do not subscribe. The web hooks listen for languagechange; they do not independently watch time zone changes. During server rendering the locale falls back to en-US; the server’s Intl implementation supplies calendar data when available.

Migration from Expo

Replace Expo Localization’s getLocales() and getCalendars() with One.Localization.getLocales() and One.Localization.getCalendars(). Import useLocales and useCalendars from one; the hook names and the Locale and Calendar field names stay the same.

For the older combined asynchronous snapshot, see One.Device.getLocalizationInfo. Use Localization when you need the preferred locale list or reactive formatting preferences.

Edit this page on GitHub.