This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
Expo 本地化
一个为原生用户本地化信息提供接口的库。
expo-localization 可用于本地化应用,为特定地区、语言或文化定制体验。它还可以访问原生设备上的区域设置数据。将 expo-localization 与本地化库(例如 lingui-js、react-i18next、react-intl、i18n-js 或 react-native-intlayer)配合使用,可以为用户打造出色的无障碍体验。
安装
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
Configurable properties
使用
如需详细了解如何使用 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
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.