Internationalization (i18n)

BlueForm translates at the form level, not inside field components. Labels, descriptions, placeholders and error messages are translated before they reach a field: useField() returns ready-to-render strings, so field components never deal with locales or an i18n library.

i18n is optional: without i18nConfig, every text is shown as written. Runnable examples (a hand-written t, JSON files, your own language store, i18next, react-intl, Lingui, Paraglide JS): i18n recipe.

config (keys)label: "email.label"description: "email.desc"props.placeholder: "email.hint"rules: { required: true }i18nConfigt(key, params)validationTranslation: { required: "validation.required"}messages (any source)"email.label": "Email""email.desc": "We never share it""email.hint": "you@company.com""validation.required": "{field} is required"BlueFormcalls tyour componentuseField()+ propsEmail *you@company.comWe never share itEmail is requiredwhat the user sees
Dashed: what you write. Only BlueForm calls t; your component gets finished strings from useField() and its props. A new t (the language changed): BlueForm translates again, the values are kept.

One function: t

Everything goes through i18nConfig.t(key, params):

WhatWhen t is calledparams
label, description, props.placeholderwhen the field rendersfrom { message, params }, see Text forms
error messageswhen they are displayed (errorMessage of useField() / useArrayField())field + the rule value, for validation messages

BlueForm does not dictate a key format or where messages come from: t can be a switch, a lookup in JSON files, or an i18n library's function. For a key it does not know, a good t returns the key itself (i18next does by default): plain text then shows unchanged next to translated text.

const t = (key: string, params?: Record<string, unknown>) =>
  (messages[lang][key] ?? key).replace(/\{(\w+)\}/g, (_, name) => String(params?.[name] ?? ""));

Where the config lives: setupForm and <Form>

i18nConfig can be given to setupForm, to a <Form>, or both. They are merged key by key, and the Form's keys win (v0.22+; before, a Form's i18nConfig replaced the setup's entirely). This is the same rule as formOptions.

KeyUsually inWhy
validationTranslationsetupFormyour message keys do not change with the language
t<Form>a new function per language, usually from a hook, which setupForm cannot call
enabledeitherfalse turns i18n off, for every form or for one
// once
const [Form] = setupForm({
  fieldMapping,
  renderRoot,
  i18nConfig: {
    validationTranslation: { required: "validation.required", minLength: "validation.minLength" },
  },
});

// each form: only what changes with the language
<Form i18nConfig={{ t }} config={config} />

How the merge resolves:

  • A key on the Form replaces the same key from setupForm; the other keys are kept. Override one key or all of them.
  • The merge is shallow: a Form's validationTranslation replaces the setup's whole map, it is not merged rule by rule.
  • A key set to undefined on the Form drops the setup's value for that form (e.g. validationTranslation: undefined).
  • Without a Form i18nConfig, the setup's applies as is.

When t comes from a hook, wrap the wiring once in a hook of your app and pass its result, so forms do not repeat it:

function useI18nConfig() {
  const { t } = useTranslation(); // or your own store, see below
  return useMemo(() => ({ t: (key: string, params?: Record<string, unknown>) => String(t(key, params)) }), [t]);
}

<Form i18nConfig={useI18nConfig()} config={config} />

A static t in setupForm (e.g. i18next.t) is enough when the app has one language, or reloads the page to switch.

Switching language

A new t function means the language changed. When the Form receives one:

  • labels, descriptions and placeholders are translated again;
  • fields that show a validation error are validated again, so their messages follow; fields without an error are not validated;
  • errors set with setError() (e.g. a server error) are kept, and translated again as they are displayed;
  • the values are kept: nothing is remounted.

So keep t stable within a language and new only when it changes. The hooks of i18n libraries already do that; a hand-written t needs a useMemo keyed on the language. A new function on every render reads as a language change on every render.

