',
optional: true,
default: 'undefined',
},
}}
/>
### 说明
| Prop | 说明 |
| ----------- | --------------------------------------------------------------- |
| `variables` | 一个对象,其中的键用于标识每个值映射到字符串中的位置。 |
| `$context` | 可选在 `variables` 对象中包含 `$context` 变量,为内容提供上下文 (用于翻译) 。 |
| `$id` | 可选在 `variables` 对象中包含 `$id` 变量,为翻译编辑器提供一个标识符。 |
| `$maxChars` | 可选包含 `$maxChars` 变量,以限制翻译的字符数。该库会通过 `formatCutoff()` 逻辑严格执行此限制。 |
***
## 示例
### 上下文
要为字符串添加上下文,请使用 `$context` prop。
```jsx title="Component.tsx"
// [!code word:$context]
import { useGT } from 'gt-react-native';
const Component = () => {
const gt = useGT();
return {gt('Hello, world!', { $context: '正式问候语' })}
;
};
```
### 传递变量
要在字符串中添加变量,请使用 `{variable-name}` 语法,即用花括号括住变量名。
```jsx title="Component.tsx"
// [!code word:username]
import { useGT } from 'gt-react-native';
const Component = () => {
const gt = useGT();
return {gt('Hello, {username}! How is your day?', { username: 'Brian123' })}
;
};
```
### 使用 ICU 消息格式
`gt-react-native` 支持 ICU 消息格式,因此你也可以对变量进行格式化。
```jsx title="Component.tsx"
// [!code word:account-balance]
import { useGT } from 'gt-react-native';
const Component = () => {
const gt = useGT();
return
{ gt(
'Your account balance: {dollars, number, ::currency/USD}!',
{
"dollars" : 1000000,
}
) }
;
};
```
有关 ICU 消息格式的更多信息,请参阅 [ICU 消息格式文档](https://unicode-org.github.io/icu/userguide/format_parse/messages/)。
### 字符限制
使用 `$maxChars` 可限制译文长度:
```jsx title="Component.tsx"
// [!code word:$maxChars]
import { useGT } from 'gt-react-native';
const Component = () => {
const gt = useGT();
return {gt('Welcome to our application', { $maxChars: 15 })}
;
// Output: "Bienvenue à no\u202F…"
};
```
***
## 说明
* `InlineTranslationOptions` 用于内联字符串翻译。
* `variables` 对象用于向文本传入值。
* `variablesOptions` 对象用于定义变量的行为。
## 后续步骤
* 参阅 [`useGT`](/docs/react-native/api/strings/use-gt),了解内联字符串翻译的更多信息。
* 参阅 [`ICU 消息格式`](https://unicode-org.github.io/icu/userguide/format_parse/messages/),了解格式化选项的更多信息。