Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 24, 2023 04:44 pm GMT

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 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 stringconst [, , , 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 filefor (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(`
`
, ''); 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


Original Link: https://dev.to/woovi/automatic-translation-of-i18n-files-4ip9

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To