This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
This is documentation for the next SDK version. For up-to-date documentation, see the latest version (SDK 57).
Expo 本地化
一个为原生用户本地化信息提供接口的库。
expo-localization 允许您将应用本地化,为特定地区、语言或文化定制使用体验。它还提供对原生设备上区域设置数据的访问。将 lingui-js、react-i18next、react-intl、i18n-js 或 react-native-intlayer 等本地化库与 expo-localization 结合使用,可以帮助您为用户打造非常易于访问的体验。
安装
- npx expo install expo-localizationIf you are installing this in an existing React Native app, make sure to install expo in your project.
app config 中的配置
如果你在项目中使用 config plugins(持续原生生成 (CNG)),你可以使用 expo-localization 内置的 config plugin 进行配置。该插件允许你配置各种无法在运行时设置的属性,并且这些属性需要重新构建新的应用二进制文件后才会生效。如果你的应用不使用 CNG,那么你需要手动配置该库。
Example app.json with config plugin
{ "expo": { "plugins": [ [ "expo-localization", { "supportsRTL": true, "forcesRTL": false, "supportedLocales": ["en", "ja"] } ] ] } }
Configurable properties
| Name | Default | Description |
|---|---|---|
supportsRTL | true | 应用是否允许 RTL 布局。启用后,应用会根据 RTL 设备语言使用 RTL 布局,遵循 React Native 的 |
forcesRTL | false | 是否无论设备语言为何都强制使用 RTL 布局。适用于测试,或仅针对 RTL 区域设置本地化的应用。 |
supportedLocales | undefined | 应用支持的区域设置,用于在系统设置中启用按应用选择语言的功能。可以提供一个供两个平台共用的数组,也可以分别提供平台专属的 |
使用
有关使用 expo-localization 以及为从右到左语言添加支持的更多信息,请参阅 本地化 指南。
API
import { getLocales, getCalendars } from 'expo-localization';
行为
你可以使用同步的 getLocales() 和 getCalendars() 方法来获取用户设备的区域设置。在 iOS 上,只要应用正在运行,结果将保持不变。
在 Android 上,用户可以在“设置”中更改区域设置偏好,而无需重启应用。为了保持本地化内容的最新状态,你可以在应用每次返回前台时重新运行 getLocales() 和 getCalendars() 方法。使用 AppState 来检测这一点。
Hooks
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.
[Calendar, ...Calendar[]]Example
[{ "calendar": "gregory", "timeZone": "Europe/Warsaw", "uses24hourClock": true, "firstWeekday": 1 }]
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.
Example
[{ "languageTag": "pl-PL", "languageCode": "pl", "textDirection": "ltr", "digitGroupingSeparator": " ", "decimalSeparator": ",", "measurementSystem": "metric", "currencyCode": "PLN", "currencySymbol": "zł", "regionCode": "PL", "temperatureUnit": "celsius" }]
Methods
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.
[Calendar, ...Calendar[]]Example
[{ "calendar": "gregory", "timeZone": "Europe/Warsaw", "uses24hourClock": true, "firstWeekday": 1 }]
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.
Example
[{ "languageTag": "pl-PL", "languageCode": "pl", "textDirection": "ltr", "digitGroupingSeparator": " ", "decimalSeparator": ",", "measurementSystem": "metric", "currencyCode": "PLN", "currencySymbol": "zł", "regionCode": "PL", "temperatureUnit": "celsius" }]
Types
| Property | Type | Description |
|---|---|---|
| calendar | CalendarIdentifier | 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 |
| firstWeekday | Weekday | null | |
| timeZone | string | null | Time zone for the calendar. Can be Example
|
| uses24hourClock | boolean | null |
| Property | Type | Description |
|---|---|---|
| currencyCode | string | null | Currency code for the locale.
On iOS, it's the currency code from the Example
|
| currencySymbol | string | null | Currency symbol for the currency specified by Example
|
| decimalSeparator | string | null | Decimal separator used for formatting numbers with fractional parts. Example
|
| digitGroupingSeparator | string | null | Digit grouping separator used for formatting large numbers. Example
|
| languageCode | string | null | An IETF BCP 47 language tag without the region code. Example
|
| languageCurrencyCode | string | 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 Example
|
| languageCurrencySymbol | string | null | Currency symbol for the currency specified by Example
|
| languageRegionCode | string | null | The region code for the preferred language. When the language is not region-specific, it returns the same value as Example
|
| languageScriptCode | string | null | An ISO 15924 4-letter script code. On Android and Web, it may be Example
|
| languageTag | string | An IETF BCP 47 language tag with a region code. Example
|
| measurementSystem | 'metric' | 'us' | 'uk' | null | The measurement system used in the locale.
Is |
| regionCode | string | 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 Example
|
| temperatureUnit | 'celsius' | 'fahrenheit' | null | The temperature unit used in the locale.
Returns |
| textDirection | 'ltr' | 'rtl' | Text direction for the locale. One of: |
Enums
The calendar identifier, one of Unicode calendar types.
Gregorian calendar is aliased and can be referred to as both CalendarIdentifier.GREGORIAN and CalendarIdentifier.GREGORY.
CalendarIdentifier.ETHIOAA = "ethioaa"Ethiopic calendar, Amete Alem (epoch approx. 5493 B.C.E)
CalendarIdentifier.ETHIOPIC = "ethiopic"Ethiopic calendar, Amete Mihret (epoch approx, 8 C.E.)
CalendarIdentifier.ISLAMIC_CIVIL = "islamic-civil"Islamic calendar, tabular (intercalary years [2,5,7,10,13,16,18,21,24,26,29] - civil epoch)
CalendarIdentifier.ISLAMIC_TBLA = "islamic-tbla"Islamic calendar, tabular (intercalary years [2,5,7,10,13,16,18,21,24,26,29] - astronomical epoch)
CalendarIdentifier.ISLAMIC_UMALQURA = "islamic-umalqura"Islamic calendar, Umm al-Qura
CalendarIdentifier.ISO8601 = "iso8601"ISO calendar (Gregorian calendar using the ISO 8601 calendar week rules)
An enum mapping days of the week in Gregorian calendar to their index as returned by the firstWeekday property.