GT ClassMethodsLocales
getLocaleName
GT getLocaleName 方法的 API 参考
概览
getLocaleName 方法使用 Intl.DisplayNames API 获取语言代码的显示名称。
它会为任何有效的 BCP-47 语言代码返回可读的名称,并根据该实例的源语言环境进行本地化。
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(如已配置) - 库的默认语言环境(“en”)
自定义映射集成
- 优先检查自定义 locale 映射
- 如定义了自定义名称,则优先生效
- 对于标准 BCP-47 代码,回退到
Intl.DisplayNames
示例
const gt = new GT({
sourceLocale: 'en',
targetLocale: 'fr'
});
// 获取 targetLocale 的名称
console.log(gt.getLocaleName()); // "French (France)"
// 获取其他 locale 的名称
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 类型
本指南如何?