Component strings
Your content is yours to translate. A component also generates text of its own: the name of a calendar's Next month
button, a rating's 4 stars
, a validation message. Each of those strings has an English default and a data-label-* attribute that replaces it.
<form-field> <label for="when">Reisedatum</label> <date-picker data-label-toggle="Kalender öffnen" data-label-dialog="Datum wählen" data-label-prev="Vorheriger Monat" data-label-next="Nächster Monat"> <input type="date" id="when" name="when" /> </date-picker></form-field>
Three things to know:
- Where the attribute goes. A component reads
data-label-* from its own element. Attribute utilities such as data-hotkey read it from the nearest ancestor that has it, so one attribute on <html> covers the page.
- Placeholders. A string can contain tokens in braces, such as
{n} or {date}, which the component fills in. Keep them in the translation and move them where the grammar needs them.
- Plurals. Where a count is involved, the attribute name ends in a plural category from
Intl.PluralRules: data-label-one, data-label-few, data-label-other. Supply the categories the language has.
<star-rating lang="de" label="Reise bewerten" data-label-one="{n} Stern" data-label-other="{n} Sterne"></star-rating>
The attributes for each component are listed in the table on its page. Dates, month names and weekday names need no attribute. They come from Intl, in the language set by the nearest lang.
Page strings
The most reliable translated page is one the server sends already translated, with its own URL and its own lang. Do that where you can.
For an application that switches language without a reload, VbI18n swaps strings in place. Mark each element with a key and give the class your messages. Nothing runs until you create it.
<nav> <a href="/" data-i18n="nav.home">Home</a></nav> <input type="search" data-i18n="search.placeholder" data-i18n-attr="placeholder" placeholder="Search" /> <p data-i18n="welcome" data-i18n-vars='{"name":"Ada"}'>Hello, Ada</p>
import { VbI18n } from 'vanilla-breeze'; const i18n = new VbI18n({ locale: 'fr', fallbackLocale: 'en', messages: { en: { 'nav.home': 'Home', welcome: 'Hello, {name}' }, fr: { 'nav.home': 'Accueil', welcome: 'Bonjour, {name}' }, },}); i18n.apply(); i18n.t('welcome', { name }); await i18n.load('de', '/messages/de.json'); i18n.setLocale('de');
The English text in the markup is the fallback. It shows before the script runs and for any key with no translation. When you change language this way, update lang and dir on <html> as well, so typography, formatting and direction follow.
Text that must not be translated
Browsers and translation services translate whatever they find. Code, commands, product names and user names should stay as written. The translate attribute says so. It is an HTML attribute and no stylesheet can set it, so it has to be in your markup.
<p>Install <span translate="no">Vanilla Breeze</span> from npm.</p><p>Run <code translate="no">npm install</code> first.</p><pre translate="no"><code>const greeting = 'Hello';</code></pre><p>Signed in as <span translate="no">ada_okafor</span>.</p>
Add it to <code>, <kbd>, <samp>, <pre> and <var> content, and to names and identifiers in running text.