# General Translation Platform: getLocaleEmoji
URL: https://generaltranslation.com/zh/docs/platform/core/reference/gt-class-methods/locales/get-locale-emoji.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: 为区域设置或地区返回一个 emoji 标记。getLocaleEmoji 的 API 参考。

根据区域设置代码对应的地区，在 [GT](/docs/platform/core/reference/gt-class/constructor) 实例上获取相应的 emoji 旗帜或符号。General Translation 会返回国家和地区的旗帜 emoji；对于没有特定地区的语言，则提供后备内容，并支持通过映射自定义 emoji。

## 概览 [#overview]

在 [`GT`](/docs/platform/core/reference/gt-class/constructor) 实例上调用 `getLocaleEmoji` 时，可以选择传入区域设置代码。若省略，则默认使用该实例的 `targetLocale`。

```typescript
const gt = new GT({ sourceLocale: 'en', targetLocale: 'fr-CA' });

const emoji = gt.getLocaleEmoji('fr-CA');
console.log(emoji); // "🇨🇦" (加拿大国旗)

const usEmoji = gt.getLocaleEmoji('en-US');
console.log(usEmoji); // "🇺🇸" (美国国旗)

const enEmoji = gt.getLocaleEmoji('en');
console.log(enEmoji); // "🇺🇸" (美国国旗)
```

签名：

```typescript
getLocaleEmoji(locale?: string): string
```

*注意：`getLocaleEmoji` 在本地运行，无需 API 密钥。省略 `locale` 时，它会使用该实例的 `targetLocale`。如果要在没有 `GT` 实例的情况下进行查找，请参阅独立的 [`getLocaleEmoji`](/docs/platform/core/reference/utility-functions/locales/get-locale-emoji)。*

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

* **基于区域的选择。** 使用区域设置中的区域代码 (如果有) 来选择旗帜 emoji，该 emoji 由 Unicode 区域指示符号组合而成。
* **自定义映射优先。** 在 [`CustomMapping`](/docs/platform/core/reference/types/custom-mapping) 中定义的 emoji 会优先于基于区域的选择。
* **后备内容。** 对于某些不带区域的语言，会返回对应语言的 emoji；对于无法识别或无效的区域设置，则返回默认的地球 emoji `🌍`。
* **未提供区域设置。** 如果未提供区域设置，且实例未配置 `targetLocale`，则会抛出 `Error`。

## 参数 [#parameters]

| 参数                  | 描述                            | 类型       | 可选 | 默认值                 |
| ------------------- | ----------------------------- | -------- | -- | ------------------- |
| [`locale`](#locale) | 要获取其对应 emoji 的 BCP-47 区域设置代码。 | `string` | 是  | `this.targetLocale` |

### `locale` [#locale]

**类型** `string` · **可选** · **默认值** `this.targetLocale`

用于获取对应 emoji 的 BCP-47 区域设置代码。若未提供，则使用该实例的 `targetLocale`。

## 返回值 [#returns]

**类型** `string`

表示该区域设置的旗帜 emoji 或其他符号：

* 对于带地区的区域设置，返回国家/地区旗帜 emoji (例如 `🇺🇸`、`🇫🇷`、`🇯🇵`) 。
* 对于某些不带地区的语言，返回特定语言的 emoji。
* 对于无法识别的区域设置，返回默认的地球 emoji (`🌍`) 。

如果未提供区域设置，且该实例也未配置 `targetLocale`，则会抛出 `Error`。

## 示例 [#examples]

```typescript
const gt = new GT({ sourceLocale: 'en', targetLocale: 'es' });

// 获取目标区域设置的 emoji
console.log(gt.getLocaleEmoji()); // "🇪🇸" (uses targetLocale 'es')

// 获取不同区域设置的 emoji
console.log(gt.getLocaleEmoji('en-US')); // "🇺🇸"
console.log(gt.getLocaleEmoji('fr-FR')); // "🇫🇷"
console.log(gt.getLocaleEmoji('de-DE')); // "🇩🇪"
console.log(gt.getLocaleEmoji('ja-JP')); // "🇯🇵"
console.log(gt.getLocaleEmoji('zh-CN')); // "🇨🇳"
```

## 备注 [#notes]

* 在可用的情况下，会根据区域设置中的区域代码返回对应的旗帜 emoji。
* 自定义映射的 emoji 优先于基于区域的选择结果。
* 使用 Unicode 区域指示符号生成旗帜。
* 对于无法识别或无效的区域设置，默认返回 `🌍` (地球仪) 。
* 兼容所有支持 Unicode emoji 的现代浏览器和操作系统。

## Sitemap

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