# gt-next: General Translation Next.js SDK: getLocaleDirection URL: https://generaltranslation.com/zh/docs/next/api/helpers/get-locale-direction.mdx --- title: getLocaleDirection description: getLocaleDirection 服务器端方法的 API 参考 --- ## 概述 `getLocaleDirection` 函数用于在服务器端渲染期间获取当前或指定区域设置的文本方向 (`'ltr'` 或 `'rtl'`) 。 `getLocaleDirection` 是一个服务器端方法,只能在服务器组件中使用。 如需在客户端使用,请参阅 [`useLocaleDirection`](/docs/next/api/helpers/use-locale-direction)。 ## 参考 ### 参数 | 参数 | 类型 | 说明 | | -------- | -------------- | ------------------------------------------------------- | | `locale` | `string` (可选) | BCP 47 区域设置代码 (例如 `'ar'`、`'en-US'`) 。如果省略,则使用当前用户的区域设置。 | ### 返回值 * 如果提供了 `locale`:`'ltr' | 'rtl'` — 指定区域设置的文本方向。 * 如果省略了 `locale`:`Promise<'ltr' | 'rtl'>` — 一个 Promise,解析结果为当前区域设置的文本方向。 *** ## 示例 ### 获取当前区域设置的文本方向 ```jsx title="DirectionWrapper.jsx" copy import { getLocaleDirection } from 'gt-next/server'; export default async function DirectionWrapper({ children }) { const dir = await getLocaleDirection(); // [!code highlight] return
{children}
; } ``` ### 获取特定区域设置的文本方向 ```jsx title="SpecificDirection.jsx" copy import { getLocaleDirection } from 'gt-next/server'; export default function SpecificDirection() { const dir = getLocaleDirection('ar'); // 'rtl' — 无需 await [!code highlight] return

阿拉伯语文本方向:{dir}

; } ``` *** ## 说明 * 不传参数调用时,`getLocaleDirection` 是异步的,必须使用 `await` 等待。 * 传入区域设置字符串调用时,它会同步返回结果。 * 可用于给 `` 或布局元素设置 `dir` 属性,以支持 RTL。 ## 后续步骤 * 可用于为元素设置 `dir` 属性,以支持 RTL。 - 客户端对应的版本请参见 [`useLocaleDirection`](/docs/next/api/helpers/use-locale-direction)。