Лучшие практики
Поместите URL-адреса в переменные
When translating strings that contain URLs or similar data, it is considered a good practice to place these URLs inside variables and then referencing them within your templates.
<Translate
string={`Click <a href="{{url}}">here</a>`}
variables={{url: 'https://tacotranslate.com'}}
/>
Используйте метки ARIA
When translating the text of interactive elements like buttons, it’s important to include ARIA labels to ensure accessibility. ARIA labels help screen readers provide descriptive information about the element’s function.
For instance, if you have a button that lets users copy text from a code block, you can use the aria-label
attribute to provide a clear description:
<Translate
aria-label={useTranslation('Copy to clipboard')}
string="Copy"
/>
Something about this feels very meta.
Глобальный массив источников и несколько источников компонентов
This pattern only works when using the Next.js Pages Router.
При работе с более крупными приложениями полезно разбивать строки и переводы на несколько, более мелких источников. Этот подход помогает уменьшить размеры пакетов и время передачи, обеспечивая эффективную и масштабируемую локализацию.
Хотя это просто, когда рендеринг происходит только на стороне клиента, управление источниками быстро становится сложным при получении переводов для серверного рендеринга. Однако вы можете автоматизировать управление источниками, используя глобальный origins
массив.
Рассмотрим этот пример, в котором мы разделили наши компоненты и страницы на отдельные файлы.
export const defaultOrigin = process.env.TACOTRANSLATE_ORIGIN;
const origins = [];
export default origins;
import TacoTranslate, {Translate} from 'tacotranslate/react';
import origins from '../utilities/tacotranslate-origins';
// Set an origin name for this component
const origin = 'components/pricing-table';
// Push the origin into the origins array as this file is imported
origins.push(origin);
export default function PricingTable() {
return (
<TacoTranslate origin={origin}>
<Translate string="Pricing table" />
// ...
</TacoTranslate>
);
}
import TacoTranslate, {Translate} from 'tacotranslate/react';
import origins from '../utilities/tacotranslate-origins';
import PricingTable from '../components/pricing-table';
const origin = 'pages/pricing';
export default function PricingPage() {
return (
<TacoTranslate origin={origin}>
<Translate string="Pricing page" />
<PricingTable />
</TacoTranslate>
);
}
// We can now fetch translations for all imported components and their origins automatically
export async function getStaticProps(context) {
return customGetStaticProps(context, [...origins, origin]);
}
Посмотрите наши примеры серверного рендеринга для получения дополнительной информации о customGetStaticProps
.