',
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/),了解有关格式化选项的更多信息。