TacoTranslate
/
文档定价
 
5月04日

React 应用国际化(i18n)的最佳解决方案

您是否想将您的 React 应用扩展到新的市场?TacoTranslate 让本地化 React 应用变得非常简单,帮助您轻松触达全球用户,无需费心。

为什么为 React 选择 TacoTranslate?

  • 无缝集成:专为 React 应用设计,TacoTranslate 可无缝融入您现有的工作流程。
  • 自动字符串收集:无需再手动管理 JSON 文件。TacoTranslate 会自动从代码库中收集字符串。
  • AI 驱动的翻译:利用 AI 的力量提供符合上下文且契合应用语气的准确翻译。
  • 即时语言支持:只需一键即可添加对新语言的支持,让您的应用面向全球用户。

工作原理

通过 npm 安装 TacoTranslate 包:

npm install tacotranslate

安装模块后,您需要创建一个 TacoTranslate 帐户、一个翻译项目以及相关的 API 密钥。在此创建帐户。 这是免费的,且无需提供信用卡信息。

在 TacoTranslate 应用的界面中,创建一个项目,然后转到其 API 密钥 选项卡。创建一个 read 密钥和一个 read/write 密钥。我们会把它们保存为环境变量。 read 密钥就是我们所称的 public,而 read/write 密钥是 secret。例如,你可以将它们添加到项目根目录的 .env 文件中。

您还需要添加另外两个环境变量:TACOTRANSLATE_DEFAULT_LOCALETACOTRANSLATE_ORIGIN.

  • TACOTRANSLATE_DEFAULT_LOCALE: 默认的回退区域设置代码。在此示例中,我们将其设置为 en 表示英语。
  • TACOTRANSLATE_ORIGIN: 存放字符串的“文件夹”,例如您网站的 URL。在此阅读有关 origins 的更多信息。
.env
TACOTRANSLATE_PUBLIC_API_KEY=123456
TACOTRANSLATE_SECRET_API_KEY=789010
TACOTRANSLATE_DEFAULT_LOCALE=en
TACOTRANSLATE_ORIGIN=your-website-url.com

请务必不要在客户端的生产环境中泄露秘密 read/write API 密钥。

设置 TacoTranslate

在你的 React 应用中初始化 TacoTranslate:通过将你的应用包裹在 TacoTranslate 上下文提供器中。

import React, {useState} from 'react';
import TacoTranslate, {Translate} from 'tacotranslate/react';

const tacoTranslate = createTacoTranslateClient({
	apiKey: 'YOUR_API_KEY',
});

export default function App() {
	const [locale, setLocale] = useState('en');

	return (
		<TacoTranslate client={tacoTranslate} locale={locale}>
			<Translate string="Hello, world!"/>
		</TacoTranslate>
	);
}

现在您可以在应用的任何位置使用 Translate 组件来显示翻译后的文本!务必查看 我们的文档,以获取更多信息以及针对您设置的实现指南。

import {Translate} from 'tacotranslate/react';

export default async function Component() {
	return (
		<Translate string="Hello? This is TacoTranslate speaking." />
	);
}

使用 TacoTranslate 的好处

  • 节省时间:自动化繁琐的本地化和字符串收集过程,为您节省宝贵时间。
  • 成本效益高:减少对人工翻译的需求,降低您的本地化成本。
  • 提高准确性:AI 驱动的翻译确保上下文语义的准确性并提供高质量的结果。
  • 可扩展的解决方案:随着您的应用和客户群增长,轻松添加对新语言的支持。

立即开始!

当您将任何字符串添加到 Translate 组件时,您的 React 应用将被自动翻译。请注意,只有在 API 密钥上具有 read/write 权限的环境才能创建要翻译的新字符串。

我们建议设置一个封闭且受保护的预发布环境,在上线前在那里测试您的生产应用并添加新的字符串。这样可以防止任何人 任何人 窃取您的秘密 API 密钥,并可能通过添加恶意字符串使您的翻译项目膨胀。

Be sure to check out the complete examples over at our GitHub profile. If you encounter any problems, feel free to reach out, and we’ll be more than happy to help.

TacoTranslate lets you automatically localize your React applications quickly to and from over 75 languages. Translate for free!

来自 Nattskiftet 的产品挪威制造