TacoTranslate
/
DocumentationPricing
 
  1. Introduction
  2. Getting started
  3. Setup and configuration
  4. Using TacoTranslate
  5. Server-side rendering
  6. Advanced usage
  7. Best practices
  8. Error handling and debugging
  9. Supported languages

Best practices

Put URLs in variables

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'}}
/>

Use ARIA labels

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.

Global origins array and multiple component origins

This pattern only works when using the Next.js Pages Router.

When working with larger applications, it’s beneficial to split strings and translations into multiple, smaller origins. This approach helps decrease bundle sizes and transfer times, ensuring efficient and scalable localization.

While this is straightforward when rendering only on the client side, managing origins quickly becomes complex when fetching translations for server-side rendering. However, you can automate origin management by utilizing a global origins array.

Consider this example, where we have separated our components and pages into separate files.

utilities/tacotranslate-origins.ts
export const defaultOrigin = process.env.TACOTRANSLATE_ORIGIN;
const origins = [];

export default origins;
components/pricing-table.tsx
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>
	);
}
pages/pricing.tsx
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]);
}

See our server-side rendering examples for more information about customGetStaticProps.

Error handling and debugging