# x18n integration

Use `glosslint/x18n` when your project manages translations with [x18n](https://github.com/x18n-dev/x18n). The plugin checks your source locale's messages and the hard-coded strings x18n finds, and `toX18nGlossary` turns your glossary into x18n's `glossary` so translators and machine translation see the same terms. `@x18n/core` is an optional peer dependency.

## Setup

Load [`.agents/glossary.md`](https://glosslint.loadsheet.dev/guide/glossary) with `fromGlossary()`, pass it to the plugin from `glosslint/x18n`, and export it with `toX18nGlossary` in `x18n.config.ts`:

```ts [x18n.config.ts]
import { defineConfig } from "@x18n/core";
import { fromGlossary, toX18nGlossary } from "glosslint";
import glosslint from "glosslint/x18n";

const corpus = fromGlossary();

export default defineConfig({
  plugins: [glosslint({ corpus })],
  glossary: toX18nGlossary(corpus),
});
```

If you keep your glossary at another path, pass it to `fromGlossary(new URL("./docs/glossary.md", import.meta.url))`. Per-locale translations and forms go in the glossary's [`Translations` and `Forms`](https://glosslint.loadsheet.dev/guide/glossary#translations-and-forms) fields:

```md [.agents/glossary.md]
## Dock
What: A powered stand where one bike is locked.
Translations: de: Dock
Forms:
- de: Dockingstation, Dockingstationen
```

## Options

| Option         | Default                     | What it does                                                                                           |
| -------------- | --------------------------- | ------------------------------------------------------------------------------------------------------ |
| `glossary`     | `.agents/glossary.md`       | The glossary file to read, as a path from the current folder or a URL.                                 |
| `corpus`       | none                        | A corpus to use instead of a file, such as one from `defineCorpus`. Not with `glossary`.               |
| `sourceLocale` | the project's source locale | The locale whose messages are checked.                                                                 |
| `kindOf`       | none                        | Decides a message's kind before glosslint does. See [Casing rules](https://glosslint.loadsheet.dev/guide/casing).                     |

With no `glossary` or `corpus`, `glosslint()` reads `.agents/glossary.md` once, when the config loads. The setup above passes `corpus` because `toX18nGlossary` needs the same terms. Passing both throws [`GLOSSLINT_C012`](https://glosslint.loadsheet.dev/guide/config-errors).

## Checks

The plugin registers three checks in x18n:

| Check             | Default severity | Reports           |
| ----------------- | ---------------- | ----------------- |
| `glosslint-term`  | error            | `GLOSSLINT_TERM`  |
| `glosslint-avoid` | warning          | `GLOSSLINT_AVOID` |
| `glosslint-case`  | error            | `GLOSSLINT_CASE`  |

To change a check's severity, override it under `checks` in `x18n.config.ts`:

```ts [x18n.config.ts]
export default defineConfig({
  plugins: [glosslint({ corpus })],
  glossary: toX18nGlossary(corpus),
  checks: {
    "glosslint-avoid": "error",
  },
});
```

## Message kinds and fixes

A message's key decides whether its value is a label, a header, or a sentence, following the rules in [Casing rules](https://glosslint.loadsheet.dev/guide/casing). If the key does not place the message, glosslint falls back to the text's shape. Pass `kindOf` to decide specific keys or files yourself before glosslint runs.

Fixes depend on the finding kind and how the message's kind was decided:

- **Safe fixes.** Spelling and casing findings in messages carry a safe fix that `x18n check --fix` applies, except `context` terms in messages placed only by shape.
- **Quick fixes.** A `context` casing fix in a message placed only by its shape is offered as a quick fix you pick yourself, rather than a safe fix, because the shape guess might be wrong.
- **No fix.** Avoid words and hard-coded strings get no fix. Avoid words need a person to verify the meaning first, and x18n does not edit source files for hard-coded strings outside extraction.

When one message has several safe fixes, each safe fix applies all of them at once, and each finding also keeps a quick fix that changes only its own span.

## Exporting your corpus to x18n's glossary

`toX18nGlossary(corpus)` maps each of your own terms to an x18n glossary entry. The terms that `Presets: tech, inclusive` or `extends: [tech]` add are left out, so translators get your glossary and not hundreds of product names; a term of your own with a preset term's name is kept:

- `term`, `translations`, and `forms` come from the glossary's `Translations` and `Forms` fields or from [`defineCorpus`](https://glosslint.loadsheet.dev/guide/corpus#typescript-only-term-fields). A locale that says `keep` or `keep (gloss)` gets the term itself as its translation, a bare `translate` gets none, and `` `/pattern/` `` forms and `Hints` are left out, since x18n's glossary has no place for them.
- `note` combines the term's `description` and `nuance`. Pass `{ note: "short" }` for the `description` alone, the glossary's `What`, when translators want a note that fits on one line. A gloss is added after it as `fr: keep, gloss as "couche"`.
- `doNotTranslate: true` is set on every `exact` term whose `Translations` items all keep the term, or that has none.

For example, given `Dock` (with German `translations` and `forms`) and `eBike` (`case: "exact"` with no translations), `toX18nGlossary(corpus)` produces:

```ts
[
  {
    term: "Dock",
    translations: { de: "Dock" },
    forms: { de: ["Dockingstation", "Dockingstationen"] },
    note: "A powered stand where one bike is locked. A station is a group of docks.",
  },
  {
    term: "eBike",
    doNotTranslate: true,
  },
]
```

See [API reference](https://glosslint.loadsheet.dev/guide/api) for the `toX18nGlossary` function and `X18nGlossaryEntry` type.

## Merging into a shared glossary YAML

When translators keep an x18n glossary in YAML, such as a `glossary.yml` shared by several repositories, `glosslint export --merge` adds your glossary to it without dropping their work:

```sh
npx glosslint export --note short --merge ../translations/glossary.yml
```

- An entry matches one in the file when they share the term or one of its source-locale `forms`, in any case and with hyphens and spaces ignored. "Prerendering" matches `prerendering`, and a term `Prerender` with the form `prerendering` matches it too. Other punctuation counts, so `C#` doesn't match `C++`. A term goes to the entry with the same term before any form is compared, so `Render` with the form `Rendering` goes to `Render` when the file has both. A term that matches more than one entry, or two terms that match the same entry, stop the merge before anything is written, and the command names them. `--source-locale` names the locale of those forms (default `en`).
- A matched entry keeps its `term`, its `translations` and every field glosslint does not write. glosslint sets `note`, adds translations and forms for locales the entry lacks, and sets `doNotTranslate` only on entries with no translations. A term with `Translations` whose entry has `doNotTranslate: true` stops the merge, since x18n would never translate it, and the command names the entry and the flag to remove. A locale that keeps the term agrees with the flag, so it is skipped instead. A translation the file already has wins over the glossary's, and the command lists each one it kept.
- Terms with no match are added at the end. Comments, entry order and the formatting of the entries it does not touch stay as they are.

Without `--merge`, `glosslint export` prints the entries as YAML. See [Command line](https://glosslint.loadsheet.dev/integrations/cli#export).

## Exporting a locale to diglot

[diglot](https://github.com/danielroe/diglot) keeps one glossary per locale, such as `fr/glossary.yml`, whose entries replace the shared x18n entries with the same term and can say more: a decision, the patterns a translation may also be written as, and a `match` pattern for the term in the source. `glosslint export --diglot <locale>` writes that file from the locale's items in `Translations`, `Forms` and `Hints`:

```sh
npx glosslint export --diglot fr --merge ../translations/fr/glossary.yml
```

Only the terms with an item for the locale in `Translations` are exported, so a `Case: exact` term without one stays in the shared x18n file. Given the example glossary, where `Dock` has `fr: borne`, the form `fr: bornes` and the hint `fr: Feminine: la borne.`, and `Week Pass (WP)` has `fr: keep (abonnement hebdomadaire)`, `glosslint export --diglot fr` prints:

```yaml
terms:
  - term: Week Pass
    match: \b(?:weekly[- ]?tickets|weekly[- ]?ticket|Week[- ]?Passing|Week[- ]?Passed|Week[- ]?Passes|Week[- ]?Pass|WPs|WP)\b
    decision: keep-gloss
    gloss: abonnement hebdomadaire
  - term: Dock
    match: \b(?:Docking|Docked|Docks|racks|bays|Dock|rack|bay)\b
    note: "Feminine: la borne."
    decision: translate
    target: borne
    accept:
      - bornes
```

- `decision` is `keep`, `keep-gloss` with the `gloss`, or `translate` with the translation as `target`, or no `target` for a bare `translate`.
- `accept` lists the locale's `Forms`, escaped, and its `` `/pattern/` `` forms as they are. A form in backticks loses them before it is escaped. It is written only for `translate`.
- `note` is the locale's `Hints` item.
- `match` finds the term in the source: the term, its abbreviations, its source-locale `Forms` and its `Avoid` words, with their plurals and `ed` and `ing` forms, longest first, as whole words. A space or hyphen inside a word is optional.
- `caseSensitive: true` is set on `Case: exact` terms only.

`--merge <file>` writes into the locale's diglot glossary in place, matching entries as the x18n merge does, by term and then by source-locale form, and keeping the file's spelling of `term`. glosslint owns `decision`, `target`, `gloss` and `accept`, and `note` when the term has a hint. It adds `accept` patterns the entry lacks and removes none. The file keeps its own `match` and `caseSensitive`, or its lack of them, its note when the term has no hint, every other key, `style`, comments and order. Entries the glossary doesn't have stay, and new terms are added at the end with the generated `match` and `caseSensitive`. A file with CRLF line endings keeps them. A second merge changes no byte. The command prints what changed:

```txt
fr/glossary.yml: 2 terms merged, 1 added, 1 updated, 0 unchanged, 1 kept `match`.
+ Week Pass
~ dock: note, decision (was keep), target, accept
```

`` kept `match` `` counts the entries whose own `match` differs from the one glosslint generates; the file's stays. A missing locale or one that is not a BCP 47 tag, a locale no term has, a merge file that is missing, isn't valid YAML or is an x18n list, a term that matches two entries, or two terms that match one entry exit `2` and write nothing.

<!-- vitehub-docs:section-links -->
## In this section

- [Overview](https://glosslint.loadsheet.dev/raw/integrations.md)
- [ESLint](https://glosslint.loadsheet.dev/raw/integrations/eslint.md)
- [Nuxt](https://glosslint.loadsheet.dev/raw/integrations/nuxt.md)
- [Oxlint](https://glosslint.loadsheet.dev/raw/integrations/oxlint.md)
- [x18n](https://glosslint.loadsheet.dev/raw/integrations/x18n.md) (this page)
- [Command line](https://glosslint.loadsheet.dev/raw/integrations/cli.md)

Next: [Command line](https://glosslint.loadsheet.dev/raw/integrations/cli.md)

All pages: [llms.txt](https://glosslint.loadsheet.dev/llms.txt)
