# Translate the grid and support right-to-left

> Run the grid in German with a bundled locale pack, add your own Arabic strings with plural forms, and lay it out right to left.

This recipe localizes an order screen for two markets: German, which ships as a locale pack, and Arabic, which you translate yourself and which needs a right-to-left layout. Strings, number and date formatting, and direction are three separate settings, so you can mix them freely.

## 1. German with a bundled pack

`@birchui/core` exports a locale pack per language: `localeEn`, `localeDe`, `localeFr`, `localeEs`, `localePt`, `localeJa` and `localeZh`. A pack is a `LocalePack`, an object with a `locale` code and a `text` map of message keys. Pass it as `localeText`, and set `locale` for number and date formatting:

```ts title="orders-grid.de.ts"
import { createGrid, localeDe } from '@birchui/core';

const grid = createGrid(document.querySelector<HTMLElement>('#orders')!, {
  columns,
  rows: orders,
  getRowId: (order) => order.id,
  localeText: localeDe,
  locale: 'de-DE',
  pagination: { pageSize: 50 },
});
```

`locale` is passed to `Intl` for `number` and `date` columns and to locale-aware sorting, so `1234.5` shows as `1.234,5`. Each pack is also published as its own entry point, such as `@birchui/core/locales/de`, if you want to load only the language in use.

## 2. One language for every widget

To set the language once for the whole app, including Grid Pro panels and other Birch UI products, use `setDefaultLocaleText()` before creating widgets:

```ts title="i18n.ts"
import { setDefaultLocaleText, localeDe } from '@birchui/core';

setDefaultLocaleText(localeDe);
```

A grid's own `localeText` wins over the default, key by key. Keys missing from both fall back to the English text built into the component.

## 3. Arabic strings with plural forms

There is no bundled Arabic pack, so write one. Messages are strings with `{name}` placeholders, or `PluralText` objects whose categories follow `Intl.PluralRules` for the pack's locale:

```ts title="locale-ar.ts"
import type { LocalePack } from '@birchui/core';

export const localeAr: LocalePack = {
  locale: 'ar',
  text: {
    'grid.noRows': 'لا توجد صفوف لعرضها',
    'grid.pager.page': 'الصفحة {page} من {pageCount}',
    'grid.pager.next': 'الصفحة التالية',
    'grid.pager.previous': 'الصفحة السابقة',
    'grid.filter.placeholder': 'تصفية…',
    'grid.filter.valueCount': {
      zero: 'لا توجد قيم',
      one: 'قيمة واحدة',
      two: 'قيمتان',
      few: '{count} قيم',
      many: '{count} قيمة',
      other: '{count} قيمة',
    },
    'grid.announce.copiedRows': { one: 'تم نسخ صف', two: 'تم نسخ صفين', other: 'تم نسخ {count} صفوف' },
  },
};
```

The keys are the ones the grid passes to its translator, such as `grid.noRows`, `grid.pager.page` and `grid.filter.placeholder`. The English pack, `localeEn.text`, lists every key with its default text, which makes a good translation template.

## 4. Right to left

```ts title="orders-grid.ar.ts"
import { createGrid } from '@birchui/core';
import { localeAr } from './locale-ar';

const grid = createGrid(document.querySelector<HTMLElement>('#orders')!, {
  columns,
  rows: orders,
  getRowId: (order) => order.id,
  localeText: localeAr,
  locale: 'ar-EG',
});

grid.getDirection();
```

Leave `rtl` unset and the grid reads the `dir` attribute of its container or the nearest ancestor, so `<html lang="ar" dir="rtl">` is enough. Set `rtl: true` or `rtl: false` to override the page. `getDirection()` returns the resolved `'ltr'` or `'rtl'`.

In right-to-left mode the layout is mirrored: columns run from right to left, and the left and right arrow keys follow the visual order.

## 5. Switch language at runtime

```ts
import { localeDe, localeEn } from '@birchui/core';
import { localeAr } from './locale-ar';

const packs = { en: localeEn, de: localeDe, ar: localeAr };

export function setLanguage(language: keyof typeof packs): void {
  document.documentElement.lang = language;
  document.documentElement.dir = language === 'ar' ? 'rtl' : 'ltr';
  grid.update({ localeText: packs[language], locale: language, rtl: language === 'ar' });
}
```

Changing `localeText`, `locale` or `rtl` with `update()` re-renders the grid without recreating it. Changing only the `dir` attribute on the page is not observed; pass `rtl` as well, as above.

## Your own strings

Use the same machinery for strings in your toolbar so one pack covers everything:

```ts
import { createTranslator, formatMessage, t } from '@birchui/core';

const translate = createTranslator({ 'orders.selected': { one: '{count} order selected', other: '{count} orders selected' } }, 'en');

translate('orders.selected', '{count} orders selected', { count: 3 });
t('grid.noRows', 'No rows to show');
formatMessage({ one: '{count} line', other: '{count} lines' }, 'en', { count: 1 });
```

`createTranslator(overrides, locale)` returns a function that looks in the overrides, then in the default pack from `setDefaultLocaleText()`, then uses the fallback. `t()` reads the default pack only. `resolveDirection(element, rtl)` applies the same direction rules as the grid to any element.

## Pitfalls

- **Column headers are yours.** `header` text is not translated by the grid; build column definitions from your own translations.
- **`format` functions are yours.** `locale` only affects columns without a `format`. Create `Intl.NumberFormat` instances per language in your own formatters.
- **Packs may be partial.** Missing keys show English. Check `localeEn.text` against your pack when upgrading.

## Related

- [Localization](https://docs.birchui.com/grid/localization.md), the reference for locale packs and message keys.
- [`@birchui/core` API reference](https://docs.birchui.com/api/core.md) for `LocalePack`, `PluralText` and the translation functions.
- [Accessibility](https://docs.birchui.com/grid/accessibility.md#announcements), since announcements are translated too.
- [Forms localization](https://docs.birchui.com/forms/i18n.md).
