Utility FunctionsLocales
getLocaleProperties
独立函数 getLocaleProperties 的 API 参考
概述
独立的 getLocaleProperties 函数无需 GT 类实例即可检索指定语言代码的相关属性。
它会返回完整的 LocaleProperties 对象,包含显示名称、区域代码、书写体系信息以及旗帜 emoji 等详细信息。
import { getLocaleProperties } from 'generaltranslation';
const props = getLocaleProperties('fr-CA', 'en');
console.log(props.name); // "法语(加拿大)"
console.log(props.nativeName); // "français(加拿大)"
console.log(props.emoji); // "🇨🇦"
console.log(props.regionCode); // "CA"参考资料
参数
Prop
Type
参数说明
| 参数 | 说明 | 
|---|---|
| locale | 要获取其属性的 BCP-47 语言代码 | 
| defaultLocale | 用于本地化显示名称的 locale(默认为 'en') | 
| customMapping | 可选的语言代码及其属性的自定义映射 | 
返回值
LocaleProperties - 一个包含所有 locale 信息的完整对象:
- code: 标准化的语言代码
- name: 默认语言环境中的显示名称
- nativeName: 该语言环境的本地显示名称
- languageCode,- languageName,- nativeLanguageName: 语言信息
- regionCode,- regionName,- nativeRegionName: 地区信息
- scriptCode,- scriptName,- nativeScriptName: 文字脚本信息
- maximizedCode,- minimizedCode: 规范形式
- nameWithRegionCode,- nativeNameWithRegionCode: 合并显示格式
- emoji: 国旗或代表性 emoji
行为
自定义映射集成
- 对所有属性优先使用自定义映射
- 支持别名解析与属性覆盖
- 对未映射的代码回退至标准 Intl APIs
- 为使用别名的 locale 执行规范化解析
示例
import { getLocaleProperties } from 'generaltranslation';
// 英文显示名称
const enProps = getLocaleProperties('es-MX', 'en');
console.log(enProps.name); // "西班牙语(墨西哥)"
console.log(enProps.languageName); // "西班牙语"
console.log(enProps.regionName); // "墨西哥"
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"
// 原生名称始终使用目标 locale
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 类方法 getLocaleProperties
- 通过 getLocaleName获取简短的 locale 名称
- 通过 getLocaleEmoji获取 locale 的 emoji
这份指南怎么样?

