147 lines
6.6 KiB
Markdown
147 lines
6.6 KiB
Markdown
# String packs
|
|
|
|
A string pack replaces the wrapped site's own UI strings with data: it adds a language the site
|
|
does not offer, fixes its translation, or rewords it. This is phase 1: the formats, the validator,
|
|
the application engine, the hook interface and `waybetter strings check`. The Store kind, the
|
|
install screen and the settings UI are not built yet.
|
|
|
|
## Rules
|
|
|
|
1. **Idempotent.** The result is a pure function of (the site's own values, the ordered pack list).
|
|
Applying twice equals applying once. Packs apply in the listed order; a later pack wins per key.
|
|
2. **The site's own language is the fallback.** A pack patches on top of whatever language the site
|
|
shows. A key no pack changes keeps exactly the site's value.
|
|
3. **An inventory of the site's known keys.** A pack can change only inventory keys. An unknown key
|
|
is reported and never applied.
|
|
4. **Packs are data.** Plain text in an ICU MessageFormat subset. Never HTML, never code.
|
|
|
|
## The pack
|
|
|
|
```json
|
|
{
|
|
"format": 1,
|
|
"site": "x",
|
|
"catalog": "web",
|
|
"language": "en-x-pirate",
|
|
"name": "Pirate",
|
|
"authors": ["Someone"],
|
|
"base": "en",
|
|
"strings": {
|
|
"a0000001": "Home port",
|
|
"b0000002": "{count, plural, one {# sighting} other {# sightings}}"
|
|
}
|
|
}
|
|
```
|
|
|
|
- `site` and `catalog` name the inventory the pack is for. One site can have several catalogs
|
|
(different builds of the site with different key spaces); each has its own inventory and packs.
|
|
- `language` is a BCP 47 tag; a private-use tag is fine. `base` is the language whose plural rules
|
|
the pack follows when the runtime does not know `language`; without either, every number takes
|
|
the `other` form.
|
|
- `strings` maps a key id to text. The ids are the inventory's; a pack never carries the site's own
|
|
text.
|
|
|
|
A value may use `{name}`, `{name, plural, one {…} other {…}}` (with `#` for the number and `=0`,
|
|
`=1` exact forms) and `{name, select, a {…} other {…}}`. A value is refused, and the site's text
|
|
stays, when:
|
|
|
|
- it names a parameter the original does not take;
|
|
- it pluralizes a parameter the original does not pluralize, or selects on one the original does
|
|
not select on (the original's plural parameters are the ones known to be numbers);
|
|
- a plural or select has no `other`;
|
|
- it uses a number or date format, `selectordinal` or a plural offset;
|
|
- it contains `<` or `>` (unless the inventory gives the key tag slots; then exactly those tags);
|
|
- it contains control or bidi-override characters, is empty or is longer than 2000 characters;
|
|
- the key's kind cannot be replaced by data yet (`rich`, `format`).
|
|
|
|
A parameter is inserted the way string concatenation would insert it (`String(value)`).
|
|
|
|
## The inventory
|
|
|
|
```json
|
|
{
|
|
"format": 1, "site": "x", "catalog": "web", "base": "en", "build": "…",
|
|
"entries": {
|
|
"a0000001": { "kind": "text", "hash": "0k3j…" },
|
|
"b0000002": { "kind": "message", "params": ["count"], "plural": ["count"], "hash": "…" },
|
|
"c0000003": { "kind": "rich", "group": "rich", "hash": "…" }
|
|
}
|
|
}
|
|
```
|
|
|
|
| Kind | The site's value | A pack can replace it |
|
|
|---|---|---|
|
|
| `text` | a string | yes |
|
|
| `message` | a function of parameters that returns a string | yes |
|
|
| `rich` | text with element slots | not yet |
|
|
| `format` | a compiled number, date or currency formatter | no |
|
|
|
|
`hash` is a hash of the base-language value (`hashText`), for the drift report; the inventory holds
|
|
no text of the site. `group` names the part of the catalog a key arrives with (default `main`).
|
|
|
|
`driftReport(inventory, observed, loadedGroups)` compares an inventory with a newer build: `added`,
|
|
`reworded` (the hash changed), `reshaped` (kind or parameters changed) and `vanished`. A key is
|
|
called vanished only when its group's base catalog was actually read.
|
|
|
|
`packInventory(inventory)` is the form a page runtime ships: ids and shapes, no hashes.
|
|
|
|
## The engine
|
|
|
|
```ts
|
|
const engine = createStringEngine(inventory, [compilePack(packA, inventory), compilePack(packB, inventory)]);
|
|
engine.resolve(id, siteValue); // packB's value, else packA's, else exactly siteValue
|
|
```
|
|
|
|
`resolve` returns a string for a `text` key and a function with the original's call shape for a
|
|
`message` key (closures over the parsed message; nothing is evaluated). Feeding a value the engine
|
|
produced back in returns the same value. It keeps the site's own value of every key it changed
|
|
(`engine.originals()`), which is what a restore needs; `applyPacks` and `restoreCatalog` do both
|
|
for a whole catalog object without changing it. A live value whose kind differs from the
|
|
inventory's is left alone and listed in `engine.stats().mismatched`; a key the site has and the
|
|
inventory lacks is listed in `unlisted`.
|
|
|
|
## Hooking a site
|
|
|
|
How a site hands its strings to its own code is that site's implementation detail, so the hook is
|
|
an adapter the app supplies:
|
|
|
|
```ts
|
|
const adapter: CatalogHookAdapter = {
|
|
catalog: 'web',
|
|
install(win, sink) {
|
|
// At document start: arrange for the site's catalog to pass through
|
|
// sink.seen(group) a catalog file is loading
|
|
// sink.matched(language, group) the hook matched
|
|
// sink.resolve(id, original) the value to register
|
|
return () => { /* remove the hook */ };
|
|
},
|
|
};
|
|
const handle = installStringPacks(window, [{ adapter, inventory, packs }], { onStatus });
|
|
```
|
|
|
|
- The adapter must not change the site's objects: hand the site a shadow
|
|
(`Object.create(module)`) instead.
|
|
- Everything fails open: an exception in a wrapper leaves the site's behaviour as it was.
|
|
- A catalog without a usable pack is not hooked at all.
|
|
- The self-check decides after `settleMs`: `ok`, `not-matched` (a catalog file was seen but the
|
|
hook did not match: the site changed) or `no-catalog` (no catalog file was seen).
|
|
|
|
For a webpack site, `watchChunkArray(win, name, onChunk)` sees every chunk before the site's own
|
|
`push` gets it and passes it on unchanged; `mapChunkRequires(chunk, map)` lets one identified
|
|
chunk's modules receive a shadow of what they `require`.
|
|
|
|
`installTextNet(win, { map })` is a DOM safety net for labels no catalog hook can reach (the server
|
|
sends them already worded): exact text in text nodes, `aria-label`, `placeholder` and `title`, the
|
|
site's value kept per node, restored on `dispose()`. It is secondary: it cannot see parameters or
|
|
plural forms.
|
|
|
|
## Checking a pack
|
|
|
|
```sh
|
|
waybetter strings check pack.strings.json --inventory strings/x.web.inventory.json
|
|
```
|
|
|
|
Prints what was refused and why, the unknown keys, and the coverage per group (translated of
|
|
translatable, and how many keys are not translatable yet). Exit 1 when anything would not be
|
|
applied. Without `--inventory`, the inventories come from `waybetter.strings.json`
|
|
(`{ "inventories": ["…"] }`) in the app's root.
|