GT ClassMethodsLocales
getLocaleName
GT getLocaleName 方法的 API 参考
概述
getLocaleName 方法使用 Intl.DisplayNames API 获取 locale 代码的显示名称。
它会根据该实例的源 locale,将任何有效的 BCP-47 locale 代码返回为可读的本地化名称。
const gt = new GT({
  sourceLocale: 'en',
  targetLocale: 'es'
});
const name = gt.getLocaleName('fr-CA');
console.log(name); // "法语(加拿大)"参考资料
参数
Prop
Type
参数说明
| 参数 | 说明 | 
|---|---|
| locale | 用于获取显示名称的 BCP-47 语言代码;若未提供,则使用该实例的 targetLocale。 | 
返回
string - 该 locale 的本地化显示名称。
抛出
- Error- 当未提供 locale 且该实例未配置- targetLocale时
行为
显示语言
显示名称将按以下顺序进行本地化:
- 实例的 sourceLocale(如已配置)
- 库的默认 locale('en')
自定义映射集成
- 优先检查自定义 locale 映射
- 如果定义了自定义名称,则以其为准
- 对于标准 BCP-47 代码,回退使用 Intl.DisplayNames
示例
const gt = new GT({
  sourceLocale: 'en',
  targetLocale: 'fr'
});
// 获取目标语言区域的名称
console.log(gt.getLocaleName()); // "French (France)"
// 获取其他语言区域的名称
console.log(gt.getLocaleName('es')); // "Spanish (Spain)"
console.log(gt.getLocaleName('de')); // "German (Germany)"
console.log(gt.getLocaleName('ja')); // "Japanese (Japan)"注意事项
- 该方法使用实例的 sourceLocale来确定显示语言
- 自定义映射名称优先于标准的 Intl.DisplayNames
下一步
- 使用 getLocaleEmoji获取 locale 的 emoji
- 使用 getLocaleProperties获取完整的 locale 信息
- 了解 CustomMapping 类型
这份指南怎么样?

