# 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)。 `getTranslations` 和 `useTranslations` 使用[字典](/docs/next/guides/dictionaries)存储所有待翻译内容。 这与使用 [`` 组件](/docs/next/guides/t)进行翻译不同。 如果你只打算使用 `` 组件进行翻译,那么本文档与你无关。 ## 参考文档 ### 属性 ### 说明 | 属性 | 说明 | | ---- | ------------------------------ | | `id` | 用于给所有翻译键统一添加的可选前缀。处理嵌套字典值时很有用。 | ### 返回值 一个解析为翻译函数 `d` 的 Promise;给定 `id` 后,该函数会返回对应条目的翻译结果 ```jsx Promise<(id: string, options?: DictionaryTranslationOptions) => string> ``` | 名称 | 类型 | 说明 | | ---------- | ------------------------------------------------------------------------------------- | ------------------ | | `id` | `string` | 待翻译条目的 `id` | | `options?` | [`DictionaryTranslationOptions`](/docs/next/api/types/dictionary-translation-options) | 用于自定义 `d` 行为的翻译选项。 | *** ## 示例 ### 字典基本用法 字典中的每一项都会被翻译。 ```jsx title="dictionary.jsx" copy const dictionary = { greeting: <>Hello, Alice!, // [!code highlight] }; export default dictionary; ``` 当我们需要在服务器端访问这些条目时,可以调用 `getTranslations`。 它会返回一个函数,该函数接收字典中的翻译键。 ```jsx title="TranslateGreeting.jsx" copy import { getTranslations } from 'gt-next/server'; export default async function TranslateGreeting() { const d = await getTranslations(); // [!code highlight] return (

{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]
); } ``` *** ## 注意事项 * `getTranslations` 函数允许你在服务器端访问字典中的翻译内容。 ## 后续步骤 * 请参阅 [`useTranslations`](/docs/next/api/dictionary/use-translations),它是 `getTranslations` 的客户端对应版本。