Formats
PO, JSON, and other translation file formats, and how to write your own
A formatter reads and writes one catalogue format. The CLI uses it to write the source catalogue on extract, and to read every catalogue on compile; your app never sees it.
import po from '@saykit/format-po';
buckets: [
{
// ...
formatter: po(),
},
];PO (Gettext) is the default, and the format most translation tools already speak. JSON (@saykit/format-json) is there for projects that want plain JSON, ARB or WebExtension files.
PO
PO files look like this:
msgid ""
msgstr ""
"Project-Id-Version: \n"
"Language: fr\n"
"Content-Type: text/plain; charset=UTF-8\n"
"X-Generator: saykit\n"
#. Translator comment from the source
#: src/app/page.tsx:14
#: src/app/inbox.tsx:8
msgid "Hello, {name}!"
msgstr "Bonjour, {name} !"
msgid "Inbox"
msgctxt "noun"
msgstr "Boîte de réception"Each entry has:
msgid, the source string (or ICU MessageFormat for plural/select)msgstr, the translation (empty for new entries)msgctxt, the descriptor's context, if any#., translator comments (from// TRANSLATORS:lines)#:, source references (file:line)#. id:xxxx, SayKit's stable id (for messages without a custom id)
PO is readable, diffs line by line, and is understood natively by POEdit, Crowdin, Lokalise, Weblate and Transifex.
Options
Prop
Type
formatter: po({ includeReferences: true, includeLineNumbers: false });Dropping line numbers keeps the file references but stops diffs churning every time a line moves.
Some translation tools want a flag on every entry. Weblate, for example, only runs its ICU checks and placeholder highlighting on entries flagged icu-message-format, and every SayKit catalogue is ICU:
formatter: po({ flags: ['icu-message-format'] });#. id:welcome
#: src/app.tsx
#, icu-message-format
msgid "Welcome, {name}"
msgstr ""JSON
@saykit/format-json writes one JSON file per locale, keyed by message id (the content hash when there is no custom id), with the translation as the value.
import json from '@saykit/format-json';
buckets: [
{
// ...
formatter: json(),
},
];{
"greeting": "Bonjour, {name} !",
"inbox": "Boîte de réception"
}This plain layout is lean but drops comments, context, and source references, so give your messages explicit ids to get stable, readable keys.
Dialects
To keep the metadata a plain { key: value } map can't hold, pass dialect to switch to a richer but still standard JSON layout.
Prop
Type
formatter: json({ dialect: 'arb' });{
"@@locale": "fr",
"greeting": "Bonjour, {name} !",
"@greeting": {
"description": "A friendly hello",
"x-saykit-context": "formal",
"x-saykit-references": ["src/app/page.tsx:14"]
}
}Both formats carry translator comments in their native description field. Context and source references have no standard slot, so SayKit round-trips them through x-saykit-context / x-saykit-references extension fields, and other tooling reads the description and safely ignores the rest.
References
As with PO, you can trim or drop the source references a dialect writes. Neither option affects the plain layout, which carries no metadata to begin with.
Prop
Type
formatter: json({ dialect: 'arb', includeLineNumbers: false });Other formats
For YAML, XLIFF or anything else, a Formatter is a plain object. Its extension cannot be .js, which is where the compiled module goes.
import type { Formatter } from '@saykit/config';
const json: Formatter = {
extension: '.json',
parse(content) {
/* return Message[] */
},
stringify(messages, { locale, existingContent }) {
/* return string */
},
};See the custom formatter guide for a complete walkthrough.
The Message shape
Formatters work with this shape, exported from @saykit/config (not the runtime's compiled Message):
Prop
Type
stringify receives an array of these and parse returns one.
Next
- Custom formatter guide, write your own
- Configuration, where formatters plug in