Use cases
Customize how Weglot translates your React app.
What gets translated
The plugin translates text written in JSX, and:
string literals inside JSX expressions, such as
{ok ? "Saved" : "Not saved"}or{name || "Anonymous"};strings read from a constant, such as
{STATUSES[status]}or{LABELS.title};the
title,alt,label,placeholder,aria-label,aria-description,description,subtitleandcontentattributes.
A sentence keeps its markup: <p>Hello <strong>world</strong>, welcome</p> is translated as one string, so the translation can place <strong> where the target language needs it.
The plugin leaves alone strings outside JSX (a useState("idle"), a route, an event name), identifiers and class names, URLs and file names, the content of <code>, <pre>, <script>, <style>, <kbd>, <samp> and <var>, and anything it cannot read statically.
Exclude content from translation
Add translate="no" or data-weglot-skip to any element. Its content, and its children, stay in the original language.
<span translate="no">Taskly</span>
<div data-weglot-skip>{customerQuote}</div>Translate your own component props
By default, only the built-in attributes listed above are translated. Use translatableAttributes to declare which of your components' props hold text. Each entry adds a prop name, or removes it with a ! prefix. "*" applies first, then the component's own entry.
withWeglot(nextConfig, {
sourceRoot: "./app",
translatableAttributes: {
"*": ["!label"], // never translate `label`...
Button: ["label"], // ...except on Button
PageTitle: ["heading"],
"Select.Option": ["hint"], // only this Option
},
});Prop names are case-insensitive. Component keys are case-sensitive and use the exported name, so import { PageTitle as PT } still matches PageTitle.
Add a language switcher
Use the LocaleSwitcher component, a dropdown of the languages you list:
To match your own design, use useWeglotContext:
The visitor's language is stored in a cookie (see localePersistence in Settings). All languages are served on the same URLs, so search engines only index the source language.
Last updated