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