Translations and localesMIT
Translate form text, choose a locale for built-in messages and number and date formatting, override messages, and add your own languages.
A form has two kinds of text: the text you write in the schema, such as titles and choices, and the built-in messages, such as "This field is required." or the Next button. Both follow the form's locale.
Translate schema text#
Any user-facing text in a schema accepts either a string or an object of translations keyed by language or locale:
import type { FormSchema } from '@birchui/forms';
const visitorSignIn: FormSchema = {
title: { en: 'Visitor sign-in', de: 'Besucheranmeldung', fr: 'Enregistrement des visiteurs' },
elements: [
{
type: 'text',
name: 'visitorName',
title: { en: 'Your name', de: 'Ihr Name', fr: 'Votre nom' },
required: true,
},
{
type: 'radio',
name: 'purpose',
title: { en: 'Purpose of visit', de: 'Zweck des Besuchs', fr: 'Motif de la visite' },
choices: [
{ value: 'meeting', text: { en: 'Meeting', de: 'Besprechung', fr: 'Réunion' } },
{ value: 'delivery', text: { en: 'Delivery', de: 'Lieferung', fr: 'Livraison' } },
{ value: 'maintenance', text: { en: 'Maintenance', de: 'Wartung', fr: 'Maintenance' } },
],
},
{
type: 'checkbox',
name: 'safetyBriefing',
title: {
en: 'I have read the site safety rules.',
de: 'Ich habe die Sicherheitsregeln gelesen.',
fr: "J'ai lu les règles de sécurité du site.",
},
required: true,
},
],
};This works for title, description, placeholder, choice and matrix texts, requiredMessage, patternMessage, validator messages, markdown content, page titles, button labels and completedMessage. Choice values stay the same in every language, so the submitted data does not depend on the language the user picked.
For a locale such as de-AT, the form looks for a translation in this order:
- the exact locale,
de-AT; - the language,
de; - a
defaultentry; - English,
en; - the first translation in the object.
So { en: 'Postcode', 'en-US': 'ZIP code' } shows "ZIP code" in the United States and "Postcode" everywhere else.
Choose the locale#
The form's locale is the locale option, or the schema's locale, or English. It selects translations and built-in messages, and it formats numbers in computed fields and dates in validation messages:
const form = createForm(container, { schema: visitorSignIn, locale: navigator.language });
languagePicker.addEventListener('change', () => form.update({ locale: languagePicker.value }));Changing the locale with update() re-renders the form in the new language and keeps the user's answers.
Built-in languages#
Built-in messages ship in English (en), German (de), French (fr) and Spanish (es). A regional locale such as fr-CA uses its language's messages. Any other locale falls back to English for built-in messages, while your schema translations still apply.
availableLocales() lists the locales with messages, including any you register.
Messages#
Every built-in message has a key:
Placeholders in braces are filled in: {min}, {max}, {file}, {title}, {current}, {total} and {value}.
Override messages for one form#
Pass the keys you want to change in messages. The rest keep the locale's text:
createForm(container, {
schema: visitorSignIn,
locale: 'en-GB',
messages: {
required: 'Please answer this question.',
submit: 'Sign in',
completed: 'You are signed in. Please collect your visitor badge from reception.',
},
});To change text for one field only, use requiredMessage or a validator message in the schema instead.
Add a language#
registerLocale(locale, messages) adds or extends a language for every form on the page. Missing keys fall back to English.
import { registerLocale } from '@birchui/forms';
registerLocale('nl', {
required: 'Dit veld is verplicht.',
email: 'Voer een geldig e-mailadres in, zoals [email protected].',
next: 'Volgende',
prev: 'Terug',
submit: 'Verzenden',
progress: 'Pagina {current} van {total}',
errorSummary: 'Corrigeer het volgende',
selectPlaceholder: 'Kies een optie',
completed: 'Bedankt. Uw antwoorden zijn verzonden.',
});
createForm(container, { schema: visitorSignIn, locale: 'nl' });Register languages before creating the forms that use them. Calling registerLocale for an existing locale, such as 'de', replaces only the keys you pass.
Helpers#
| Export | Purpose |
|---|---|
getMessages(locale, overrides?) |
The full message set for a locale, merged with overrides. |
localize(text, locale) |
Resolves a string or translation object with the lookup order above. |
formatMessage(template, params) |
Fills {placeholders} in a message. |
availableLocales() |
The locales that have message sets. |
import { formatMessage, getMessages, localize } from '@birchui/forms';
localize({ en: 'Postcode', 'en-US': 'ZIP code' }, 'en-US');
formatMessage(getMessages('de').minLength, { min: 8 });These return "ZIP code" and "Geben Sie mindestens 8 Zeichen ein.", handy for showing schema text or messages in your own UI around the form.