# General Translation Platform: getLocaleDirection
URL: https://generaltranslation.com/zh/docs/platform/core/reference/gt-class-methods/locales/get-locale-direction.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: 返回区域设置的文本方向。getLocaleDirection 的 API 参考。

General Translation 会为从左到右书写的语言返回 `'ltr'`，为从右到左书写的语言返回 `'rtl'`。

## 概览 [#overview]

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

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

const direction = gt.getLocaleDirection('ar');
console.log(direction); // "rtl"

const englishDirection = gt.getLocaleDirection('en-US');
console.log(englishDirection); // "ltr"
```

签名：

```typescript
getLocaleDirection(locale?: string): 'ltr' | 'rtl'
```

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

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

* **方向检测。** 对于从左到右书写的语言 (全球大多数语言) ，返回 `'ltr'`；对于从右到左书写的语言 (如阿拉伯语、希伯来语、波斯语、乌尔都语等) ，返回 `'rtl'`。
* **区域设置后备。** 省略 `locale` 时，会使用实例的 `targetLocale`。
* **缺少区域设置。** 如果未提供 `locale`，且实例也未配置 `targetLocale`，则会抛出 `Error`。

## 参数 [#parameters]

| 参数                  | 描述                       | 类型       | 可选 | 默认值                 |
| ------------------- | ------------------------ | -------- | -- | ------------------- |
| [`locale`](#locale) | 要检查其文本方向的 BCP-47 区域设置代码。 | `string` | 是  | `this.targetLocale` |

### `locale` [#locale]

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

用于检查文本方向的 BCP-47 区域设置代码。若未提供，则使用该实例的 `targetLocale`。

## 返回 [#returns]

**类型** `'ltr' | 'rtl'`

该区域设置的文本方向：

* `'ltr'`：从左到右 (大多数语言) 。
* `'rtl'`：从右到左 (阿拉伯语、希伯来语、波斯语、乌尔都语等) 。

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

## 示例 [#examples]

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

// 检查不同语言的书写方向
console.log(gt.getLocaleDirection()); // "rtl"（使用 targetLocale 'ar'）
console.log(gt.getLocaleDirection('en')); // "ltr"
console.log(gt.getLocaleDirection('ar')); // "rtl"
console.log(gt.getLocaleDirection('he')); // "rtl"
console.log(gt.getLocaleDirection('fa')); // "rtl"
console.log(gt.getLocaleDirection('fr')); // "ltr"
```

## 备注 [#notes]

* 对于所有从左到右书写的语言 (世界上大多数语言) ，都会返回 `'ltr'`。
* 对于从右到左书写的语言 (如阿拉伯语、希伯来语、波斯语等) ，会返回 `'rtl'`。

## Sitemap

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