# General Translation Platform: resolveAliasLocale URL: https://generaltranslation.com/zh/docs/platform/core/reference/utility-functions/locales/resolve-alias-locale.mdx --- title: resolveAliasLocale description: 在没有 GT 实例的情况下,将区域设置别名解析为自定义映射使用的区域设置代码。resolveAliasLocale 的 API 参考。 --- [`resolveAliasLocale`](/docs/platform/core/reference/gt-class-methods/locales/resolve-alias-locale) 是 General Translation 的 Core 库提供的一个独立工具函数,用于在配置了自定义映射时,将规范区域设置代码转换回其别名区域设置代码。 ## 概览 [#overview] 直接从 `generaltranslation` 导入 `resolveAliasLocale`,然后传入区域设置代码和自定义映射进行调用。它不需要 API 密钥,也不需要 [GT](/docs/platform/core/reference/gt-class/constructor) 实例。若要使用对应的实例方法,请改用 [`GT`](/docs/platform/core/reference/gt-class/constructor) 实例上的 [`resolveAliasLocale`](/docs/platform/core/reference/gt-class-methods/locales/resolve-alias-locale) 方法。 ```typescript import { resolveAliasLocale } from 'generaltranslation'; const customMapping = { 'simplified-chinese': { code: 'zh-CN', name: 'Simplified Chinese' }, 'traditional-chinese': { code: 'zh-TW', name: 'Traditional Chinese' }, }; console.log(resolveAliasLocale('zh-CN', customMapping)); // 'simplified-chinese' ``` 签名: ```typescript resolveAliasLocale(locale: string, customMapping?: CustomMapping): string ``` ## 工作原理 [#how-it-works] * **反向查找。** 将规范区域设置代码转换回 [`customMapping`](/docs/platform/core/reference/types/custom-mapping) 中定义的面向用户的别名。 * **回退。** 如果映射中没有匹配的别名,则返回原始区域设置。 * **无状态。** 不会产生副作用。 ## 参数 [#parameters] | 参数 | 描述 | 类型 | 可选 | 默认值 | | ---------------------------------- | ---------------- | --------------------------------------------------------------------- | -- | --- | | [`locale`](#locale) | 别名要解析到的规范区域设置代码。 | `string` | 否 | — | | [`customMapping`](#custom-mapping) | 用于解析该别名的自定义映射。 | [`CustomMapping`](/docs/platform/core/reference/types/custom-mapping) | 是 | — | ### `locale` [#locale] **类型** `string` · **必填** 要解析到的别名所对应的规范区域设置代码。 ### `customMapping` [#custom-mapping] **类型** [`CustomMapping`](/docs/platform/core/reference/types/custom-mapping) · **可选** 用于解析别名的自定义映射。若未提供映射,则原样返回输入的区域设置。 ## 返回值 [#returns] **类型** `string` 若存在映射,则返回对应的别名区域设置代码;否则返回原始区域设置。 ## 示例 [#examples] ```typescript import { resolveAliasLocale } from 'generaltranslation'; const customMapping = { 'simplified-chinese': { code: 'zh-CN', name: 'Simplified Chinese' }, 'traditional-chinese': { code: 'zh-TW', name: 'Traditional Chinese' }, }; // 将 canonical 解析为 alias console.log(resolveAliasLocale('zh-CN', customMapping)); // 'simplified-chinese' console.log(resolveAliasLocale('zh-TW', customMapping)); // 'traditional-chinese' // 未映射的区域设置返回原始值 console.log(resolveAliasLocale('es-ES', customMapping)); // 'es-ES' ``` ## 注意事项 [#notes] * 将规范的区域设置代码转换回更便于用户理解的别名。 * 如果不存在映射,则返回原始区域设置。 * 对显示面向用户的区域设置名称至关重要。 * 支持自定义区域设置映射。 * 这是一个无状态函数,不会产生副作用。