# gt-next: General Translation Next.js SDK: 字符串 URL: https://generaltranslation.com/zh/docs/next/guides/strings.mdx --- title: 字符串 description: 如何使用 useGT 实现纯文本字符串的国际化 --- {/* 自动生成:请勿直接编辑。请改为编辑 content/docs-templates/ 中的 template。 */} 字符串翻译可让你在不使用 JSX 的情况下直接获取文本翻译,非常适合用于属性、对象属性和纯文本值。在 Next.js 组件中使用 [`useGT`](/docs/next/api/strings/use-gt) 进行字符串翻译。 ## 快速开始 ```jsx import { useGT } from 'gt-next'; 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/next/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 消息格式。 ## 示例 ### 表单输入项 ```jsx import { useGT } from 'gt-next'; function ContactForm() { const gt = useGT(); return (