Last active
December 2, 2019 15:40
-
-
Save 413n/b690593f1ebe41de65be25c9be2b7c9c to your computer and use it in GitHub Desktop.
Replace all locales id with locales msg in the entire project (lingui)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require("fs"); | |
const readline = require("readline"); | |
const stream = require("stream"); | |
const path = require("path"); | |
const util = require("util"); | |
// Folder where all locales in all files will be replaced | |
const srcFolder = "./path/to/folder/"; | |
// Master locale file where it will take all the locales to replace | |
const localeFile = "./path/to/folder/"; //.po file | |
// Folder and subfolder file walker | |
// Ref: https://ourcodeworld.com/articles/read/420/how-to-read-recursively-a-directory-in-node-js | |
function filewalker(dir, done) { | |
let results = []; | |
fs.readdir(dir, function(err, list) { | |
if (err) return done(err); | |
var pending = list.length; | |
if (!pending) return done(null, results); | |
list.forEach(function(file) { | |
file = path.resolve(dir, file); | |
fs.stat(file, function(err, stat) { | |
// If directory, execute a recursive call | |
if (stat && stat.isDirectory()) { | |
// Add directory to array [comment if you need to remove the directories from the array] | |
results.push(file); | |
filewalker(file, function(err, res) { | |
results = results.concat(res); | |
if (!--pending) done(null, results); | |
}); | |
} else { | |
results.push(file); | |
if (!--pending) done(null, results); | |
} | |
}); | |
}); | |
}); | |
} | |
// Get pure string of the locale | |
function getPureString(str) { | |
str = str.match(/"[^"]+"/g); | |
if (Array.isArray(str)) { | |
return str[0].replace('"', "").replace('"', ""); | |
} else { | |
return null; | |
} | |
} | |
// Read all the locale file lines | |
function readLines({ input }) { | |
const output = new stream.PassThrough({ objectMode: true }); | |
const rl = readline.createInterface({ input }); | |
rl.on("line", line => { | |
output.write(line); | |
}); | |
rl.on("close", () => { | |
output.destroy(); | |
}); | |
return output; | |
} | |
// Defining the master locale file | |
const input = fs.createReadStream(localeFile); | |
// Main function | |
(async () => { | |
let locales = {}; | |
let prevLocale = ""; | |
let readNextLine = false; | |
// Pass through every line and save the locales | |
for await (const line of readLines({ input })) { | |
if (!!line.includes("msgid")) { | |
locale = getPureString(line); | |
if (!!locale) { | |
prevLocale = locale; | |
readNextLine = true; | |
} | |
} | |
if (!!readNextLine && !!line.includes("msgstr")) { | |
localeStr = getPureString(line); | |
if (!!localeStr) { | |
locales[prevLocale] = localeStr; | |
readNextLine = false; | |
} | |
} | |
} | |
// Pass through every file and replace the locales | |
filewalker(srcFolder, (err, data) => { | |
if (err) { | |
console.log("ERROR WALKING", err); | |
throw err; | |
} | |
const files = data; | |
for (const file of files) { | |
if (file.endsWith(".js") || file.endsWith(".po")) { | |
let fileContent = fs.readFileSync(file, "utf8"); | |
if (!!fileContent) { | |
let replacedContent = fileContent; | |
for (let locale in locales) { | |
let regexp = new RegExp( | |
locale.replace(".", "\\."), | |
"g" | |
); | |
if (locale !== locales[locale]) { | |
while (!!replacedContent.match(regexp)) { | |
replacedContent = replacedContent.replace( | |
regexp, | |
locales[locale] | |
); | |
// Some debugging... | |
// console.log( | |
// file | |
// .split("\\") | |
// .splice(-2) | |
// .join("\\"), | |
// locale, | |
// "with", | |
// locales[locale], | |
// "\n" | |
// // , fileContent === replacedContent | |
// // ? "\x1b[44m" + | |
// // replacedContent + | |
// // "\x1b[0m" | |
// // : "" | |
// ); | |
} | |
} | |
} | |
if (!!replacedContent && fileContent !== replacedContent) { | |
fs.writeFileSync(file, replacedContent, "utf8"); | |
// Some debugging... | |
// console.log( | |
// "after", | |
// locale, | |
// "IN", | |
// file | |
// .split("\\") | |
// .splice(-2) | |
// .join("\\") | |
// ); | |
} | |
} else { | |
console.log("ERROR READING", fileContent); | |
} | |
} | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment