Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/__snapshots__/Range.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ exports[`should match the snapshot 1`] = `
class="rdp-root"
data-mode="range"
id="test"
lang="en-US"
>
<div
class="rdp-months"
Expand Down Expand Up @@ -646,6 +647,7 @@ exports[`when a day in the range is clicked when the day is clicked again when a
class="rdp-root"
data-mode="range"
id="test"
lang="en-US"
>
<div
class="rdp-months"
Expand Down
1 change: 1 addition & 0 deletions examples/__snapshots__/StylingCssModules.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ exports[`should match the snapshot 1`] = `
<div
class="rdp-root"
data-mode="single"
lang="en-US"
>
<div
class="rdp-months"
Expand Down
17 changes: 17 additions & 0 deletions src/DayPicker.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ test("forward aria attributes to the root element", () => {
expect(dayPicker()).toHaveAttribute("aria-labelledby", "calendar-heading");
});

describe("when rendering the root language tag", () => {
test("sets the default locale code when lang is not provided", () => {
render(<DayPicker data-testid={testId} />);
expect(dayPicker()).toHaveAttribute("lang", defaultLocale.code);
});

test("uses the locale code when locale is provided", () => {
render(<DayPicker data-testid={testId} locale={ja} />);
expect(dayPicker()).toHaveAttribute("lang", ja.code);
});

test("prefers lang over locale code when both are provided", () => {
render(<DayPicker data-testid={testId} locale={ja} lang="ar" />);
expect(dayPicker()).toHaveAttribute("lang", "ar");
});
});

test("use custom components", () => {
render(
<DayPicker
Expand Down
2 changes: 1 addition & 1 deletion src/DayPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ export function DayPicker(initialProps: DayPickerProps) {
style={style}
dir={props.dir}
id={props.id}
lang={props.lang}
lang={props.lang ?? locale.code}
nonce={props.nonce}
title={props.title}
role={props.role}
Expand Down
7 changes: 6 additions & 1 deletion src/types/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,12 @@ export interface PropsBase {
nonce?: HTMLDivElement["nonce"];
/** Add a `title` attribute to the container element. */
title?: HTMLDivElement["title"];
/** Add the language tag to the container element. */
/**
* Add the language tag to the container element.
*
* When omitted, DayPicker uses the active locale code (`locale.code`).
* Set this prop to override the language tag.
*/
lang?: HTMLDivElement["lang"];
/**
* The locale object used to localize dates. Pass a locale from
Expand Down
20 changes: 20 additions & 0 deletions website/docs/localization/changing-locale.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,26 @@ export function Spanish() {
<Examples.Spanish />
</BrowserWindow>

## Language tag on the root element

DayPicker sets the root element `lang` attribute from the active locale code (`locale.code`).

If you need a different language tag, pass the `lang` prop explicitly.

```tsx
import { arSA } from "react-day-picker/locale";

<DayPicker locale={arSA} />;
```

You can target locale-specific styles with the CSS `:lang(...)` selector:

```css
.rdp-root:lang(ar) {
font-family: "Noto Naskh Arabic", serif;
}
```

## Numerals

Use the `numerals` prop to select the numbering system used by labels and formatters (for example, `latn`, `arab`, `thai`). Locale label translations also respect the numerals you choose.
Expand Down