From ef6cbf9d7055bd66a6391413f1237fd4e154c245 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Mon, 6 Jul 2020 16:39:07 -0400 Subject: [PATCH] feat: add translation script that transforms qstr to qstrid --- .gitignore | 1 + scripts/translationScripts/qstrConverter.js | 75 +++++++++++++++++++ .../status-react-translations/.gitkeep | 0 3 files changed, 76 insertions(+) create mode 100644 scripts/translationScripts/qstrConverter.js create mode 100644 scripts/translationScripts/status-react-translations/.gitkeep diff --git a/.gitignore b/.gitignore index ee4390dff7..626215217b 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,4 @@ yarn.lock TODO resources.rcc resources.qrc +status-react-translations/ diff --git a/scripts/translationScripts/qstrConverter.js b/scripts/translationScripts/qstrConverter.js new file mode 100644 index 0000000000..fbf97b0c97 --- /dev/null +++ b/scripts/translationScripts/qstrConverter.js @@ -0,0 +1,75 @@ +const fs = require('fs'); +const path = require("path") +const enTranslations = require('./status-react-translations/en.json'); + +const getAllFiles = function(dirPath, ext, arrayOfFiles = []) { + const files = fs.readdirSync(dirPath) + + files.forEach(file => { + if (fs.statSync(dirPath + "/" + file).isDirectory()) { + arrayOfFiles = getAllFiles(dirPath + "/" + file, ext, arrayOfFiles) + } else if (!ext || file.endsWith(ext)) { + arrayOfFiles.push(path.join(__dirname, dirPath, "/", file)) + } + }) + + return arrayOfFiles +} + +console.log('Scanning files...') +const qmlFiles = getAllFiles('../../ui', 'qml'); + +const translationKeys = Object.keys(enTranslations) +const translationValues = Object.values(enTranslations) + +// Match all qsTr("...") functions and get the string inside +const qstrRegex = /qsTr\(["'](.*?)["']\)/g +const tabsRegex = /\n([\s]+)/ +let numberOfFilesDone = 0 + +console.log(`Modifying ${qmlFiles.length} files...`) +qmlFiles.forEach(file => { + let fileContent = fs.readFileSync(file).toString(); + + let match, replaceableText, enTranslationIndex, lastSpace, tabSubstring, spaces, replacementId, quote; + let modified = false; + + while ((match = qstrRegex.exec(fileContent)) !== null) { + modified = true; + replaceableText = match[1]; + + enTranslationIndex = translationValues.indexOf(replaceableText) + if (enTranslationIndex > -1) { + replacementId = translationKeys[enTranslationIndex] + } else { + // We need to replace all qsTr because we can't mix qsTrId and qsTr + // TODO remove chars that are not alphanumarical + replacementId = replaceableText.replace(/\s/g, '-').toLowerCase(); + } + + quote = match[0][5]; + // Replace the qsTr by a qsTrId and a comment + fileContent = fileContent.replace(`qsTr(${quote}${replaceableText}${quote})`, `qsTrId("${replacementId}")`) + + // Find the place where to put the comment + lastSpace = fileContent.lastIndexOf('\n ', match.index); + tabSubstring = fileContent.substring(lastSpace, match.index); + + spaces = tabsRegex.exec(tabSubstring); + fileContent = fileContent.substring(0, lastSpace + 1) + + spaces[1] + `//% "${replaceableText}"` + + fileContent.substring(lastSpace); + + // Increase the last index of the regex as we increased the size of the file and so if the next qstr is on the same line, + // the chances are high that the next match willl be the same word, creating an infinite loop + qstrRegex.lastIndex += spaces[1].length + 6 + replaceableText.length + } + + + fs.writeFileSync(file, fileContent); + numberOfFilesDone++; + if (numberOfFilesDone % 10 === 0) { + console.log(`\t${numberOfFilesDone}/${qmlFiles.length} completed...`) + } +}); +console.log('Allo done!') diff --git a/scripts/translationScripts/status-react-translations/.gitkeep b/scripts/translationScripts/status-react-translations/.gitkeep new file mode 100644 index 0000000000..e69de29bb2