# react-native: 字符串 URL: https://generaltranslation.com/zh/docs/react-native/guides/strings.mdx --- title: 字符串 description: 如何使用 useGT 国际化纯文本字符串 --- {/* 自动生成:请勿直接编辑。请改为编辑 content/docs-templates/ 中的模板。 */} 字符串翻译让你无需使用 JSX 即可直接处理文本翻译,非常适合用于属性、对象属性和纯文本值。在 React Native 组件中,使用 [`useGT`](/docs/react-native/api/strings/use-gt) 进行字符串翻译。 ## 快速开始 ```jsx import { useGT } from 'gt-react-native'; function MyComponent() { const gt = useGT(); return ( ); } ``` ## 何时使用字符串翻译 当你需要纯文本而不是 JSX 时,字符串翻译更合适: ### HTML 属性 ```jsx const gt = useGT(); ``` ### 对象属性 ```jsx const gt = useGT(); const user = { name: 'John', role: 'admin', bio: gt('Experienced software developer with 5 years in React'), status: gt('Currently available for projects') }; ``` ### 配置和常量 ```jsx const gt = useGT(); const navigationItems = [ { label: gt('Home'), href: '/' }, { label: gt('Products'), href: '/products' }, { label: gt('Contact'), href: '/contact' } ]; ``` ### 何时改用 T 对于 JSX 内容,请使用 [`` 组件](/docs/react-native/api/components/t): ```jsx // ✅ 对 JSX 内容使用

Welcome to our store!

// ✅ 对纯文本使用字符串翻译 ``` ## 使用变量 ### 基本变量 将占位符替换为动态值: ```jsx const gt = useGT(); const itemCount = 5; // 带占位符的字符串 const message = gt('You have {count} items in your cart', { count: itemCount }); // 结果:"You have 5 items in your cart" ``` ### 多个变量 ```jsx const gt = useGT(); const order = { id: 'ORD-123', total: 99.99, date: '2024-01-15' }; const confirmation = gt( 'Order {orderId} for ${total} was placed on {date}', { orderId: order.id, total: order.total, date: order.date } ); ``` ### ICU 消息格式 如需进行高级格式化,请使用 ICU 语法: ```jsx const gt = useGT(); translate('There are {count, plural, =0 {no items} =1 {one item} other {{count} items}} in the cart', { count: 10 }); ``` 在 [Unicode 文档](https://unicode-org.github.io/icu/userguide/format_parse/messages/)中详细了解 ICU Message Format。 ## 示例 ### 表单输入项 ```jsx import { useGT } from 'gt-react-native'; function ContactForm() { const gt = useGT(); return (