GT ClassMethodsLocales
getLocaleProperties
GT getLocaleProperties 方法的 API 参考
概览
getLocaleProperties 方法用于检索指定语言代码的完整属性,提供
包括显示名称、区域代码、书写系统信息以及 emoji 国旗在内的详细信息。
它返回一个完整的 LocaleProperties 对象,涵盖构建功能完善的国际化用户界面所需的全部数据。
const gt = new GT({
  sourceLocale: 'en',
  targetLocale: 'es'
});
const props = gt.getLocaleProperties('fr-CA');
console.log(props.name); // "法语(加拿大)"
console.log(props.nativeName); // "français (Canada)"
console.log(props.emoji); // "🇨🇦"参考资料
参数
Prop
Type
参数说明
| 参数 | 说明 | 
|---|---|
| locale | 用于获取属性的 BCP-47 语言代码。若未提供,则使用该实例的 targetLocale | 
返回
LocaleProperties - 一个包含所有 locale 信息的完整对象:
- code: 标准化的语言代码
- name: 源语言环境中的显示名称
- nativeName: 该 locale 的本地显示名称
- languageCode,- languageName,- nativeLanguageName: 语言信息
- regionCode,- regionName,- nativeRegionName: 地区信息
- scriptCode,- scriptName,- nativeScriptName: 文字脚本信息
- maximizedCode,- minimizedCode: 规范化形式
- nameWithRegionCode,- nativeNameWithRegionCode: 组合显示格式
- emoji: 旗帜或其他代表性 emoji
抛出
- Error- 当未提供 locale 且该实例未配置- targetLocale时
示例
基本用法
const gt = new GT({
  sourceLocale: 'en',
  targetLocale: 'fr'
});
// 获取目标语言区域的属性
const props = gt.getLocaleProperties();
console.log(props.name); // "French (France)"
console.log(props.nativeName); // "français (France)"
console.log(props.languageCode); // "fr"
console.log(props.regionCode); // "FR"
console.log(props.emoji); // "🇫🇷"
// 获取其他语言区域的属性
const germanProps = gt.getLocaleProperties('de-AT');
console.log(germanProps.name); // "Austrian German"
console.log(germanProps.nativeName); // "Österreichisches Deutsch"
console.log(germanProps.regionName); // "Austria"
console.log(germanProps.nativeRegionName); // "Österreich"注意事项
- 所有显示名称均遵循该实例的源语言(sourceLocale)设置
- 自定义映射属性的优先级高于标准 Intl APIs
后续步骤
- 了解 LocaleProperties接口
- 使用 getLocaleName获取简洁的 locale 名称
- 使用 getLocaleEmoji获取 locale 的 emoji
- 了解 CustomMapping类型
这份指南怎么样?

