แนวทางปฏิบัติที่ดีที่สุด
เก็บ 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 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.