Skip to content

Multilingual Fields

Status: 🧪 Experimental (@Multilingual, since 3.1)

Some texts exist in several languages: a product’s description in a catalogue sold in three countries, a hotel’s room name, a notice shown to guests. Modelling them as one field per language (descriptionEs, descriptionEn, …) spreads one fact across the form, and the person writing them has to translate by hand. A multilingual field is one field whose value is a text per language.

Annotate a Map<String, String> field (language → text) with @Multilingual:

@Multilingual(languages = {"es", "en", "de"}, multiline = true)
@NotEmpty
Map<String, String> description = new LinkedHashMap<>();

Every renderer draws the same field:

  • A language switch above the input, starting on the UI’s language when the field has it, otherwise on the main language (the first one). Languages still without a text are shown in italics.
  • Show all languages stacks one input per language, each labelled with the language’s name.
  • Translate (only while the server can translate, see below) fills the other languages from the one on screen. A translated text carries an auto mark until somebody edits it.
  • Translate automatically (autoTranslate = true makes it the default; the user can switch it off) translates whenever a language is committed, as long as that language was written by a person.

A translation never overwrites what somebody wrote: it is applied to a language only while that language still holds what it held when the translation was asked for, and an explicit Translate only replaces languages that are empty or were machine-translated (when every language is written by a person, an explicit press translates all of them).

Read-only, the field shows one text: the UI’s language, otherwise the first one written. A listing column does the same.

@NotEmpty / @NotNull mean a text in the main language. The browser checks it before a validationRequired action and the server checks it again.

@Multilingual(languages = …) wins. A field that declares none uses the application’s languages: a LanguagesSupplier bean, otherwise the mateu.languages setting (es,en,de, main language first), otherwise English.

Mateu ships no translation service. Register a TextTranslator bean and every editable, translatable multilingual field with more than one language offers Translate:

@Service
public class DeepLTranslator implements TextTranslator {
@Override
public Map<String, String> translate(String text, String from, List<String> to, HttpRequest rq) {
// call your provider; answer language → translated text (missing languages are left alone)
}
}

The browser runs the field’s reserved action translate-<field> with _text, _from and _targets. The answer only carries data (_translations); the client merges it, so a slow translator never undoes typing. The server refuses it (403) when the field is read-only, not translatable (translatable = false, e.g. a brand name) or no translator is registered, and answers “Unknown language” for a language the field does not have.

A @FieldAction on a multilingual field receives the texts as _value and the language on screen as _language, so a “Review” button can check the text the user is looking at.

JavaC#Python
Field@Multilingual Map<String,String>[Multilingual] Dictionary<string,string>Annotated[dict[str, str], Multilingual(...)]
TranslatorTextTranslator beanITextTranslator serviceTextTranslator subclass / add_mateu(text_translators=…)
LanguagesLanguagesSupplier / mateu.languagesILanguagesSupplier / MateuOptions.Languages / MATEU_LANGUAGESLanguagesSupplier / add_mateu(languages=…)

Vaadin, Vaadin Aura, Primer, UI5, SLDS, Redwood, React Native and the desktop app. In Redwood the field is Mateu’s own element styled with Redwood tokens (JET has no multilingual input).