Skip to content

Commit

Permalink
chore(inspectTranslations) add flag to replace TODO: copy (#1498)
Browse files Browse the repository at this point in the history
* chore(inspectTranslations) add flag to replace TODO: copy

* Update scripts/InspectTranslationKeys.js

Co-authored-by: Kelly Phan <[email protected]>

---------

Co-authored-by: Kelly Phan <[email protected]>
  • Loading branch information
ansmonjol and keellyp authored May 15, 2024
1 parent 4432242 commit 0e4538c
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions scripts/InspectTranslationKeys.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { GettextExtractor, JsExtractors } = require('gettext-extractor')
const SRC_DIR = './src/'
const TRANSLATION_FILES_PATH = './ditto/base.json' // './ditto/**.json' for when we'll support several languages

async function extract() {
async function extract(replaceMode) {
// Extract all the translation keys by parsing the 'translate' function
const extracts = new GettextExtractor()
.createJsParser([
Expand Down Expand Up @@ -44,9 +44,8 @@ async function extract() {
translationFiles.forEach((file) => {
// Get all translation keys from the ditto file
const dittoTranslations = JSON.parse(fs.readFileSync(file), 'utf-8')
const dittoKeys = Object.keys(dittoTranslations)
// Ignore timzeone keys as they're used in the config without calling translate
.filter((key) => key.split('_')[0] !== 'TZ')
// Ignore timezone keys as they're used in the config without calling translate
const dittoKeys = Object.keys(dittoTranslations).filter((key) => key.split('_')[0] !== 'TZ')
const keysNotInDitto = _.uniq(_.difference(foundKeys, dittoKeys))
const dittoKeysNotUsed = _.uniq(_.difference(dittoKeys, foundKeys))

Expand All @@ -70,6 +69,32 @@ async function extract() {
'\u001b[0m',
)
console.info(dittoKeysNotUsed.join('\n'))

if (replaceMode) {
for (let i = 0; i < dittoKeysNotUsed.length; i++) {
const key = dittoKeysNotUsed[i]
const translation = dittoTranslations[key]

// Iterate through each file to replace the translation string
for (let j = 0; j < files.length; j++) {
const filePath = files[j]
let fileContent = fs.readFileSync(filePath, 'utf-8')

// Perform a global replace for all occurrences of the translation string
const regex = new RegExp(`TODO: ${translation}`, 'g')
const replacedContent = fileContent.replace(regex, key)

// Check if replacement occurred in this file
if (replacedContent !== fileContent) {
console.info(`Replacing '${translation}' with '${key}' in: ${filePath}`)
// Write the modified content back to the file
fs.writeFileSync(filePath, replacedContent, 'utf-8')
}
}
}

console.info('\u001b[' + 32 + 'm' + '✔ Replacements done' + '\u001b[0m')
}
}

throw Error
Expand All @@ -84,7 +109,9 @@ async function extract() {
*/
async function main() {
try {
await extract()
const replaceMode = process.argv.includes('--replace')

await extract(replaceMode)
} catch (e) {
console.info('\u001b[' + 31 + 'm' + '\nTranslation check failed' + '\u001b[0m', e)
process.exit(1)
Expand Down

0 comments on commit 0e4538c

Please sign in to comment.