Automatic translation of i18n files
Tags: i18n, automation
Published: 2023-04-24 16:44:05

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:

```jsx
i18n
- src
-- locales
---- en.json
---- es.json
---- pt-BR.json
```

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

```jsx
"New Charge": "__STRING_NOT_TRANSLATED__",
```

We use the `zx` script below to translate all `__STRING_NOT_TRANSLATED`, using [translate-shell](https://github.com/soimort/translate-shell)

```jsx
// 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 = [
  filePath,
  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__') {
      stringsToParse.push({
        [key]: parsedData[key],
      });
    }
  }

  const promiseParseString = stringsToParse.map(async (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);

  console.log({translatedStringsParsed})

  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));
}
```


## 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](https://twitter.com/edumaciel10_)

---
Woovi
[Woovi](https://www.woovi.com) 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](https://woovi.com/jobs/)!

---
Photo by <a href="https://unsplash.com/@agk42?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Alex Knight</a> on <a href="https://unsplash.com/photos/2EJCSULRwC8?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>