react-native-localize
@amazon-devices/react-native-localize是一个库,用于在适用于Vega的React Native (RNK) 应用中使用Localize React Native。该库提供本地化和国际化支持,让您可以根据不同的语言、地区和文化调整应用。
安装
要使用该程序包,请在应用的package.json文件中添加JavaScript库依赖项,如下所示。
"dependencies": {
...
"@amazon-devices/react-native-localize": "~2.0.0"
}
示例
以下是如何使用@amazon-devices/react-native-localize的示例。
import { getCurrencies, getLocales } from "@amazon-devices/react-native-localize";
console.log(getLocales());
console.log(getCurrencies());
API参考
getLocales()
按顺序返回用户的首选区域设置。
方法类型
type getLocales = () => Array<{
languageCode: string;
scriptCode?: string;
countryCode: string;
languageTag: string;
isRTL: boolean;
}>;
示例
import { getLocales } from "@amazon-devices/react-native-localize";
console.log(getLocales());
/* -> [
{ countryCode: "GB", languageTag: "en-GB", languageCode: "en", isRTL: false },
{ countryCode: "US", languageTag: "en-US", languageCode: "en", isRTL: false },
{ countryCode: "FR", languageTag: "fr-FR", languageCode: "fr", isRTL: false },
] */
getNumberFormatSettings()
返回数字格式化设置。
方法类型
type getNumberFormatSettings = () => {
decimalSeparator: string;
groupingSeparator: string;
};
示例
import { getNumberFormatSettings } from "@amazon-devices/react-native-localize";
console.log(getNumberFormatSettings());
/* -> {
decimalSeparator: ".",
groupingSeparator: ",",
} */
getCurrencies()
按顺序返回用户的首选货币代码。
方法类型
type getCurrencies = () => string[];
示例
import { getCurrencies } from "@amazon-devices/react-native-localize";
console.log(getCurrencies());
// -> ["EUR", "GBP", "USD"]
getCountry()
根据用户的设备区域设置而不是位置返回用户当前的国家/地区代码。
方法类型
type getCountry = () => string;
示例
import { getCountry } from "@amazon-devices/react-native-localize";
console.log(getCountry());
// -> "FR"
拉丁美洲地区
使用拉丁美洲区域设置的设备将返回“UN”而不是“419”,因为“419”不是标准的国家/地区代码。
getCalendar()
返回用户的首选日历格式。
方法类型
type getCalendar = () =>
| "gregorian"
| "buddhist"
| "coptic"
| "ethiopic"
| "ethiopic-amete-alem"
| "hebrew"
| "indian"
| "islamic"
| "islamic-umm-al-qura"
| "islamic-civil"
| "islamic-tabular"
| "iso8601"
| "japanese"
| "persian";
示例
import { getCalendar } from "@amazon-devices/react-native-localize";
console.log(getCalendar());
// -> "gregorian"
getTemperatureUnit()
返回用户的首选温度单位。
方法类型
type getTemperatureUnit = () => "celsius" | "fahrenheit";
示例
import { getTemperatureUnit } from "@amazon-devices/react-native-localize";
console.log(getTemperatureUnit());
// -> "celsius"
getTimeZone()
根据用户的设备设置而不是位置返回用户的首选时区。
方法类型
type getTimeZone = () => string;
示例
import { getTimeZone } from "@amazon-devices/react-native-localize";
console.log(getTimeZone());
// -> "Asia/Yerevan"
uses24HourClock()
如果用户偏好24小时的时钟格式,则返回true;如果用户偏好12小时的时钟格式,则返回false。
方法类型
type uses24HourClock = () => boolean;
示例
import { uses24HourClock } from "@amazon-devices/react-native-localize";
console.log(uses24HourClock());
// -> true
usesMetricSystem()
如果用户偏好公制度量系统,则返回true;如果用户偏好英制度量系统,则返回false。
方法类型
type usesMetricSystem = () => boolean;
示例
import { usesMetricSystem } from "@amazon-devices/react-native-localize";
console.log(usesMetricSystem());
// -> true
usesAutoDateAndTime()
告知设备是否启用自动日期和时间设置。
方法类型
type usesAutoDateAndTime = () => boolean | undefined;
示例
import { usesAutoDateAndTime } from "@amazon-devices/react-native-localize";
console.log(usesAutoDateAndTime());
// -> false/true
usesAutoTimeZone()
告知设备是否启用自动时区设置。
方法类型
type usesAutoTimeZone = () => boolean | undefined;
示例
import { usesAutoTimeZone } from "@amazon-devices/react-native-localize";
console.log(usesAutoTimeZone());
// -> false
支持的版本
| 程序包名称 | 亚马逊NPM库版本 | Vega OS内部版本号 | Vega SDK版本 | 发行说明 |
|---|---|---|---|---|
@amazon-devices/react-native-localize |
2.0.1+3.0.4 | OS 1.1 (201010438050) |
0.20 |
相关主题
Last updated: 2025年9月30日

