# gt-react: General Translation React SDK: DictionaryTranslationOptions URL: https://generaltranslation.com/zh/docs/react/api/types/dictionary-translation-options.mdx --- title: DictionaryTranslationOptions description: DictionaryTranslationOptions 类型的 API 参考 --- {/* 自动生成:请勿直接编辑。请改为编辑 content/docs-templates/ 中的 template。 */} ## 概述 `DictionaryTranslationOptions` 类型用于向辞书条目传递变量,并指定其渲染方式。 它与 [`useTranslations`](/docs/react/api/dictionary/use-translations) 搭配使用,用于向辞书条目传递变量。 **构建时翻译:** `useTranslations` 的翻译在构建时进行;但变量本身永远不会被翻译。 相反,它们会以格式化后的形式插入译文中。 请务必遵循[此处的部署指南](/docs/react/tutorials/quickdeploy)。 ## 参考 ### 参数 ', optional: true, default: 'undefined', }, }} /> ### 说明 | Prop | 说明 | | ----------- | ---------------------------- | | `variables` | 一个对象,其键用于标识每个值映射到辞书条目中的哪个位置。 | *** ## 示例 ### 传递变量 要将变量传递给辞书条目,需要完成两件事: (1) 向条目中添加变量; (2) 在调用 [`d`](/docs/react/api/dictionary/use-translations) 时引用该变量。 首先,使用以下语法向辞书条目中添加变量:`{username}`。 `username` 是变量名。 ```jsx title="dictionary.ts" // [!code word:username] const dictionary = { greeting: { hello: 'Hello, {username}!', }, }; export default dictionary; ``` 接下来,引用这个变量: ```jsx title="Component.tsx" // [!code word:username] import { useTranslations } from 'gt-react'; const Component = () => { const t = useTranslations(); return
{t('greeting.hello', { username : 'Brian123' })}
; }; ``` ### 使用 ICU 消息格式 `gt-react` 支持 ICU 消息格式,因此你也可以对变量进行格式化。 ```jsx title="dictionary.ts" // [!code word:account-balance] const dictionary = { account: { balance: '您的账户余额:{dollars, number, ::currency/USD}!', }, }; export default dictionary; ``` 接下来,引用该变量: ```jsx title="Component.tsx" // [!code word:account-balance] import { useTranslations } from 'gt-react'; const Component = () => { const t = useTranslations(); return
{ t( 'account.balance', { "dollars" : 1000000, } ) }
; }; ``` *** ## 说明 * `variables` 对象用于向辞书条目传入值。 * `variablesOptions` 对象用于定义这些变量的行为。 ## 后续步骤 * 更多有关辞书和常见做法的信息,请参阅 [辞书](/docs/react/guides/dictionaries)。 * 更多有关辞书接口的信息,请参阅 [`useTranslations`](/docs/react/api/dictionary/use-translations)。 * 更多有关格式化选项的信息,请参阅 [`ICU 消息格式`](https://unicode-org.github.io/icu/userguide/format_parse/messages/)。