# General Translation Platform: resolveAliasLocale
URL: https://generaltranslation.com/zh/docs/platform/core/reference/gt-class-methods/locales/resolve-alias-locale.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: 将区域设置别名解析为自定义映射所使用的区域设置代码。resolveAliasLocale 的 API 参考。

在配置了自定义映射的情况下，此方法会在 [GT](/docs/platform/core/reference/gt-class/constructor) 实例上，返回你为规范区域设置公开的别名代码。这是 [`resolveCanonicalLocale`](/docs/platform/core/reference/gt-class-methods/locales/resolve-canonical-locale) 的逆操作。General Translation 使用别名，这样你就可以对外暴露自定义的区域设置代码，同时在内部将它们映射到标准的 BCP-47 代码。

## 概述 [#overview]

在 [`GT`](/docs/platform/core/reference/gt-class/constructor) 实例上，使用规范的区域设置代码调用 `resolveAliasLocale`。如果存在已配置的 别名，则返回该 别名；否则原样返回输入的区域设置。

```typescript
const gt = new GT({
  sourceLocale: 'en',
  customMapping: {
    cn: { code: 'zh', name: 'Mandarin' },
  },
});

const alias = gt.resolveAliasLocale('zh');
console.log(alias); // "cn"
```

签名：

```typescript
resolveAliasLocale(
  locale: string,
  customMapping?: CustomMapping
): string
```

*注意：`resolveAliasLocale` 在本地运行，无需 API 密钥。省略 `customMapping` 时，它会使用该实例的 `customMapping`。如需在不使用 `GT` 实例的情况下进行解析，请参阅独立使用的 [`resolveAliasLocale`](/docs/platform/core/reference/utility-functions/locales/resolve-alias-locale)。*

## 工作原理 [#how-it-works]

* **反向查找。** 在 [`CustomMapping`](/docs/platform/core/reference/types/custom-mapping) 中查找为标准区域设置配置的别名。
* **透传。** 当输入的区域设置本身已是别名，或不存在别名映射时，原样返回该区域设置。
* **映射回退。** 省略 `customMapping` 时，将使用实例的 `customMapping`。

## 参数 [#parameters]

| 参数                                 | 描述                    | 类型                                                                    | 可选 | 默认值                  |
| ---------------------------------- | --------------------- | --------------------------------------------------------------------- | -- | -------------------- |
| [`locale`](#locale)                | 要解析回其别名的规范区域设置代码。     | `string`                                                              | 否  | —                    |
| [`customMapping`](#custom-mapping) | 要使用的自定义映射，用于替代该实例的映射。 | [`CustomMapping`](/docs/platform/core/reference/types/custom-mapping) | 是  | `this.customMapping` |

### `locale` [#locale]

**类型** `string` · **必填**

用于解析回其别名的规范区域设置代码。

### `customMapping` [#custom-mapping]

**类型** [`CustomMapping`](/docs/platform/core/reference/types/custom-mapping) · **可选** · **默认值** `this.customMapping`

用于替代该实例映射的自定义映射。

## 返回值 [#returns]

**类型** `string`

返回规范区域设置已配置的别名；如果输入的区域设置本身已经是别名，或不存在别名映射，则原样返回该区域设置。

## 示例 [#examples]

```typescript
const gt = new GT({
  sourceLocale: 'en',
  customMapping: {
    cn: { code: 'zh', name: 'Mandarin' },
  },
});

// 将 canonical 区域设置解析回 别名
const alias = gt.resolveAliasLocale('zh');
console.log(alias); // "cn"

// 未映射的区域设置返回原始值
const unchanged = gt.resolveAliasLocale('es');
console.log(unchanged); // "es"
```

## 注意事项 [#notes]

* 存在自定义映射时，返回原始 别名 的区域设置代码。
* 如果未找到映射，则返回输入的区域设置，不作更改。

## Sitemap

See the full [sitemap](https://generaltranslation.com/sitemap.md) for all pages.
