Thunderbird/Betterbird add-on (Manifest V3): typing inside a quote splits the quote and inserts your own text with a marker such as [OH]:. - English and German user interface - Licensed under the Mozilla Public License 2.0 - English documentation, JSDoc lint gate, 87 tests (node:test + jsdom)
98 lines
4.7 KiB
Markdown
98 lines
4.7 KiB
Markdown
# Component: Translations (i18n)
|
||
|
||
**Files:** `src/_locales/en/messages.json`, `src/_locales/de/messages.json`, `src/lib/i18n.js`;
|
||
checked by `test/i18n.test.js`
|
||
|
||
**What:** The user interface is available in English and German through the standard WebExtension
|
||
`i18n` API. `manifest.json` sets `"default_locale": "en"`. Thunderbird picks the messages that match
|
||
its user interface language and falls back to English for any other language.
|
||
|
||
## Message files
|
||
|
||
Each locale has one `messages.json`. Every entry has a key, the text and a description (the
|
||
`example` value is shortened here):
|
||
|
||
```json
|
||
{
|
||
"profileOwnSettings": {
|
||
"message": "$IDENTITY$ – own settings",
|
||
"description": "Options page: entry of the identity drop-down list for an identity with its own settings.",
|
||
"placeholders": {
|
||
"identity": {
|
||
"content": "$1",
|
||
"example": "Jane Doe <…>"
|
||
}
|
||
}
|
||
}
|
||
}
|
||
```
|
||
|
||
- **Keys:** letters, digits, `_` and `@` only; the project uses camelCase.
|
||
- **Descriptions** are notes for translators. They are written in English and are identical in every
|
||
locale.
|
||
- **Placeholders** (`$NAME$`) are defined under `placeholders`; `$1` … `$9` take the substitutions
|
||
passed to `getMessage(key, [values])`. All locales define the same placeholders.
|
||
|
||
## Where messages are used
|
||
|
||
| Where | How | Example |
|
||
| --------------- | ----------------------------------------------------------- | ------------------------------------ |
|
||
| `manifest.json` | `__MSG_key__` | `"name": "__MSG_extensionName__"` |
|
||
| `background.js` | `msg(key)` → `i18n.getMessage(key)` | badge `badgeOn` / `badgeOff` |
|
||
| `options.html` | `data-i18n` and `data-i18n-aria-label` attributes | `<h2 data-i18n="markerHeading">` |
|
||
| `options.js` | `msg(key, substitutions)` for texts that change at run time | `msg("profileOwnSettings", [label])` |
|
||
|
||
The compose script has no texts of its own and does not use the i18n API.
|
||
|
||
## `localizeDocument()` (`src/lib/i18n.js`)
|
||
|
||
`localizeDocument(doc, getMessage)` walks the document once when the options page loads:
|
||
|
||
| Attribute | Effect |
|
||
| ---------------------- | ------------------------------------ |
|
||
| `data-i18n="key"` | replaces the element's `textContent` |
|
||
| `data-i18n-aria-label` | sets the `aria-label` attribute |
|
||
| `data-i18n-title` | sets the `title` attribute |
|
||
|
||
- The English text in the HTML stays as fallback when a message is missing (`getMessage()` returns an
|
||
empty string or no string at all).
|
||
- Elements with `data-i18n` must contain text only, because their whole content is replaced. Wrap
|
||
the text in a `<span data-i18n="…">` when the element also holds other elements (for example a
|
||
checkbox label).
|
||
- The function returns the number of replaced texts and attributes.
|
||
- Translations are always set as text (`textContent`, `setAttribute`, `new Option()`), never as
|
||
markup. A translation therefore cannot inject HTML (see
|
||
[decision 0005](../decisions/0005-no-markup-sinks.md)).
|
||
|
||
`options.js` calls it first thing, together with `document.documentElement.lang =
|
||
i18n.getUILanguage()`.
|
||
|
||
## Adding a string
|
||
|
||
1. Add the key to `src/_locales/en/messages.json` with the English `message` and an English
|
||
`description` that says where the text appears (and any limits, such as the length of a badge).
|
||
2. Add the same key to `src/_locales/de/messages.json` with the German `message`, the **same**
|
||
`description` and the same `placeholders`.
|
||
3. Use it:
|
||
- in `options.html`: `data-i18n="key"` (or `data-i18n-aria-label`, `data-i18n-title`) with the
|
||
English text as fallback content;
|
||
- in a script: `msg("key")` with the key as a **literal string**, so the tests can find it;
|
||
- in `manifest.json`: `__MSG_key__`.
|
||
4. Run `npm test`.
|
||
|
||
`test/i18n.test.js` checks that
|
||
|
||
- `en` and `de` exist and have exactly the same keys,
|
||
- every message has a text and the English description, and placeholders match across locales,
|
||
- every key used in `options.html`, in `msg()`/`getMessage()` calls and in `manifest.json` exists,
|
||
- every message is used somewhere (remove unused keys),
|
||
- elements with `data-i18n` contain no child elements,
|
||
- the options page is fully translated in both languages (rendered in jsdom).
|
||
|
||
## Adding a language
|
||
|
||
Create `src/_locales/<code>/messages.json` with all keys of the English file, translated messages and
|
||
unchanged descriptions and placeholders. The key, description and placeholder checks run for every
|
||
locale folder automatically; add the new code to the list in the test "options page is fully
|
||
translated" as well, so the rendered page is checked too.
|