Skip to content

Languages and locale formatting

This is the mechanism for showing the server-rendered UI in more than one language. English is the source language, the default and the fallback of every message. Afrikaans ships as the showcase; more languages follow as needed. API error bodies do not change: they keep their code and their English message. The normative text is the languages specification.

The Nuxt UI package does not ship a message catalog. Its date display (FwLocalTime) formats in the viewer’s browser locale, or a locale you pass.

  • Catalogs. One flat JSON file per language, ui-kit/messages/<tag>.json. A message is a string, or a plural object whose keys are CLDR categories (one, other, and so on; other is required). Placeholders are {name}; a doubled opening or closing brace is a literal brace. Nothing else is syntax, so this is not ICU MessageFormat.
  • Keys start with a namespace: kit. for the UI kits, validation.<CODE> for validation codes, flash.<key> for an application’s flashes, and the application’s own (app. by convention).
  • Lookup, per key: the locale’s tag (af-ZA), then its language (af), then en. A key no catalog has renders as itself, and the tests fail on it.
  • Layers. The kit’s catalog first, then the application’s catalogs on top. Within one tag the top layer wins, so an application can override a kit message in a language it ships. An English override never beats the kit’s Afrikaans for an Afrikaans user.
  • Plurals. The category comes from the argument count (an integer or a decimal) by the CLDR rule of the message’s language: en (one when the number is exactly 1 with no fraction digits), af (one when n = 1, so 1.0 too), zu (one when the integer part is 0 or n = 1). Any other language uses en’s rule.
  • Locale of a request, resolved once: the user’s preference, the tenant’s default, Accept-Language by quality, then en, keeping the first that names a language the application supports.

The resolved tag is written to <html lang>, and the page, its flash messages and its Datastar patches are rendered with that request’s messages.

h := i18n.Middleware(i18n.Config{Supported: []string{"en", "af"}})(next)
// in a handler or a templ component, with the request's context:
i18n.T(ctx, "kit.paginator.showing", i18n.Args{"from": 1, "to": 10, "total": 55, "noun": "rye"})

Config also takes User and Tenant (functions of the request returning a tag, or "") and Bundle (the application’s catalogs over the kit’s). i18n.FromContext(ctx) gives the tag (en when nothing was resolved). Read application catalogs with i18n.LoadFS(fsys, "i18n") (files i18n/<tag>.json) and layer them with i18n.Kit().With(layer).

views.New takes the same configuration as an option, for an application that does not run the middleware:

views.New(a, views.WithLocales(i18n.Config{Supported: []string{"en", "af"}, Bundle: bundle}))

Views.Context(r) gives a handler the same context. Components get the request through the templ ctx; a label prop still wins over the catalog.

Every user-visible string of the kit’s templates (labels, aria-labels, titles, placeholders, dialog and pager text, label defaults, generic flash fallbacks) is a kit. message. A prop that sets a label wins over the catalog, so cancelLabel set on an alert dialog is kept in every language. English output is byte-for-byte what it was before the mechanism existed.

Texts that end up inside a Datastar expression (the password toggle’s labels, the strength words, the multi-select chips’ remove label) are quoted for JavaScript. A text with markup in it (the secret-reference help, the permission line of the permission-denied dialog) is escaped, and only the markup the template passes for named placeholders is spliced in.

Two texts are not translated: the logo’s aria-label (the brand) and the islands’ own scripts (the diagram says “Rendering diagram…” and CodeMirror has its own phrases).

  • validation.<CODE> returns the message for a validation code with the error’s parameters as arguments. It returns the error’s own English message when no catalog has the key or the text needs a parameter the error did not carry. Every code has a key in en and af: NOT_NULL, NOT_BLANK, TOO_SHORT, TOO_LONG, PATTERN, OUT_OF_RANGE, SIZE, NOT_ONE_OF. Rule violations carry no parameters yet, so a parameterised text falls back to the English message until they do.
  • In Go the helper is i18n.Validation(ctx, code, params, message).
  • A flash registered with the views is shown from flash.<key> (its summary) and flash.<key>.detail (its detail) when the catalogs have them, otherwise as registered.

The server writes plain values and the browser formats them with Intl, in the language of the page. There is no server-side locale data.

Component Server text Browser
Local time UTC instant toLocaleString(document.documentElement.lang)
Local number the plain decimal Intl.NumberFormat, keeping the value’s fraction digits
Local money 3132.48 ZAR currency style, the currency’s minor units unless digits is given
Local quantity 12.5 kg the number formatted, the unit symbol after it

The components take plain strings (an amount and a currency code, a value and a unit symbol), so they depend on no money or units type. A value that is not a plain decimal, or a currency that is not three capital letters, is refused at render time like every kit input. See money and units for the server-side types.

Both implementations assert that every key of a translated catalog exists in en with the same placeholders and plural shape, that every kit fixture renders with no missing message in en, af, af-ZA, zu and an unknown language, that every key the sources use is in en and every English kit key is used, and that plural objects have other. The Java build writes a shared conformance vector (lookup, fallback, placeholders, plurals, resolution, validation) that the Go package compares against. A report lists the English keys a catalog lacks; it is not a failure.