TacoTranslate
/
ドキュメンテーション価格
 
  1. イントロダクション
  2. はじめに
  3. セットアップと構成
  4. TacoTranslateの使用
  5. サーバーサイドレンダリング
  6. 高度な使用法
  7. ベストプラクティス
  8. エラー処理とデバッグ
  9. サポートされている言語

ベストプラクティス

URLを変数に入れる

URLや類似のデータを含む文字列を翻訳する際は、これらのURLを変数内に格納し、テンプレート内で参照する方法が推奨されます。

<Translate
	string={`Click <a href="{{url}}">here</a>`}
	variables={{url: 'https://tacotranslate.com'}}
/>

ARIAラベルを使用する

ボタンのようなインタラクティブな要素のテキストを翻訳する際は、アクセシビリティを確保するためにARIAラベルを含めることが重要です。ARIAラベルはスクリーンリーダーが要素の機能についての説明情報を提供するのに役立ちます。

例えば、コードブロックからテキストをコピーするボタンがある場合、 aria-label 属性を使って明確な説明を提供できます:

<Translate
	aria-label={useTranslation('Copy to clipboard')}
	string="Copy"
/>

これについては何か非常にメタな感じがします。

グローバル起点配列と複数のコンポーネント起点

このパターンは Next.js Pages Router を使用している場合にのみ機能します。

大規模なアプリケーションで作業する場合、文字列と翻訳を複数の小さなオリジンに分割することが有益です。この方法はバンドルサイズと転送時間を減らし、効率的でスケーラブルなローカリゼーションを実現します。

これはクライアント側のみでレンダリングする場合は簡単ですが、サーバーサイドレンダリングのために翻訳を取得する際にはオリジンの管理が急速に複雑になります。ただし、TacoTranslateクライアントの origins 配列を利用することでオリジン管理を自動化できます。

例えば、コンポーネントとページを別々のファイルに分割した以下の例を考えてみてください。

components/pricing-table.tsx
import TacoTranslate, {Translate} from 'tacotranslate/react';
import tacoTranslate from '../tacotranslate-client';

// Set an origin name for this component
const origin = 'components/pricing-table';

// Push the origin into the origins array as this file is imported
tacoTranslate.origins.push(origin);

export default function PricingTable() {
	return (
		<TacoTranslate origin={origin}>
			<Translate string="Pricing table" />
			// ...
		</TacoTranslate>
	);
}
pages/pricing.tsx
import TacoTranslate, {Translate} from 'tacotranslate/react';
import getTacoTranslateStaticProps from 'tacotranslate/next/get-static-props';
import tacoTranslateClient from '../tacotranslate-client';
import PricingTable from '../components/pricing-table';

const origin = 'pages/pricing';
tacoTranslateClient.origins.push(origin);

export default function PricingPage() {
	return (
		<TacoTranslate origin={origin}>
			<Translate string="Pricing page" />
			<PricingTable />
		</TacoTranslate>
	);
}

// We will now fetch translations for all imported components and their origins automatically
export async function getStaticProps(context) {
	return getTacoTranslateStaticProps(context, {client: tacoTranslateClient});
}

getTacoTranslateStaticPropsに関する詳細は、サーバーサイドレンダリングの例をご覧ください。

エラー処理とデバッグ

Nattskiftetの製品ノルウェー製