การใช้งานขั้นสูง
การจัดการภาษาที่อ่านจากขวาไปซ้าย
TacoTranslate ทำให้การรองรับภาษาที่อ่านจากขวาไปซ้าย (RTL) เช่น ภาษาอาหรับและภาษาฮีบรู ในแอป React ของคุณเป็นเรื่องง่าย การจัดการภาษาที่อ่านจากขวาไปซ้ายอย่างถูกต้องจะช่วยให้เนื้อหาของคุณแสดงผลได้อย่างถูกต้องสำหรับผู้ใช้ที่อ่านจากขวาไปซ้าย
import {useTacoTranslate} from 'tacotranslate/react';
function Document() {
const {locale, isRightToLeft} = useTacoTranslate();
return (
<html lang={locale} dir={isRightToLeft ? 'rtl' : 'ltr'}>
<body>
// ...
</body>
</html>
);
}คุณยังสามารถใช้ฟังก์ชัน isRightToLeftLocaleCode ที่มีให้ เพื่อตรวจสอบภาษาปัจจุบันนอก React.
import {isRightToLeftLocaleCode} from 'tacotranslate';
function foo(locale = 'es') {
const direction = isRightToLeftLocaleCode(locale) ? 'rtl' : 'ltr';
// ...
}การปิดการแปล
หากต้องการปิดการแปลสำหรับส่วนเฉพาะของสตริง หรือเพื่อให้บางส่วนถูกเก็บไว้ตามเดิม คุณสามารถใช้วงเล็บเหลี่ยมสามชั้น ฟีเจอร์นี้มีประโยชน์ในการรักษารูปแบบต้นฉบับของชื่อ คำศัพท์ทางเทคนิค หรือเนื้อหาอื่น ๆ ที่ไม่ควรถูกแปล
import {Translate} from 'tacotranslate/react';
function Component() {
return (
<Translate string="Hello, [[[TacoTranslate]]]!" />
);
}ในตัวอย่างนี้ คำว่า “TacoTranslate” จะยังคงไม่ถูกเปลี่ยนแปลงในการแปล
ผู้ให้บริการ TacoTranslate หลายราย
เราแนะนำเป็นอย่างยิ่งให้ใช้ผู้ให้บริการ TacoTranslate หลายตัวในแอปของคุณ ซึ่งมีประโยชน์สำหรับการจัดระเบียบการแปลและสตริงให้อยู่ในต้นกำเนิดต่างๆ เช่น ส่วนหัว ส่วนท้าย หรือส่วนเฉพาะต่างๆ
คุณสามารถ อ่านข้อมูลเพิ่มเติมเกี่ยวกับการใช้ต้นทางได้ที่นี่.
TacoTranslate โปรไวเดอร์จะสืบทอดการตั้งค่าจากโปรไวเดอร์หลัก ดังนั้นคุณจึงไม่จำเป็นต้องทำซ้ำการตั้งค่าอื่น ๆ
import createTacoTranslateClient from 'tacotranslate';
import {TacoTranslate} from 'tacotranslate/react';
const tacoTranslateClient = createTacoTranslateClient({apiKey: 'YOUR_API_KEY'});
function Header() {
return (
<TacoTranslate origin="header">
// ...
</TacoTranslate>
);
}
function Menu() {
return (
<TacoTranslate origin="menu">
// ...
</TacoTranslate>
);
}
export default function App() {
return (
<TacoTranslate client={tacoTranslateClient} origin="page" locale="es">
<Header />
<Menu />
</TacoTranslate>
);
}การเขียนทับ origin หรือ locale
นอกเหนือจากการใช้ผู้ให้บริการ TacoTranslate หลายตัวแล้ว คุณยังสามารถแทนที่ทั้ง origin และ locale ที่ระดับคอมโพเนนต์ Translate และฮุก useTranslation ได้ด้วย
import {Translate, useTranslation} from 'tacotranslate/react';
function Greeting() {
const spanishHello = useTranslation('Hello!', {locale: 'es'});
return (
<>
{spanishHello}
<Translate string="What’s up?" origin="greeting" />
</>
);
}การจัดการการโหลด
เมื่อเปลี่ยนภาษาในฝั่งไคลเอนต์ การดึงคำแปลอาจใช้เวลาสักครู่ ขึ้นอยู่กับการเชื่อมต่อของผู้ใช้ คุณสามารถแสดงตัวบ่งชี้การโหลดเพื่อยกระดับประสบการณ์ผู้ใช้โดยให้สัญญาณตอบรับเป็นภาพในระหว่างการสลับ
import {useTacoTranslate} from 'tacotranslate/react';
function Component() {
const {isLoading} = useTacoTranslate();
return (
isLoading ? 'Translations are loading...' : null
);
}การทำพหุพจน์
เพื่อจัดการการใช้รูปพหูพจน์และการแสดงป้ายที่ขึ้นกับจำนวนให้ถูกต้องในภาษาต่าง ๆ นี่ถือเป็นแนวทางปฏิบัติที่ดีที่สุด:
import {Translate, useLocale} from 'tacotranslate/react';
function PhotoCount() {
const locale = useLocale();
const count = 1;
return count === 0 ? (
<Translate string="You have no photos." />
) : count === 1 ? (
<Translate string="You have 1 photo." />
) : (
<Translate
string="You have {{count}} photos."
variables={{count: count.toLocaleString(locale)}}
/>
);
}หลายภาษา
เพื่อรองรับหลายภาษาในแอปพลิเคชันเดียวกันพร้อมกัน คุณสามารถ ใช้ผู้ให้บริการ TacoTranslate หลายตัว โดยมีค่า locale ที่แตกต่างกัน ดังแสดงด้านล่าง:
คุณยังสามารถเขียนทับค่า locale ได้ในระดับคอมโพเนนต์หรือฮุก.
import createTacoTranslateClient from 'tacotranslate';
import {TacoTranslate, Translate} from 'tacotranslate/react';
const tacoTranslateClient = createTacoTranslateClient({apiKey: 'YOUR_API_KEY'});
function Spanish() {
return (
<TacoTranslate locale="es">
<Translate string="Hello, world in Spanish!" />
</TacoTranslate>
);
}
function Norwegian() {
return (
<TacoTranslate locale="no">
<Translate string="Hello, world in Norwegian!" />
</TacoTranslate>
);
}
export default function App() {
return (
<TacoTranslate client={tacoTranslateClient} origin="page" locale="es">
<Spanish />
<Norwegian />
</TacoTranslate>
);
}การใช้รหัสการแปล
คุณสามารถเพิ่ม id ให้กับคอมโพเนนต์ Translate เพื่อจัดการการแปลหรือความหมายที่แตกต่างกันสำหรับสตริงเดียวกัน สิ่งนี้มีประโยชน์โดยเฉพาะเมื่อข้อความเดียวกันต้องการการแปลที่แตกต่างกันตามบริบท โดยการกำหนด ID ที่ไม่ซ้ำกัน คุณจะมั่นใจได้ว่าแต่ละกรณีของสตริงนั้นได้รับการแปลอย่างถูกต้องตามความหมายเฉพาะของมัน
import {Translate} from 'tacotranslate/react';
function Header() {
return (
<Translate id="header" string="Login" />
);
}
function Footer() {
return (
<Translate id="footer" string="Login" />
);
}ตัวอย่างเช่น การเข้าสู่ระบบในส่วนหัวอาจแปลเป็น “Iniciar sesión” และการเข้าสู่ระบบในส่วนท้ายอาจแปลเป็น “Acceder” ในภาษาสเปน