# react-native: DictionaryTranslationOptions URL: https://generaltranslation.com/zh/docs/react-native/api/types/dictionary-translation-options.mdx --- title: DictionaryTranslationOptions description: DictionaryTranslationOptions 类型的 API 参考 --- {/* 自动生成:请勿直接编辑。请改为编辑 content/docs-templates/ 中的 template。 */} ## 概述 `DictionaryTranslationOptions` 类型用于向字典条目传递变量,并指定其渲染方式。 它与 [`useTranslations`](/docs/react-native/api/dictionary/use-translations) 搭配使用,用于向字典条目传递变量。 **构建时翻译:** `useTranslations` 的翻译在构建时进行;不过,变量本身不会被翻译。 相反,它们会以格式化后的形式插入译文中。 请务必遵循[此处的部署指南](/docs/react-native/tutorials/quickdeploy)。 ## 参考 ### 参数 ', optional: true, default: 'undefined', }, }} /> ### 说明 | 属性 | 说明 | | ----------- | ------------------------------ | | `variables` | 一个对象,其中的各个键用于标识每个值映射到字典条目中的位置。 | *** ## 示例 ### 传递变量 要向字典条目传递变量,需要完成两件事:(1) 在条目中添加一个变量;(2) 在 [`d`](/docs/react-native/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-native'; const Component = () => { const t = useTranslations(); return
{t('greeting.hello', { username : 'Brian123' })}
; }; ``` ### 使用 ICU 消息格式 `gt-react-native` 支持 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-native'; const Component = () => { const t = useTranslations(); return
{ t( 'account.balance', { "dollars" : 1000000, } ) }
; }; ``` *** ## 说明 * `variables` 对象用于向字典条目传递值。 * `variablesOptions` 对象用于定义变量的行为。 ## 后续步骤 * 参阅 [字典](/docs/react-native/guides/dictionaries),了解有关字典及常见用法的更多信息。 * 参阅 [`useTranslations`](/docs/react-native/api/dictionary/use-translations),了解有关字典接口的更多信息。 * 参阅 [`ICU 消息格式`](https://unicode-org.github.io/icu/userguide/format_parse/messages/),了解有关格式化选项的更多信息。