Skip to content

Templates and catalogues

Two build steps, both from @gopherium/gottext/build. One reads your source and writes the template. The other turns translated catalogues into JSON.

This entry point needs gettext-extractor, an optional peer. Install it or importing anything from @gopherium/gottext/build fails to load:

Terminal window
pnpm add -D gettext-extractor

pot reads your sources and returns the .pot file as bytes:

import { writeFileSync } from 'node:fs'
import { pot } from '@gopherium/gottext/build'
writeFileSync('languages/myapp.pot', pot({
domain: 'myapp',
root: process.cwd(),
sources: ['src/**/*.ts', 'src/**/*.tsx'],
}))
  • domain is the text domain the template carries. Only calls naming it are read.
  • root is what the globs resolve against.
  • sources are the globs holding your messages.
  • ignored optionally names paths inside those globs to skip.
  • goRoots optionally names directories of Go source to read too.
  • manifests optionally names globs of plugin manifests to read too.

It finds four call shapes: __ for a plain message, _x when a word needs a context to tell two senses apart, _n for a plural, and _nx for a plural with a context. It finds them written plainly or through an i18n. prefix.

A call belongs to the domain it names. A call naming no domain belongs to default, the domain the WordPress packages read, so __('Move up') stays out of a myapp template. When the domain is not written as a string, such as a constant, the call is kept, because the template cannot tell which domain it names.

If your app loads its catalogue under the default domain and writes its calls with no domain, build its template with domain: 'default'.

Run it in a script your build calls, and commit the template. It is the list every translator works from.

If a message lives in Go rather than TypeScript, mark it and name the directory in goRoots:

Msgid("Save")

goMessages is the walk behind it, exported if you need it alone. Without goRoots no Go source is read at all.

If your plugins describe themselves in a plugin.json, name those files in manifests and pot reads them too:

pot({
domain: 'myapp',
root: process.cwd(),
sources: ['src/**/*.ts'],
manifests: ['plugins/*/plugin.json'],
})

The globs are read from root, like sources. From each manifest pot takes the name and description, and the label and help of each entry in settings, each key read in lower case:

{
"id": "events",
"name": "Events",
"description": "Sell seats for your events",
"backend": "github.com/you/myapp/plugins/events",
"settings": [
{
"key": "max_seats", "type": "count", "default": "4", "min": 1,
"label": "Seats per booking", "help": "The most seats one booking can hold"
}
],
"menu": [{ "label": "Reservations", "screen": "reservations", "capability": "events.manage" }]
}

This one gives the template four messages. Every other key is ignored, so id, backend and the menu entry’s label stay out. An empty or null value stays out too. Next to each message the template writes the manifest’s path, such as plugins/events/plugin.json, so a translator can see where it came from.

pot rejects a glob that matches no file, a path it cannot read as a file, a manifest that is not a JSON object, a settings that is not a list of objects, and any of the four keys it reads holding something other than text. The error names the glob or the manifest.

manifests reads its globs through Node’s fs.globSync, which is stable from Node 22.17. On Node 22.13 to 22.16 it works but prints Node’s experimental warning.

A .po file is for translators, not for browsers. compileCatalog turns one into the shape @wordpress/i18n loads, and serializeCatalog writes it as JSON:

import { readFileSync, writeFileSync } from 'node:fs'
import { compileCatalog, serializeCatalog } from '@gopherium/gottext/build'
const compiled = compileCatalog(readFileSync('languages/es-ES.po', 'utf8'))
writeFileSync('src/languages/es-ES.json', serializeCatalog(compiled))

The output is sorted and deterministic, with the metadata entry first, so recompiling an unchanged catalogue produces an identical file and your diffs stay quiet.

Ship one JSON file per language and load it from startLocale.