# generaltranslation: General Translation Core SDK: getLocaleName URL: https://generaltranslation.com/zh/docs/core/class/methods/locales/get-locale-name.mdx --- title: getLocaleName description: GT getLocaleName 方法的 API 参考文档 --- ## 概述 `getLocaleName` 方法使用 `Intl.DisplayNames` API 获取区域设置代码的显示名称。 它会为任何有效的 BCP-47 区域设置代码返回一个便于阅读的名称,并根据该实例的源区域设置进行本地化。 ```typescript const gt = new GT({ sourceLocale: 'en', targetLocale: 'es' }); const name = gt.getLocaleName('fr-CA'); console.log(name); // "French (Canada)" ``` *** ## 参考 ### 参数 ### 参数说明 | 参数 | 说明 | | -------- | -------------------------------------------------- | | `locale` | 要获取其显示名称的 BCP-47 区域设置代码。若未提供,则使用实例的 `targetLocale` | ### 返回值 `string` - 该区域设置的本地化显示名称。 ### 抛出 * `Error` - 如果未提供区域设置,且该实例也未配置 `targetLocale` *** ## 行为 ### 显示语言 显示名称会按以下规则进行本地化: 1. 实例的 `sourceLocale` (如果已配置) 2. 库的默认区域设置 (`en`) ### 自定义映射集成 * 优先检查自定义区域设置映射 * 如果定义了自定义名称,则以其为准 * 对标准 BCP-47 代码则回退到 `Intl.DisplayNames` *** ## 示例 ```typescript 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`](/docs/core/class/methods/locales/get-locale-emoji) 获取区域设置对应的表情符号 * 通过 [`getLocaleProperties`](/docs/core/class/methods/locales/get-locale-properties) 获取完整的区域设置信息 * [了解 CustomMapping 类型](/docs/core/types/custom-mapping)