# General Translation React SDKs (gt-react, gt-next, gt-react-native): getTranslations
URL: https://generaltranslation.com/zh/docs/react/tanstack-start/reference/functions/get-translations.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: 在 TanStack Start Runtime 中使用 General Translation 读取字典翻译。getTranslations 的 API 参考。

`getTranslations` 函数会为当前服务器请求或浏览器返回一个字典查找函数。从 `gt-tanstack-start` 导入它。

## 概览 [#overview]

```ts
import { getTranslations } from 'gt-tanstack-start';

const t = await getTranslations('account');
const heading = t('heading');
```

```ts
getTranslations(rootId?: string): Promise<DictionaryTranslationFunction>
```

## 参数 [#parameters]

| 参数       | 描述                | 类型       | 可选 | 默认 |
| -------- | ----------------- | -------- | -- | -- |
| `rootId` | 添加到每个字典 id 前面的前缀。 | `string` | 是  | —  |

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

在服务器端，`getTranslations` 会读取由 [`gtMiddleware`](/docs/react/tanstack-start/reference/functions/gt-middleware) 创建的区域设置和国际化配置。在浏览器中，它会读取由 [`initializeGT`](/docs/react/reference/config#initialize) 初始化的条件存储。可选的 `rootId` 会将所有查找限定在同一字典路径下。

在未激活的 middleware 请求作用域之外进行服务器调用时会抛出异常。查找未知的 字典 id 也会抛出异常。

## 返回值 [#returns]

**类型** `Promise<(id: string, options?: DictionaryTranslationOptions) => string>`

解析后的函数接受 [`DictionaryTranslationOptions`](/docs/react/reference/types/dictionary-translation-options)，并提供用于处理嵌套字典对象的 `.obj(id)`。

## 示例 [#example]

```ts
import { createServerFn } from '@tanstack/react-start';
import { getTranslations } from 'gt-tanstack-start';

export const loadHeading = createServerFn({ method: 'GET' }).handler(
  async () => {
    const t = await getTranslations('account');
    return t('heading');
  }
);
```

## Sitemap

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