',
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/)。