Advanced

Localization

Translate every user-facing string the library renders — loading indicators, "No results found", the default required error, and aria-labels — on a per-form basis via the strings prop.

Added in 0.8.0

The strings prop and the FormStrings type are available from version 0.8.0 onward, on both Form (web) and NativeForm (React Native).


The strings prop

Pass a partial strings object to any form. Any key you provide overrides that string; every key you leave out keeps its English default — so you only translate what you need.

import { Form } from '@nestledjs/forms'
;<Form
  id="fr-form"
  strings={{
    requiredError: 'Ce champ est requis',
    loading: 'Chargement...',
    noResults: 'Aucun résultat',
    selectPlaceholder: 'Sélectionnez une option...',
    removeItem: (label) => `Supprimer ${label}`,
  }}
  fields={fields}
  submit={handleSubmit}
/>

The same prop works identically on NativeForm:

import { NativeForm } from '@nestledjs/forms-native'
;<NativeForm
  id="fr-form"
  strings={{ requiredError: 'Ce champ est requis' }}
  fields={fields}
  submit={handleSubmit}
/>

Available keys

Every key on the FormStrings type, with its English default:

KeyDefaultUsed for
requiredErrorThis field is requiredFallback error for required fields
loadingLoading...Search select: options are loading
noResultsNo results foundSearch select: nothing matches the search
noOptionsNo options availableSearch select: the options list is empty
selectPlaceholderSelect an option...Select: default placeholder option
searchPlaceholderSearch...Search inputs: default placeholder
readOnlyYesYesRead-only checkbox/switch: checked
readOnlyNoNoRead-only checkbox/switch: unchecked
noneSelectedNo options selectedRead-only multi selections: nothing selected
clearSelectionClear selectionaria-label for the clear button
toggleDropdownToggle dropdownaria-label for the dropdown toggle
removeItem`Remove ${label}`aria-label for a multi-select chip's remove button (a function of the item label)

removeItem is a function so the label can be interpolated into the translated string:

strings={{ removeItem: (label) => `Supprimer ${label}` }}

The FormStrings type is exported from @nestledjs/forms-core if you want to type a shared translation object:

import type { FormStrings } from '@nestledjs/forms-core'

const fr: Partial<FormStrings> = {
  requiredError: 'Ce champ est requis',
  loading: 'Chargement...',
}

Precedence

A per-field errorMessages.required always wins over strings.requiredError. Use strings for the app-wide default and errorMessages.required for a specific field's message:

// strings.requiredError is the default for every field...
<Form
  strings={{ requiredError: 'Ce champ est requis' }}
  fields={[
    // ...but this field overrides it
    FormFieldClass.text('email', {
      label: 'E-mail',
      required: true,
      errorMessages: { required: 'Veuillez saisir votre e-mail' },
    }),
  ]}
/>
Previous
Validation