Right-to-left example
Switch one grid between English, German, and Arabic, with translated labels and a layout direction that follows the locale.
The demo has three locales: English, German, and Arabic. Each locale sets the labels and the locale of the grid, the column headers, a hint text, and a text direction. Pick a language. The strings change, and the Arabic locale flips the grid to right-to-left.
pnpm dlx shadcn@latest add @gridcn/data-grid-i18n-demo
The labels prop
The grid keeps every built-in string in one DataGridLabels object with English defaults. The labels prop of DataGridProvider and DataGrid deep-merges over it. Only the keys you set change. A label that holds a value, such as searchMatches, is a function that receives the values.
Column headers are not labels. They belong to the column definitions, so build the columns from the locale and memoize them.
RTL
direction is "ltr" or "rtl". locale is a BCP 47 language tag. It sets the language of number and date formats and the sort order. Take both from the same locale record as the labels. The sample sets dir on the element around the grid, so the page around the grid mirrors with it, and the grid follows that dir:
import { useMemo } from "react";
import {
DataGridBody,
DataGridHeader,
DataGridProvider,
DataGridRoot,
defineColumns,
type DeepPartialLabels,
type GridDirection,
} from "@/components/data-grid/data-grid";
type Locale = "en" | "de" | "ar";
type Row = { id: string; name: string };
const TRANSLATIONS: Record<
Locale,
{
locale: string;
dir: GridDirection;
header: string;
labels: DeepPartialLabels;
}
> = {
en: { locale: "en-US", dir: "ltr", header: "Name", labels: {} },
de: {
locale: "de-DE",
dir: "ltr",
header: "Name",
labels: { grid: { emptyState: "Keine Zeilen" } },
},
ar: {
locale: "ar-EG",
dir: "rtl",
header: "الاسم",
labels: { grid: { emptyState: "لا صفوف" } },
},
};
export function LocaleGrid({ locale }: { locale: Locale }) {
const { dir, header, labels, locale: tag } = TRANSLATIONS[locale];
const columns = useMemo(
() => defineColumns<Row>()([{ id: "name", header, accessorKey: "name" }]),
[header],
);
return (
<div dir={dir}>
<DataGridProvider
defaultData={[]}
columns={columns}
labels={labels}
locale={tag}
>
<DataGridRoot className="h-48" aria-label="People">
<DataGridHeader />
<DataGridBody />
</DataGridRoot>
</DataGridProvider>
</div>
);
}With no rows, the grid shows the empty-state text of the locale. In rtl, the grid mirrors the column order, the pinned columns, the pointer math, and the placement of popups. The arrow keys move visually, so ArrowRight goes to the column on the right of the screen. If you omit direction, the grid follows the dir that it inherits from the page and updates when a dir attribute on the page changes. A direction prop on DataGridRoot overrides the inherited value.
Related
- Labels and i18n: the merge rules, the coverage by group, and how to wire an i18n library
- What mirrors in RTL: the full list
- Styling example: the look of the grid after a theme change