JSON5

Translate JSON5 catalogs in place, keeping their comments, quotes, unquoted names and number spellings.

The json5 file handler translates JSON5 files: JSON with comments, unquoted property names, single-quoted strings, trailing commas, hexadecimal numbers, Infinity and NaN, and strings continued over several lines. It selects string values by the same paths as JSON and writes each translation into a copy of the source, so comments, quotes, layout and number spellings stay as they were. Only translated strings change.

Configure JSON5 translation #

A .json5 file belongs only to a files entry whose pattern ends in .json5, or one that sets "format": "json5", and such an entry selects only .json5 files. An entry with a broad pattern such as locales/en/* and another format skips them:

json
{
  "sourceLocale": "en",
  "targetLocales": ["de", "fr"],
  "files": [{ "pattern": "locales/en/*.json5" }]
}

This writes locales/de/settings.json5 and locales/fr/settings.json5 for locales/en/settings.json5. Target paths follow the same rules as JSON, described in Output paths, and targetPath declares any other layout. The same configuration works in the CLI and the GitHub App.

What is translated #

Every string value is a unit, keyed by its path as in JSON. Property names, numbers, booleans and null are kept as data, and a string at the root or a root array works too:

json5
// Settings page strings
{
  // Page title
  title: 'Settings',
  nested: { save: 'Save', cancel: "Cancel" },
  limits: { max: 0xFF, ratio: .5 },
}

Here title, nested.save and nested.cancel are translated, and max and ratio keep their spelling. As in JSONC, the last comment before a string reaches the translator as a hint, so "Page title" describes title. A comment at the end of a line comes before the next value, so put a string's comment on the line above it. With @context, the hint is the rest of that line.

A string whose value is an identifier, a link or a number is still text to the handler, so protect it with lockedKeys. When an object repeats a property name, the last one is the value, as JSON5 parsers read it: it is translated, and the earlier ones stay as written.

How translations are written #

A translated string keeps the quotes its source used. A quote of that kind, a backslash and a line break in the translation are escaped, so 'Save' becomes 'Speichern' and a translation containing it's is written as 'it\'s'. A string continued over several lines in the source is written on one line. A byte order mark and the file's line breaks are kept.

Each written file is read back before it is saved. If a translation could not be written without changing what the file means, that file fails and nothing is written for it.

Keys and reuse #

A push reuses the existing translation of every string whose source has not changed since the last push, matched by path; a --key refresh, below, regenerates the strings it selects. lockedKeys, preservedKeys, ignoredKeys and injectLocale work as for JSON:

  • A locked string keeps its source value.
  • A preserved value keeps the existing target's value as written there, so a preserved 0x10 stays 0x10.
  • An ignored entry is left out of the target, together with the comment lines right above it.
  • An injected locale replaces the value at its path, or is added as a new entry laid out like the entries beside it.

lingo push --key and the GitHub App's /lingo translate --key refresh selected keys in an existing target. That run writes into the target rather than the source, so the target's own comments and other translations stay. The App's --context reaches JSON5 files as it does JSON files.

Invalid JSON5 #

A file that is not valid JSON5 fails with the line and column of the first problem, such as a line break inside a string, a missing comma or a number with a leading zero. Nothing is written for that file, and an existing target stays as it was. A target that is not valid JSON5 fails the same way and names its locale when the run reads it: to reuse its translations, keep its preserved values or update its keys. A run that reads none of them replaces it. Fix the file and push again.