Reference version

This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

Expo 本地化 iconExpo 本地化

一个为原生用户本地化信息提供接口的库。

Android
iOS
tvOS
Web
Included in Expo Go
Recommended version:
~58.0.0

expo-localization 可用于本地化应用,为特定地区、语言或文化定制体验。它还可以访问原生设备上的区域设置数据。将 expo-localization 与本地化库(例如 lingui-js、react-i18next、react-intl、i18n-js 或 react-native-intlayer)配合使用,可以为用户打造出色的无障碍体验。

安装

Terminal
- npx expo install expo-localization
- yarn expo install expo-localization
- pnpm expo install expo-localization
- bun expo install expo-localization

If you are installing this in an existing React Native app, make sure to install expo in your project.

在应用配置中配置

如果项目中使用了配置插件(Continuous Native Generation(CNG)),可以使用内置的配置插件来配置 expo-localization。该插件允许你配置各种无法在运行时设置的属性,这些属性需要重新构建应用二进制文件才能生效。如果应用未使用 CNG,则需要手动配置此库。

Example app.json with config plugin

app.json
{ "expo": { "plugins": [ [ "expo-localization", { "supportsRTL": true, "forcesRTL": false, "supportedLocales": ["en", "ja"] } ] ] } }

Configurable properties

NameDefaultDescription
supportsRTLtrue

Whether the app allows RTL layout. When enabled, the app renders in RTL for RTL device languages, following React Native's I18nManager.

forcesRTLfalse

Whether to force RTL layout regardless of the device language. Useful for testing or for apps localized only for RTL locales.

supportedLocalesundefined

The locales your app supports, used to enable per-app language selection in the system settings. Provide a single array shared by both platforms, or platform-specific ios and android arrays.

使用

如需详细了解如何使用 expo-localization 以及如何添加对从右向左书写语言的支持,请参阅本地化指南。

API

import { getLocales, getCalendars } from 'expo-localization';

行为

你可以使用同步方法 getLocales() 和 getCalendars() 获取用户设备的区域设置。在 iOS 上,应用运行期间,返回结果将保持不变。

在 Android 上,用户可以在不重启应用的情况下,通过“设置”更改区域设置偏好。若要确保本地化信息保持最新,可以在应用每次返回前台时重新运行 getLocales() 和 getCalendars() 方法。使用 AppState 检测应用状态。

Hooks

useCalendars()

Android
iOS
tvOS
Web

A hook providing a list of user's preferred calendars, returned as an array of objects of type Calendar. Guaranteed to contain at least 1 element. For now always returns a single element, but it's likely to return a user preference list on some platforms in the future. If the OS settings change, the hook will rerender with a new list of calendars.

Returns:
[Calendar, ...Calendar[]]

Example

[{ "calendar": "gregory", "timeZone": "Europe/Warsaw", "uses24hourClock": true, "firstWeekday": 1 }]

useLocales()

Android
iOS
tvOS
Web

A hook providing a list of user's locales, returned as an array of objects of type Locale. Guaranteed to contain at least 1 element. These are returned in the order the user defines in their device settings. On the web currency and measurements systems are not provided, instead returned as null. If needed, you can infer them from the current region using a lookup table. If the OS settings change, the hook will rerender with a new list of locales.

Returns:
[Locale, ...Locale[]]

Example

[{ "languageTag": "pl-PL", "languageCode": "pl", "textDirection": "ltr", "digitGroupingSeparator": " ", "decimalSeparator": ",", "measurementSystem": "metric", "currencyCode": "PLN", "currencySymbol": "zł", "regionCode": "PL", "temperatureUnit": "celsius" }]

Methods

Localization.getCalendars()

Android
iOS
tvOS
Web

List of user's preferred calendars, returned as an array of objects of type Calendar. Guaranteed to contain at least 1 element. For now always returns a single element, but it's likely to return a user preference list on some platforms in the future.

Returns:
[Calendar, ...Calendar[]]

Example

[{ "calendar": "gregory", "timeZone": "Europe/Warsaw", "uses24hourClock": true, "firstWeekday": 1 }]

Localization.getLocales()

Android
iOS
tvOS
Web

List of user's locales, returned as an array of objects of type Locale. Guaranteed to contain at least 1 element. These are returned in the order the user defines in their device settings. On the web currency and measurements systems are not provided, instead returned as null. If needed, you can infer them from the current region using a lookup table.

Returns:
[Locale, ...Locale[]]

Example

[{ "languageTag": "pl-PL", "languageCode": "pl", "textDirection": "ltr", "digitGroupingSeparator": " ", "decimalSeparator": ",", "measurementSystem": "metric", "currencyCode": "PLN", "currencySymbol": "zł", "regionCode": "PL", "temperatureUnit": "celsius" }]

Types

Calendar

Android
iOS
tvOS
Web
PropertyTypeDescription
calendarCalendarIdentifier | null

The calendar identifier, one of Unicode calendar types.

On Android is limited to one of device's available calendar types.

On iOS uses calendar identifiers, but maps them to the corresponding Unicode types, will also never contain 'dangi' or 'islamic-rgsa' due to it not being implemented on iOS.

firstWeekdayWeekday | null

The first day of the week. For most calendars Sunday is numbered 1, with Saturday being number 7. Can be null on some browsers that don't support the weekInfo property in Intl API.

Example

1, 7.

timeZonestring | null

Time zone for the calendar. Can be null on Web.

Example

'America/Los_Angeles', 'Europe/Warsaw', 'GMT+1'.

uses24hourClockboolean | null

True when current device settings use 24-hour time format. Can be null on some browsers that don't support the hourCycle property in Intl API.

Locale

Android
iOS
tvOS
Web
PropertyTypeDescription
currencyCodestring | null

