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 フックのレベルで、origin と locale の両方をオーバーライドすることもできます。

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)}}
		/>
	);
}

複数の言語

同じアプリケーション内で複数の言語を同時にサポートするには、以下のように異なる locale 値を持つ複数の TacoTranslate プロバイダーを使用できます。

また、コンポーネントやフックレベルで 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 の製品ノルウェー製