# Language

> Set the language of every Eunomia component and chart on the page in one call, or of a single element with its locale attribute.

Source: https://eunomia-ds.com/utilities/language/

The Eunomia components and charts speak English until told otherwise. `setDefaultLocale` sets the language of every one of them on the page in a single call; a `locale` attribute sets it for one element.

```js
import { setDefaultLocale } from "@eunomia/elements/locale.js";

setDefaultLocale("fr");
```

The same function is exposed by each package, and every copy reads and writes the same value: call it from any of them, it reaches the components and the charts alike.

| Import from | When |
| --- | --- |
| `@eunomia/elements/locale.js` | Your app uses the components |
| `@eunomia/charts/locale.js` | Your app uses the charts only |
| `@eunomia/core/locale.js` | A CDN page, or neither of the two |

None of these paths registers a component: it's safe to import anywhere, on the server too.

## What the language changes

A locale such as `"fr"`, `"fr-CA"` or `"en-GB"` drives two things:

- **The texts** the components write themselves: button labels, messages, the names screen readers announce. They exist in English (`en`) and French (`fr`).
- **The formats**: dates, times and numbers go through `Intl` in that locale, so the order of a date's fields, the month names and the 12- or 24-hour clock follow it.

A locale is matched exactly, else by its language, else falls back to English, never to an error:

| `locale` | Texts | Dates and numbers |
| --- | --- | --- |
| `"en"` | English | American |
| `"en-GB"` | English | British |
| `"fr"` | French | French |
| `"fr-CA"` | French | Canadian French |
| `"de"` | English | German |

## The whole page

Call `setDefaultLocale` once, at startup, before the components render: in your app's entry file, next to the components' imports. The page's own `<html lang>` isn't read, so pass it explicitly if that's your source:

```js
import { setDefaultLocale } from "@eunomia/elements/locale.js";

setDefaultLocale(document.documentElement.lang || "en");
```

`getDefaultLocale()` returns the current default, `"en"` until it's set.

A component already on the page reads the default when it renders, not when the default changes. To switch the language of a page live, set `locale` on the elements on screen, or render them again.

## One element

The `locale` attribute takes precedence over the default, for that element only. It's a regular attribute: changing it updates the element right away.

```html
<eun-date-picker label="Date" locale="en-GB"></eun-date-picker>
<eun-bar-chart label="Orders" locale="fr"></eun-bar-chart>
```

## Without a bundler (CDN)

The CDN files of the components and of the charts each carry their own copy of `@eunomia/core`, but they share the same default. On a CDN page, import `setDefaultLocale` from `@eunomia/core/dist/locale.js`, a single file with no imports, in a module script placed **before** the components' own: module scripts run in document order.

```html
<script type="module">
  import { setDefaultLocale } from "https://cdn.jsdelivr.net/npm/@eunomia/core/dist/locale.js";
  setDefaultLocale("fr");
</script>
<script
  type="module"
  src="https://cdn.jsdelivr.net/npm/@eunomia/elements/dist/cdn/components/eun-date-picker.js"
></script>
```

## See also

- [Dates](https://eunomia-ds.com/utilities/dates/): `formatDateRange` and the other date functions take a locale too
- [Date picker](https://eunomia-ds.com/components/date-picker/): a field whose segments and calendar follow the locale
