TacoTranslate
/
文件說明價格
 
  1. 介紹
  2. 入門指南
  3. 設定與配置
  4. 使用 TacoTranslate
  5. 伺服器端渲染
  6. 進階用法
  7. 最佳實踐
  8. 錯誤處理與除錯
  9. 支援的語言

進階用法

處理從右至左的語言

TacoTranslate 讓您輕鬆在 React 應用程式中支援從右到左(RTL)語言,例如阿拉伯語和希伯來語。正確處理 RTL 語言可確保內容對於從右到左閱讀的使用者正確顯示。

import {useTacoTranslate} from 'tacotranslate/react';

function Document() {
	const {locale, isRightToLeft} = useTacoTranslate();

	return (
		<html lang={locale} dir={isRightToLeft ? 'rtl' : 'ltr'}>
			<body>
				// ...
			</body>
		</html>
	);
}

您也可以使用提供的 isRightToLeftLocaleCode 函式來檢查 React 之外的當前語言。

import {isRightToLeftLocaleCode} from 'tacotranslate';

function foo(locale = 'es') {
	const direction = isRightToLeftLocaleCode(locale) ? 'rtl' : 'ltr';
	// ...
}

停用翻譯

要禁用對字串中特定部分的翻譯或確保某些片段保持原樣,您可以使用三重方括號。此功能有助於維持名稱、技術術語或任何其他不應被翻譯內容的原始格式。

import {Translate} from 'tacotranslate/react';

function Component() {
	return (
		<Translate string="Hello, [[[TacoTranslate]]]!" />
	);
}

在此範例中,單字 “TacoTranslate” 將在翻譯中保持不變。

多個 TacoTranslate 提供者

我們強烈建議在您的應用程式中使用多個 TacoTranslate 提供者。這對於將您的翻譯和字串組織到不同的來源(例如您的標頭、頁腳或特定區段)非常有用。

您可以在此閱讀更多關於使用來源的資訊。

TacoTranslate 提供者會繼承任何父提供者的設定,因此您不必重複其他設定。

import createTacoTranslateClient from 'tacotranslate';
import {TacoTranslate} from 'tacotranslate/react';

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

function Header() {
	return (
		<TacoTranslate origin="header">
			// ...
		</TacoTranslate>
	);
}

function Menu() {
	return (
		<TacoTranslate origin="menu">
			// ...
		</TacoTranslate>
	);
}

export default function App() {
	return (
		<TacoTranslate client={tacoTranslateClient} origin="page" locale="es">
			<Header />
			<Menu />
		</TacoTranslate>
	);
}

覆寫來源或區域設定

除了使用多個 TacoTranslate 提供者之外,您還可以在 Translate 元件和 useTranslation 鉤子層級上覆蓋原始來源和語言環境。

import {Translate, useTranslation} from 'tacotranslate/react';

function Greeting() {
	const spanishHello = useTranslation('Hello!', {locale: 'es'});

	return (
		<>
			{spanishHello}
			<Translate string="What’s up?" origin="greeting" />
		</>
	);
}

處理載入中

當在客戶端切換語言時,根據使用者的連線情況,獲取翻譯可能需要一些時間。您可以顯示載入指示器,以在切換過程中提供視覺反饋,提升使用者體驗。

import {useTacoTranslate} from 'tacotranslate/react';

function Component() {
	const {isLoading} = useTacoTranslate();

	return (
		isLoading ? 'Translations are loading...' : null
	);
}

複數形式

為了正確處理複數形式並顯示基於數量的標籤,在不同語言中,這被視為最佳做法:

import {Translate, useLocale} from 'tacotranslate/react';

function PhotoCount() {
	const locale = useLocale();
	const count = 1;

	return count === 0 ? (
		<Translate string="You have no photos." />
	) : count === 1 ? (
		<Translate string="You have 1 photo." />
	) : (
		<Translate
			string="You have {{count}} photos."
			variables={{count: count.toLocaleString(locale)}}
		/>
	);
}

多種語言

要在同一應用程式中同時支援多種語言,您可以使用多個 TacoTranslate 提供者,並設定不同的 locale 值,如下所示:

您也可以在 locale 元件或鉤子層級上覆蓋

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

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

function Spanish() {
	return (
		<TacoTranslate locale="es">
			<Translate string="Hello, world in Spanish!" />
		</TacoTranslate>
	);
}

function Norwegian() {
	return (
		<TacoTranslate locale="no">
			<Translate string="Hello, world in Norwegian!" />
		</TacoTranslate>
	);
}

export default function App() {
	return (
		<TacoTranslate client={tacoTranslateClient} origin="page" locale="es">
			<Spanish />
			<Norwegian />
		</TacoTranslate>
	);
}

使用翻譯 ID

您可以在 Translate 元件中新增一個 id,以處理相同字串的不同翻譯或含義。這在相同文字根據上下文需要不同翻譯時尤其有用。透過分配唯一的 ID,您可以確保每個字串實例都能根據其特定含義正確翻譯。

import {Translate} from 'tacotranslate/react';

function Header() {
	return (
		<Translate id="header" string="Login" />
	);
}

function Footer() {
	return (
		<Translate id="footer" string="Login" />
	);
}

例如,標頭的登入可能會翻譯成西班牙語的“Iniciar sesión”,而頁腳的登入則可能翻譯成“Acceder”。

最佳實踐

來自Nattskiftet的產品