Localization and RTL
Translate every grid string with localeText or a locale pack, format values for a locale, and lay the grid out right to left.
Every piece of text the grid renders or announces, from menu items and filter labels to the pager and screen reader announcements, goes through one translator. You can replace any of it per grid with localeText, for every widget on the page with setDefaultLocaleText(), or with a ready-made locale pack. Values are formatted with Intl for the locale option, and rtl mirrors the layout.
Translating one grid#
localeText takes a map of message keys to text. Keys you leave out keep their English default.
import { createGrid } from '@birchui/core';
const grid = createGrid<Order>(el, {
columns,
rows: orders,
locale: 'de-DE',
localeText: {
'grid.noRows': 'Keine Aufträge gefunden',
'grid.filter.placeholder': 'Filtern…',
'grid.pager.page': 'Seite {page} von {pageCount}',
'grid.menu.showAllColumns': {
one: 'Ausgeblendete Spalte anzeigen',
other: 'Alle Spalten anzeigen ({count} ausgeblendet)',
},
},
});{name}placeholders are replaced with values such as{column},{count}or{page}. Unknown placeholders are left as written.- A message can be a plural object (
zero,one,two,few,many,other). The category is chosen withIntl.PluralRulesfor the pack's locale from thecountparameter;otheris required and used as the fallback. grid.update({ localeText, locale })switches language at runtime without recreating the grid.
Locale packs#
A locale pack is { locale, text }. @birchui/core exports packs for English, German, French, Spanish, Portuguese, Japanese and Chinese, each with the strings of every Birch UI product, so one pack translates the grid, scheduler, calendar, charts and the rest:
import { createGrid, localeDe } from '@birchui/core';
const grid = createGrid<Order>(el, { columns, rows: orders, locale: 'de-DE', localeText: localeDe });Each pack is also a separate entry point, so you can load it on demand and ship only the languages you use:
const { localeJa } = await import('@birchui/core/locales/ja');
grid.update({ locale: 'ja-JP', localeText: localeJa });| Export | Entry point | Locale |
|---|---|---|
localeEn |
@birchui/core/locales/en |
en |
localeDe |
@birchui/core/locales/de |
de |
localeFr |
@birchui/core/locales/fr |
fr |
localeEs |
@birchui/core/locales/es |
es |
localePt |
@birchui/core/locales/pt |
pt |
localeJa |
@birchui/core/locales/ja |
ja |
localeZh |
@birchui/core/locales/zh |
zh |
To adjust a pack, spread it: { locale: 'de', text: { ...localeDe.text, 'grid.noRows': 'Keine Daten' } }.
A default for the whole page#
setDefaultLocaleText(pack) sets the text used by every widget that does not override a key with its own localeText. Call it once at startup, before creating widgets:
import { setDefaultLocaleText, localeFr } from '@birchui/core';
setDefaultLocaleText(localeFr);The lookup order for every message is: the widget's localeText, then the default pack, then the built-in English text.
Formatting values#
locale is a BCP 47 tag passed to Intl.NumberFormat and Intl.DateTimeFormat for number and date columns, and to the plural rules of messages. Without it, the browser's locale is used. Columns with a format function format themselves, so build those formatters with the same locale:
const eur = new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' });
const columns: ColumnDef<Order>[] = [{ field: 'total', type: 'number', format: (value) => eur.format(Number(value)) }];Right-to-left layout#
The grid lays itself out right to left when its container, or any ancestor, has dir="rtl". Set rtl: true or rtl: false to override what the page says:
const grid = createGrid<Order>(el, { columns, rows: orders, locale: 'ar-EG', rtl: true });
grid.getDirection();In RTL the first column is on the right, pinned: 'left' pins to the inline start (the right edge), horizontal scrolling and Arrow Left and Arrow Right follow the reading direction, and the root element gets dir="rtl". update({ rtl }) switches direction at runtime.
Translating your own UI#
The same helpers are exported for your toolbars and status lines, so all text follows one pack:
| Export | Purpose |
|---|---|
t(key, fallback, params?) |
Look up a key in the default pack |
createTranslator(localeText?, locale?) |
A Translate function with its own overrides on top of the default pack |
formatMessage(message, locale, params?) |
Fill placeholders and pick the plural form of one message |
setDefaultLocaleText(pack) / getDefaultLocaleText() |
Set or read the page-wide pack |
resolveDirection(element, rtl?) |
'ltr' or 'rtl' from an explicit flag or the nearest dir attribute |
grid.translate(key, fallback, params?) |
Translate with the grid's own localeText |
import { t } from '@birchui/core';
status.textContent = t('orders.selected', '{count} orders selected', { count: grid.getSelectedIds().length });Message keys#
The grid uses these keys. Grid Pro, the scheduler and the other products use their own prefixes (gridPro., scheduler., calendar. and so on) in the same packs.
| Key | English text |
|---|---|
grid.announce.copiedCell |
Copied cell |
grid.announce.filterApplied |
Filter applied to {column} |
grid.announce.filterCleared |
Filter cleared for {column} |
grid.announce.sortCleared |
Sort cleared for {column} |
grid.announce.sortedAsc |
Sorted by {column}, ascending |
grid.announce.sortedDesc |
Sorted by {column}, descending |
grid.contextMenu.copyCell |
Copy cell |
grid.contextMenu.copyRow |
Copy row |
grid.contextMenu.copySelectedRows |
Copy selected rows |
grid.contextMenu.copyWithHeaders |
Copy with headers |
grid.contextMenu.exportCsv |
Export CSV |
grid.filter.all |
All |
grid.filter.and |
AND |
grid.filter.apply |
Apply |
grid.filter.blanks |
(Blanks) |
grid.filter.close |
Close |
grid.filter.columnLabel |
Filter {column} |
grid.filter.combine |
Combine conditions with |
grid.filter.condition |
Condition {index} |
grid.filter.enterBoth |
Enter both values |
grid.filter.enterDate |
Enter a valid date |
grid.filter.enterNumber |
Enter a number |
grid.filter.from |
From |
grid.filter.matchCount |
{matches} of {count} values |
grid.filter.noMatches |
No matching values |
grid.filter.no |
No |
grid.filter.operator |
Operator |
grid.filter.or |
OR |
grid.filter.placeholder |
Filter… |
grid.filter.reset |
Reset |
grid.filter.search |
Search |
grid.filter.searchValues |
Search {column} values |
grid.filter.selectAll |
Select all |
grid.filter.to |
To |
grid.filter.values |
{column} values |
grid.filter.value |
Value |
grid.filter.yes |
Yes |
grid.group.collapse |
Collapse {group} |
grid.group.expand |
Expand {group} |
grid.group.unnamed |
column group |
grid.header.columnMenu |
{column} column menu |
grid.menu.autosizeAll |
Autosize all columns |
grid.menu.autosizeColumn |
Autosize this column |
grid.menu.clearSort |
Clear sort |
grid.menu.decreaseWidth |
Decrease width |
grid.menu.filter |
Filter… |
grid.menu.hideColumn |
Hide column |
grid.menu.increaseWidth |
Increase width |
grid.menu.moveFirst |
Move to first |
grid.menu.moveLast |
Move to last |
grid.menu.moveLeft |
Move left |
grid.menu.moveRight |
Move right |
grid.menu.pinLeft |
Pin left |
grid.menu.pinRight |
Pin right |
grid.menu.showAllColumns |
Show all columns ({count} hidden) |
grid.menu.sortAsc |
Sort ascending |
grid.menu.sortDesc |
Sort descending |
grid.menu.unpin |
Unpin |
grid.noRows |
No rows to show |
grid.pager.first |
First page |
grid.pager.label |
Pagination |
grid.pager.last |
Last page |
grid.pager.next |
Next page |
grid.pager.page |
Page {page} of {pageCount} |
grid.pager.previous |
Previous page |
grid.pager.range |
{from}–{to} of {total} |
grid.selection.selectAll |
Select all rows |
grid.selection.selectRow |
Select row {row} |
grid.value.false |
No |
grid.value.true |
Yes |
Related#
- Accessibility for the announcements these strings feed.
- Forms localization for the form renderer's own message catalogue.
- Time zones in the scheduler.