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 的產品 Nattskiftet挪威製造