Localization and Text (Client-Side)
Serenity uses a global localization table to resolve localized strings on the client. Text keys are looked up in this table, which is populated from server-side localization scripts.
The Localization Table
Localized strings are stored in a global table keyed by text keys (e.g. "Dialogs.YesButton"). The table is populated from server-side localization scripts and can be extended at runtime with addLocalText.
Resolving Text
localText
localText(key, defaultText?) returns the localized string for a key, falling back to defaultText or the key itself:
import { localText } from "@serenity-is/corelib";
localText("Dialogs.YesButton"); // "Yes" if registered, otherwise "Dialogs.YesButton"
localText("Missing.Key", "Fallback"); // "Fallback"
text
text is an alias for localText:
import { text } from "@serenity-is/corelib";
text("Dialogs.YesButton");
tryGetText
tryGetText(key) returns the localized string or undefined (unlike localText, it does not fall back to the key):
import { tryGetText } from "@serenity-is/corelib";
const value = tryGetText("Some.Key"); // string | undefined
Adding Text at Runtime
addLocalText adds entries to the localization table:
import { addLocalText } from "@serenity-is/corelib";
// Single entry
addLocalText("Db.Northwind.CustomerName", "Customer Name");
// Nested object (flattened with dots)
addLocalText({ Customer: { Name: "Name" } }, "Db.Northwind.");
// registers "Db.Northwind.Customer.Name"
Text Proxies
Generated text classes (in the module barrels or ServerTypes/Texts.ts) use proxyTexts to provide strongly-typed access to text keys. For example:
import { UserPermissionDialogTexts } from "../../ServerTypes/Administration";
UserPermissionDialogTexts.DialogTitle; // resolves the text key
The proxy supports .asKey() (returns the key) and .asTry() (returns undefined when missing).
Where the Generated Texts Live
The server typings generator writes one file per [NestedLocalTexts] class, in the folder of its namespace (e.g.
Administration/UserDialogTexts.ts, re-exported by the module barrel), or to ServerTypes/Texts/<Name>.ts for the root
namespace, and a <Module>DbTexts file for the Db.<Module>. keys that no class covers. This lets the bundler put each set
of texts into the chunk of the pages that use it, instead of the whole texts tree into the chunk of any page that imports
ServerTypes/Texts (e.g. the login page).
The ServerTypings:TextsMode option of sergen.json decides what ServerTypes/Texts.ts contains: Compat (the default) keeps
it re-exporting everything, while Remaining generates it only for a Texts class, with the keys that no other file covers.
The defaults@10.8.0 package of the templates selects Remaining, so import the texts from the module barrels or from
ServerTypes/Texts/<Name> rather than from ServerTypes/Texts when you upgrade.
Breaking change: with
Remaining, the texts that used to be re-exported byServerTypes/Textsmay now live in another generated file. The module barrels re-export them, so an import from a module barrel keeps working.
Formatting Dates and Numbers
The server sends the names of the months and the days (monthNames, shortMonthNames, dayNames, shortDayNames,
minimizedDayNames), the AM / PM designators, the time separator and the first day of the week of its current culture in
ScriptCulture. formatDate therefore writes localized names for MMM, MMMM, ddd and dddd instead of the English
ones, and Culture.firstDayOfWeek is the one of the culture (e.g. the This Week range of a date range editor starts on
Monday where the week does).
For languages whose month names take another form next to a day number, e.g. Polish "17 listopada", the genitive names are
sent too (monthGenitiveNames, shortMonthGenitiveNames, only when they differ), and formatDate uses them when its format
has a day number (d or dd), as .NET does.
Localization in Widgets
Widgets and grids use localized text for titles, buttons, and messages. For example, EntityGrid resolves its title from the entity's plural display name:
protected getDisplayName(): string {
return localText(this.getLocalTextDbPrefix() + 'EntityPlural', this.getEntityType());
}
See Also
- localText (API reference) — resolve a localized string.
- tryGetText (API reference) — resolve without key fallback.
- addLocalText (API reference) — add text at runtime.
- proxyTexts (API reference) — text proxies.
- Localization — server-side localization.
- Frontend Framework Overview — the three client-side packages and how they fit together.