Automatic translation of i18n files

April 24, 2023

Fixing translation for some languages can be a boring task.
At Woovi, we automate most of our boring tasks.
Let’s dig how we automated the first translation for our i18n workflow.

Internationalization (i18n) in our codebase

We extract all usages of i18n in our using i18next-parser.
The folder structure of our locales files are like this:

- src
-- locales
---- en.json
---- es.json
---- pt-BR.json
Enter fullscreen mode

Exit fullscreen mode

When a new i18n key is added to the code the json is populated like this:

"New Charge": "__STRING_NOT_TRANSLATED__",
Enter fullscreen mode

Exit fullscreen mode

We use the zx script below to translate all __STRING_NOT_TRANSLATED, using translate-shell

// Define the path to your JSON file and the new value to replace the placeholder string
const [, , , inputFile] = process.argv;

import { fs } from 'zx';
const filePath = path.resolve(inputFile);

const originalLanguage = filePath.split('/').pop().split('.')[0] === 'pt-BR' ? 'pt' : 'es';

const filePaths = [
  originalLanguage === 'pt' ? filePath.replace('pt-BR.json', 'es.json') : filePath.replace('es.json', 'pt-BR.json')
// read the JSON file

for (const inputFilePath of filePaths ) {
  const actualLanguage = inputFilePath.split('/').pop().split('.')[0] === 'pt-BR' ? 'pt' : 'es';

  const json = await fs.readFile(inputFilePath, 'utf8');

  const parsedData = JSON.parse(json);

  let stringsToParse = [];

  for (const key in parsedData) {
    // Check if the value of the key is the placeholder string
    if (parsedData[key] === '__STRING_NOT_TRANSLATED__') {
        [key]: parsedData[key],

  const promiseParseString = (value, key) => {
    const string = Object.keys(value)[0];
    const translatedString = (await $`trans -brief -s en -t ${actualLanguage} ${string}`).toString().trim().replace(`\n`, '');

    console.log({string, translatedString})
    return { [string]: translatedString };

  const translatedStringsParsed = await Promise.all(promiseParseString).then((value) => value).catch((err) => err);


  const mappedObject = Object.fromEntries(
    Object.entries(parsedData).map(([key, value]) => {
      const translatedString = translatedStringsParsed.find((string) => string[key]);
      if (translatedString) {
        return [key, translatedString[key]];
      return [key, value];

  // write into the JSON file

  await fs.writeFile(inputFilePath, JSON.stringify(mappedObject, null, 2));
Enter fullscreen mode

Exit fullscreen mode

In Conclusion

At Woovi, we are always investing more and more in automation to make us more productive.

How are you making your Company more productive?

This script was made by edumaciel

Woovi is a Startup that enables shoppers to pay as they like. To make this possible, Woovi provides instant payment solutions for merchants to accept orders.

If you want to work with us, we are hiring!

Photo by Alex Knight on Unsplash

Source link

Comments 0

Leave a Reply

Your email address will not be published. Required fields are marked *