Automatic translation of i18n files

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 us…


This content originally appeared on DEV Community and was authored by Sibelius Seraphini

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:

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:

"New Charge": "__STRING_NOT_TRANSLATED__",

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 = [
  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

Woovi
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


This content originally appeared on DEV Community and was authored by Sibelius Seraphini


Print Share Comment Cite Upload Translate Updates
APA

Sibelius Seraphini | Sciencx (2023-04-24T16:44:05+00:00) Automatic translation of i18n files. Retrieved from https://www.scien.cx/2023/04/24/automatic-translation-of-i18n-files/

MLA
" » Automatic translation of i18n files." Sibelius Seraphini | Sciencx - Monday April 24, 2023, https://www.scien.cx/2023/04/24/automatic-translation-of-i18n-files/
HARVARD
Sibelius Seraphini | Sciencx Monday April 24, 2023 » Automatic translation of i18n files., viewed ,<https://www.scien.cx/2023/04/24/automatic-translation-of-i18n-files/>
VANCOUVER
Sibelius Seraphini | Sciencx - » Automatic translation of i18n files. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/04/24/automatic-translation-of-i18n-files/
CHICAGO
" » Automatic translation of i18n files." Sibelius Seraphini | Sciencx - Accessed . https://www.scien.cx/2023/04/24/automatic-translation-of-i18n-files/
IEEE
" » Automatic translation of i18n files." Sibelius Seraphini | Sciencx [Online]. Available: https://www.scien.cx/2023/04/24/automatic-translation-of-i18n-files/. [Accessed: ]
rf:citation
» Automatic translation of i18n files | Sibelius Seraphini | Sciencx | https://www.scien.cx/2023/04/24/automatic-translation-of-i18n-files/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.