A changing t can come from:

  • an i18n library's hook: useTranslation(), useIntl(), useLingui();
  • your own language store, subscribed with useSyncExternalStore: getLang / setLang keep the language where you want (localStorage, a cookie, the URL) and setLang notifies the form. See Your own translator for the code and its trade-offs.

Validation messages

validationTranslation maps the built-in rules to message keys, so rules stay plain RHF rules:

i18nConfig: {
  validationTranslation: {
    required: "validation.required",   // "{field} is required"
    minLength: "validation.minLength", // "{field}: at least {minLength} characters"
  },
}
// config
username: { type: "text", label: "form.username", rules: { required: true, minLength: 3 } }
field configlabel: "email.label"rules: { required: true, minLength: 3 }validationTranslationrequired: "validation.required"minLength: "validation.minLength"BlueForm, for each rule in validationTranslationt("validation.required", { field: "Email" }) → "Email is required"t("validation.minLength", { field: "Email", minLength: 3 }) → "Email: at least 3 characters"rules handed to React Hook Formrequired: { value: true, message: "Email is required" }errorMessageEmail is required
{field} is the translated label and the rule value is a param. The message replaces the field's own message for that rule. Other errors (schema, setError(), validate) are translated when they are shown, without params.
  • It covers required, min, max, minLength, maxLength and pattern (not validate, whose functions return their own message).
  • t receives the key and { field, <rule>: value }: field is the translated label, and e.g. minLength: 3 for minLength.
  • It replaces the field's own message for that rule (rules: { required: "Name is needed" } shows the validation.required message). Leave a rule out of validationTranslation to keep per-field messages for it.

Custom error messages

Every error message goes through t when it is displayed, so custom messages can be keys too:

code: { type: "text", rules: { validate: (v) => v === "ok" || "errors.code.invalid" } },
// a schema message, a rule message, or a server error
form.setError("email", { type: "server", message: "errors.email.taken" })

These are translated without params. For a message with parameters, use validationTranslation, or build the message with your t in a config rebuilt when the language changes: the field is validated again with the new function.

Messages read directly from formState.errors are the raw keys.

Text forms

label, description and props.placeholder accept a TranslatableText:

FormExampleResult
key"form.username"t("form.username")
key + params{ message: "form.max", params: { n: 5 }, fallback: "Max" }t("form.max", { n: 5 }); fallback, then message, only if t returns undefined
literal{ text: "Họ và tên" }shown as is, t is not called (v0.14+)

A t that returns the key for unknown keys (as i18next does) never returns undefined, so fallback is not used with it.

Use the literal form for text that is not a key, e.g. labels loaded from a database, so your i18n library does not warn about missing keys:

label: row.isKey ? row.label : { text: row.label },

Placeholders and other props

v0.23+ (before, through translateProps)

props.placeholder is translated like a label: a key, { message, params } or { text }. Other field props are passed through untouched, since most are not text (type, variant, size) and sending them to t would make the i18n library report missing keys.

email: { type: "text", label: "form.email", props: { placeholder: "form.email.placeholder" } },

Help text goes in the field's description, translated, and the adapter passes it to the library's prop (Mantine description, MUI helperText). Other text props (title, aria-label) and nested values such as select options are translated where you build the config, with your t: each UI library shapes options differently (data, options, <option> children).

Type-checked keys

Added in v0.14.

Register your key union once with module augmentation, and label / description (including { message }) only accept known keys: typos fail at compile time. { text } still accepts any string.

// i18n.d.ts
import type { I18nKey as AppKey } from "./i18n/keys"; // e.g. derived from your JSON

declare module "react-headless-form" {
  interface Register {
    i18nKey: AppKey;
  }
}
username: { type: "text", label: "form.username" }, // ✅
email:    { type: "text", label: "form.emial" },    // ❌ compile-time error
note:     { type: "text", label: { text: "Raw" } }, // ✅ literal

Turning i18n off

With enabled: false (in setupForm or on one Form), t is not called: keys are shown as is, { message } shows its message, and { text } its text.