แนวปฏิบัติที่ดีที่สุด
ใส่ 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.
เมื่อทำงานกับแอปพลิเคชันขนาดใหญ่ การแยกสตริงและการแปลออกเป็นหลายๆ แหล่งที่มาขนาดเล็กจะเป็นประโยชน์ วิธีนี้ช่วยลดขนาดบันเดิลและเวลาในการถ่ายโอน ทำให้การทำ localization มีประสิทธิภาพและสามารถปรับขนาดได้ดีขึ้น.
แม้ว่าจะทำได้อย่างตรงไปตรงมาเมื่อเรนเดอร์เฉพาะฝั่งไคลเอนต์ แต่การจัดการ origins จะซับซ้อนขึ้นอย่างรวดเร็วเมื่อดึงการแปลสำหรับการเรนเดอร์ฝั่งเซิร์ฟเวอร์ อย่างไรก็ตาม คุณสามารถอัตโนมัติการจัดการแหล่งที่มาด้วยการใช้แอเรย์ TacoTranslate client origins.
พิจารณาตัวอย่างนี้ ซึ่งเราได้แยกคอมโพเนนต์และเพจออกเป็นไฟล์แยกกัน.
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>
);
}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.