# General Translation React SDKs (gt-react, gt-next): loadDictionary URL: https://generaltranslation.com/zh/docs/react/reference/functions/load-dictionary.mdx --- title: loadDictionary description: 使用 General Translation gt-react 作为独立的 i18n 库加载你自己的词典翻译。loadDictionary 的 API 参考。 --- `loadDictionary` 是一个由你定义的加载器函数,用于加载某个区域设置的翻译词典。它适用于将 `gt-react` 作为独立的 i18n 库使用——也就是由你自行提供翻译,通常用于将现有 i18n 项目迁移到 General Translation。 *适用于 `gt-react`、`gt-next`、`gt-tanstack-start` 和 `gt-react-native`,但接入方式各不相同。`loadDictionary` 是你定义的加载器,并不是导出的符号。在 `gt-react` 和 `gt-tanstack-start` 中,需要将它传入初始化调用 (如下) ;`gt-next` 则通过 `withGTConfig` 插件来解析它 (详见 Next.js 部分的文档) 。本页中的加载器约定在各处都是相同的。* ## 概览 [#overview] 定义一个函数,接收区域设置并返回对应的 词典,然后将其作为 `loadDictionary` 选项传给 [`initializeGT`](/docs/react/reference/config#initialize)。单页应用则应将其传给 [`initializeGTSPA`](/docs/react/reference/config#initialize-spa)。 ```tsx const loadDictionary = (locale: string) => import(`../public/locales/${locale}.json`).then((m) => m.default); initializeGT({ ...gtConfig, loadDictionary }); ``` *注意:在 `gt-react` 中,`loadDictionary` 是初始化调用中的配置选项,而不是 [``](/docs/react/reference/components/gt-provider) 的 prop。`loadDictionary` 仅支持包含字符串翻译的 JSON 文件。* ## 工作方式 [#how-it-works] * **独立翻译。** `loadDictionary` 会为指定的区域设置加载一个翻译 JSON 文件。如果你使用自己的翻译,而不依赖 General Translation 的翻译基础设施,请使用它。 * **优先级。** 当存在多个翻译时,`loadDictionary` 加载的 词典 始终优先于 [`loadTranslations`](/docs/react/reference/functions/load-translations) 加载的翻译。 * **仅限 JSON。** 它只支持包含 字符串 翻译的 JSON 文件。 *这两个加载器的区别在于:[`loadTranslations`](/docs/react/reference/functions/load-translations) 用于自定义获取由 CLI 管理的机器生成翻译 (来自 CDN、数据库或 bundle) ,而 `loadDictionary` 则适用于将 `gt-react` 作为独立库使用的场景:使用你自己的翻译,而不依赖任何翻译基础设施。* ## 参数 [#parameters] | 参数 | 说明 | 类型 | 可选 | 默认值 | | ------------------- | -------------- | -------- | -- | --- | | [`locale`](#locale) | 要加载其翻译内容的区域设置。 | `string` | 否 | — | ### `locale` [#locale] **类型** `string` · **必需** 要加载其 词典 的区域设置。 ## 返回值 [#returns] **类型** `Promise` 一个 Promise,解析后会得到给定区域设置的 `Dictionary`,其中 id 映射到对应的翻译内容。 ## 示例 [#examples] ```js title="loadDictionary.js" export default async function loadDictionary(locale) { const translations = await import(`../public/locales/${locale}.json`); return translations.default; } ``` ## 说明 [#notes] * `loadDictionary` 会加载你自己的翻译以供独立使用,并且其优先级高于 [`loadTranslations`](/docs/react/reference/functions/load-translations)。 * 它仅支持包含字符串翻译的 JSON 文件。 * 在运行时使用 [`useTranslations`](/docs/react/reference/hooks/use-translations) 解析词典条目。