[Interest] Translating JSON files

Thiago Macieira thiago.macieira at intel.com
Mon Jun 17 23:40:16 CEST 2019


On Monday, 17 June 2019 14:18:33 PDT Mitch Curtis wrote:
> The thing is that I have other data in the file that doesn't need to be
> translated, so copying them would require keeping each file in sync. The
> file itself is edited often fairly regularly to add new data (both
> translatable and non-translatable). 

Sounds like a design flaw. I suggest you split that up.

A simple solution is to *not* have the translations in the .json files, but 
only US-ASCII English versions. Then you extract those strings into a proper 
.ts or .pot file, translate them using your preferred tool, and *keep* the 
translations there. At runtime, you translate using:

  // .toUtf8() would be strictly speaking more correct, but if you restrict
  // to US-ASCII, you gain a bit in performance
  tr(stringFromJson.toLatin1());

> I do like the idea of generating .ts files from JSON...

You can play with the examples/corelib/serialization/convert tool and an XSL-T 
stylesheet. Or write your own extract tool, of course.

Or you may want to generate a dummy .cpp file with your extracted strings so 
you can pass it to lupdate or msgfmt, so it handles fuzzying and merging of 
new strings into an already-translated file.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel System Software Products






More information about the Interest mailing list