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