# General Translation Platform: getLocaleDirection URL: https://generaltranslation.com/zh/docs/platform/core/reference/utility-functions/locales/get-locale-direction.mdx --- title: getLocaleDirection description: 无需 GT 实例即可返回某个区域设置的文本方向。getLocaleDirection 的 API 参考。 --- [`getLocaleDirection`](/docs/platform/core/reference/gt-class-methods/locales/get-locale-direction) 是 General Translation 核心库中的一个独立工具函数,用于返回某个区域设置的文本方向 (从左到右或从右到左) 。它最常用于根据区域设置设置 HTML 元素的 `dir` 属性。 ## 概览 [#overview] 直接从 `generaltranslation` 导入 `getLocaleDirection`,并传入区域设置代码进行调用。它既不需要 API Key,也不需要 [GT](/docs/platform/core/reference/gt-class/constructor) 实例。若要使用基于实例的等效方法,请改用 [`GT`](/docs/platform/core/reference/gt-class/constructor) 实例上的 [`getLocaleDirection`](/docs/platform/core/reference/gt-class-methods/locales/get-locale-direction) 方法。 ```typescript import { getLocaleDirection } from 'generaltranslation'; const direction = getLocaleDirection('ar-SA'); console.log(direction); // "rtl" const englishDirection = getLocaleDirection('en-US'); console.log(englishDirection); // "ltr" ``` 签名: ```typescript getLocaleDirection(locale: string): 'ltr' | 'rtl' ``` ## 工作原理 [#how-it-works] 该函数使用 `Intl.Locale` API 的 `textInfo.direction` 属性: 1. 为给定的区域设置创建一个 `Intl.Locale` 对象。 2. 读取 `textInfo.direction` 属性,获取该语言对应的书写方向。 3. 从右到左的语言返回 `'rtl'`,其他语言一律返回 `'ltr'`。 4. 如果区域设置无效或引发错误,则默认返回 `'ltr'`。 ### RTL 语言识别 自动检测从右到左书写的语言,包括: * **阿拉伯语** (`ar`, `ar-SA`, `ar-EG`, `ar-AE` 等) * **希伯来语** (`he`, `he-IL`) * **波斯语/法尔西语** (`fa`, `fa-IR`) * **乌尔都语** (`ur`, `ur-PK`, `ur-IN`) * **普什图语** (`ps`) * **信德语** (`sd`) * **索拉尼库尔德语** (`ckb`) * 以及其他 RTL 书写系统。 ### 错误处理 * 无效或格式不正确的区域设置代码将默认返回 `'ltr'`。 * 对于无效输入,不会抛出任何异常。 ## 参数 [#parameters] | 参数 | 描述 | Type | 可选 | 默认值 | | ------------------- | ------------------------ | -------- | -- | --- | | [`locale`](#locale) | 要检查其文本方向的 BCP-47 区域设置代码。 | `string` | 否 | — | ### `locale` [#locale] **类型** `string` · **必填** 用于检查文本方向的 BCP-47 区域设置代码。 ## 返回值 [#returns] **类型** `'ltr' | 'rtl'` 该区域设置的文本方向: * `'ltr'`:从左到右 (大多数语言,包括英语、西班牙语、法语、德语、中文和日语) 。 * `'rtl'`:从右到左 (阿拉伯语、希伯来语、波斯语、乌尔都语,以及其他闪米特语系/中东语言) 。 ## 示例 [#examples] ```typescript import { getLocaleDirection } from 'generaltranslation'; // 从左到右的语言 console.log(getLocaleDirection('en-US')); // "ltr" console.log(getLocaleDirection('es-ES')); // "ltr" console.log(getLocaleDirection('fr-FR')); // "ltr" console.log(getLocaleDirection('ja-JP')); // "ltr" console.log(getLocaleDirection('zh-CN')); // "ltr" // 从右到左的语言 console.log(getLocaleDirection('ar-SA')); // "rtl" console.log(getLocaleDirection('he-IL')); // "rtl" console.log(getLocaleDirection('fa-IR')); // "rtl" console.log(getLocaleDirection('ur-PK')); // "rtl" ``` ## 说明 [#notes] * 对于所有从左到右书写的语言 (世界上大多数语言) ,返回 `'ltr'`。 * 对于从右到左书写的语言 (如阿拉伯语、希伯来语、波斯语等) ,返回 `'rtl'`。 * 使用现代 `Intl.Locale` API 进行精确检测。 * 兼容所有 BCP-47 区域设置代码。