Utility FunctionsLocales
getLocaleProperties
独立函数 getLocaleProperties 的 API 参考
概览
独立的 getLocaleProperties 函数可在无需 GT class 实例的情况下,根据语言代码检索其属性。
它会返回完整的 LocaleProperties 对象,包含显示名称、地区代码、书写系统信息以及 emoji 旗帜等详细信息。
import { getLocaleProperties } from 'generaltranslation';
const props = getLocaleProperties('fr-CA', 'en');
console.log(props.name); // "French (Canada)"
console.log(props.nativeName); // "français (Canada)"
console.log(props.emoji); // "🇨🇦"
console.log(props.regionCode); // "CA"参考资料
参数
Prop
Type
参数说明
| 参数 | 说明 |
|---|---|
locale | 要获取属性所使用的 BCP-47 语言代码 |
defaultLocale | 用于本地化显示名称的 locale(默认为“en”) |
customMapping | 可选的 locale 代码与属性的 customMapping 映射 |
返回值
LocaleProperties - 一个包含所有语言环境信息的完整对象:
code: 标准化的语言代码name: 默认语言环境中的显示名称nativeName: 语言环境自身的显示名称languageCode,languageName,nativeLanguageName: 语言信息regionCode,regionName,nativeRegionName: 地区信息scriptCode,scriptName,nativeScriptName: 文字脚本信息maximizedCode,minimizedCode: 标准化形式nameWithRegionCode,nativeNameWithRegionCode: 组合显示格式emoji: 国旗或代表性表情符号
行为表现
自定义映射集成
- 所有属性优先使用自定义映射进行检查
- 支持别名解析与属性覆盖
- 对未映射代码回退到标准 Intl APIs
- 为带别名的 locale 执行规范化的语言环境解析
示例
import { getLocaleProperties } from 'generaltranslation';
// 英文显示名称
const enProps = getLocaleProperties('es-MX', 'en');
console.log(enProps.name); // "Spanish (Mexico)"
console.log(enProps.languageName); // "Spanish"
console.log(enProps.regionName); // "Mexico"
console.log(enProps.emoji); // "🇲🇽"
// 法文显示名称
const frProps = getLocaleProperties('es-MX', 'fr');
console.log(frProps.name); // "espagnol (Mexique)"
console.log(frProps.languageName); // "espagnol"
console.log(frProps.regionName); // "Mexique"
// 本地名称始终使用目标语言区域
console.log(enProps.nativeName); // "español (México)"
console.log(frProps.nativeName); // "español (México)"备注
- 该函数无需实例化 GT class 即可提供 locale 数据
- 自定义映射属性优先于标准 Intl APIs
- 始终返回完整的
LocaleProperties接口 - 原生名称一律以目标 locale 自身进行计算
后续步骤
- 查看
LocaleProperties接口——完整接口文档 - 使用 GT class 方法
getLocaleProperties - 通过
getLocaleName获取简洁的 locale 名称 - 通过
getLocaleEmoji获取 locale 的 emoji
本指南如何?