Currency code for the locale. On iOS, it's the currency code from the Region setting under Language & Region, not for the current locale. On Android, it's the currency specific to the locale in the list, as there are no separate settings for selecting a region. Is null on Web, use a table lookup based on region instead.

Example

'USD', 'EUR', 'PLN'.

currencySymbolstring | null

Currency symbol for the currency specified by currencyCode.

Example

'$', '€', 'zł'.

decimalSeparatorstring | null

Decimal separator used for formatting numbers with fractional parts.

Example

'.', ','.

digitGroupingSeparatorstring | null

Digit grouping separator used for formatting large numbers.

Example

'.', ','.

languageCodestring | null

An IETF BCP 47 language tag without the region code.

Example

'en', 'es', 'pl'.

languageCurrencyCodestring | null

Currency code for the locale. On iOS, it's the currency code for the current locale in the list, not the device region. On Android, it's equal to currencyCode. Is null on Web. Prefer using currencyCode for any internalization purposes.

Example

'USD', 'EUR', 'PLN'.

languageCurrencySymbolstring | null

Currency symbol for the currency specified by languageCurrencyCode. Prefer using currencySymbol for any internalization purposes.

Example

'$', '€', 'zł'.

languageRegionCodestring | null

The region code for the preferred language. When the language is not region-specific, it returns the same value as regionCode. When the language is region-specific, it returns the region code for the language (en-CA -> CA). Prefer using regionCode for any internalization purposes.

Example

'US'.

languageScriptCodestring | null

An ISO 15924 4-letter script code. On Android and Web, it may be null if none is defined.

Example

'Latn', 'Hans', 'Hebr'.

languageTagstring

An IETF BCP 47 language tag with a region code.

Example

'en-US', 'es-419', 'pl-PL'.

measurementSystem'metric' | 'us' | 'uk' | null

The measurement system used in the locale. Is null on Web, as user chosen measurement system is not exposed on the web and using locale to determine measurement systems is unreliable. Ask for user preferences if possible.

regionCodestring | null

The region code for your device that comes from the Region setting under Language & Region on iOS, Region settings on Android and is parsed from locale on Web (can be null on Web).

Example

'US'.

temperatureUnit'celsius' | 'fahrenheit' | null

The temperature unit used in the locale. Returns null if the region code is unknown.

textDirection'ltr' | 'rtl'

Text direction for the locale. One of: 'ltr', 'rtl'.

Enums

CalendarIdentifier

Android
iOS
tvOS
Web

The calendar identifier, one of Unicode calendar types. Gregorian calendar is aliased and can be referred to as both CalendarIdentifier.GREGORIAN and CalendarIdentifier.GREGORY.

BUDDHIST

CalendarIdentifier.BUDDHIST = "buddhist"

Thai Buddhist calendar

CHINESE

CalendarIdentifier.CHINESE = "chinese"

Traditional Chinese calendar

COPTIC

CalendarIdentifier.COPTIC = "coptic"

Coptic calendar

DANGI

CalendarIdentifier.DANGI = "dangi"

Traditional Korean calendar

ETHIOAA

CalendarIdentifier.ETHIOAA = "ethioaa"

Ethiopic calendar, Amete Alem (epoch approx. 5493 B.C.E)

ETHIOPIC

CalendarIdentifier.ETHIOPIC = "ethiopic"

Ethiopic calendar, Amete Mihret (epoch approx, 8 C.E.)

GREGORIAN

CalendarIdentifier.GREGORIAN = "gregory"

Gregorian calendar (alias)

GREGORY

CalendarIdentifier.GREGORY = "gregory"

Gregorian calendar

HEBREW

CalendarIdentifier.HEBREW = "hebrew"

Traditional Hebrew calendar

INDIAN

CalendarIdentifier.INDIAN = "indian"

Indian calendar

ISLAMIC

CalendarIdentifier.ISLAMIC = "islamic"

Islamic calendar

ISLAMIC_CIVIL

CalendarIdentifier.ISLAMIC_CIVIL = "islamic-civil"

Islamic calendar, tabular (intercalary years [2,5,7,10,13,16,18,21,24,26,29] - civil epoch)

ISLAMIC_RGSA

CalendarIdentifier.ISLAMIC_RGSA = "islamic-rgsa"

Islamic calendar, Saudi Arabia sighting

ISLAMIC_TBLA

CalendarIdentifier.ISLAMIC_TBLA = "islamic-tbla"

Islamic calendar, tabular (intercalary years [2,5,7,10,13,16,18,21,24,26,29] - astronomical epoch)

ISLAMIC_UMALQURA

CalendarIdentifier.ISLAMIC_UMALQURA = "islamic-umalqura"

Islamic calendar, Umm al-Qura

ISO8601

CalendarIdentifier.ISO8601 = "iso8601"

ISO calendar (Gregorian calendar using the ISO 8601 calendar week rules)

JAPANESE

CalendarIdentifier.JAPANESE = "japanese"

Japanese imperial calendar

PERSIAN

CalendarIdentifier.PERSIAN = "persian"

Persian calendar

ROC

CalendarIdentifier.ROC = "roc"

Civil (algorithmic) Arabic calendar

Weekday

Android
iOS
tvOS
Web

An enum mapping days of the week in Gregorian calendar to their index as returned by the firstWeekday property.

SUNDAY

Weekday.SUNDAY = 1

MONDAY

Weekday.MONDAY = 2

TUESDAY

Weekday.TUESDAY = 3

WEDNESDAY

Weekday.WEDNESDAY = 4

THURSDAY

Weekday.THURSDAY = 5

FRIDAY

Weekday.FRIDAY = 6

SATURDAY

Weekday.SATURDAY = 7