# gt-next: General Translation Next.js SDK: getTranslations
URL: https://generaltranslation.com/zh/docs/next/api/dictionary/get-translations.mdx
---
title: getTranslations
description: getTranslations 服务器端翻译函数的 API 参考
---
## 概览
`getTranslations` 用于从[翻译字典](/docs/next/guides/dictionaries)中获取服务器端组件所需的字符串翻译。
```jsx
const d = await getTranslations(); // 获取翻译函数
d('greeting.hello'); // 传入 id 以获取翻译
```
`getTranslations` 支持:
* 翻译 `string` 和 JSX 内容。
* 在翻译中插入变量并使用条件逻辑。
* 可选的 id 前缀。
如需客户端翻译,请参阅 [`useTranslations`](/docs/next/api/dictionary/use-translations)。
{d('greeting')} // Hello, Alice // [!code highlight]
); } ``` ### 使用变量 [#variables] 要传递值,必须 (1) 指定一个标识符,并且 (2) 在调用 `d` 函数时引用该标识符。 在这个示例中,我们使用 `{}` 向翻译传递变量。 在字典中,我们将标识符设为 `{userName}`。 ```jsx title="dictionary.jsx" copy // [!code word:userName] const dictionary = { greeting: "Hello, {userName}!", // [!code highlight] }; export default dictionary; ``` ```jsx title="TranslateGreeting.jsx" copy // [!code word:userName] import { getTranslations } from 'gt-next/server'; export default async function TranslateGreeting() { const d = await getTranslations(); // 你好,Alice! const greetingAlice = d('greeting', { userName: "Alice" }); // [!code highlight] return ({greetingAlice}
); } ``` ### 使用前缀 我们可以使用前缀,只获取字典中的一部分内容。 ```jsx title="dictionary.jsx" copy const dictionary = { prefix1: { // [!code highlight] prefix2: { // [!code highlight] greeting: "Hello, Bob", } } }; export default dictionary; ``` 由于我们在 `getTranslations` 方法中添加了值 `'prefix1.prefix2'`,因此所有键都会带上 `prefix1.prefix2` 前缀: ```jsx title="UserDetails.jsx" copy import { getTranslations } from 'gt-next/server'; export default function UserDetails() { const d = await getTranslations('prefix1.prefix2'); // [!code highlight] return ({d('greeting')}
// greeting => prefix1.prefix2.greeting // [!code highlight]