# react-native: InlineTranslationOptions URL: https://generaltranslation.com/zh/docs/react-native/api/types/inline-translation-options.mdx --- title: InlineTranslationOptions description: InlineTranslationOptions 类型的 API 参考 --- {/* 自动生成:请勿直接编辑。请改为编辑 content/docs-templates/ 中的模板。 */} ## 概述 `InlineTranslationOptions` 类型用于向内联翻译传递变量,并指定其渲染行为。 你还可以为翻译添加上下文和标识符。 它与 [`useGT`](/docs/react-native/api/strings/use-gt) 和 [`msg`](/docs/react-native/api/strings/msg) 配合使用,用于向内联字符串翻译传递变量。 **构建时翻译:** `useGT` 和 `msg` 的翻译发生在构建时;但变量本身不会被翻译。 相反,它们会以格式化后的形式插入译文中。 请务必遵循[此处的部署指南](/docs/react-native/tutorials/quickdeploy)。 ## 参考 ### 参数 ', 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/),了解格式化选项的更多信息。