From 0df555860830490a8418a5a6b77864f94e010a0b Mon Sep 17 00:00:00 2001 From: Alexandre Monjol Date: Fri, 16 Aug 2024 15:04:28 -0300 Subject: [PATCH 1/2] misc: add new script to create translation keys --- package.json | 1 + scripts/translations/add.js | 72 +++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 scripts/translations/add.js diff --git a/package.json b/package.json index 20bc50ccb..efa096223 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "test": "jest --config jest.config.js", "test:e2e": "cypress open --config-file cypress/cypress.config.js", "inspectTranslations": "node scripts/InspectTranslationKeys", + "translations:add": "node scripts/translations/add", "changelog:update": "auto-changelog --template=.changelog/regular.hbs", "postinstall": "npx yarn-deduplicate" }, diff --git a/scripts/translations/add.js b/scripts/translations/add.js new file mode 100644 index 000000000..f7f8fe145 --- /dev/null +++ b/scripts/translations/add.js @@ -0,0 +1,72 @@ +/* eslint no-console: ["error", { allow: ["info"] }] */ +const fs = require('fs') + +const { globSync } = require('glob') + +const TRANSLATION_FILES_PATH = './ditto/base.json' // './ditto/**.json' for when we'll support several languages + +const KEY_RANDOM_CHARS_LENGTH = 11 + +function createRandomCharChain() { + let result = '' + const characters = 'abcdefghijklmnopqrstuvwxyz0123456789' + const charactersLength = characters.length + let counter = 0 + + while (counter < KEY_RANDOM_CHARS_LENGTH) { + result += characters.charAt(Math.floor(Math.random() * charactersLength)) + counter += 1 + } + return result +} + +async function addNewTranslationsKey(numberOfKeysToAdd) { + const translationFiles = globSync(TRANSLATION_FILES_PATH) + + // For each translations files + translationFiles.forEach((file) => { + // Get all translation keys + const allTranslationsFromFile = JSON.parse(fs.readFileSync(file), 'utf-8') + // Ignore timezone keys as they're used in the config without calling translate + const existingKeysInTranslationFile = Object.keys(allTranslationsFromFile).filter( + (key) => key.split('_')[0] !== 'TZ', + ) + + const newKeys = {} + + for (let i = 0; i < Number(numberOfKeysToAdd); i++) { + const key = `text_${Date.now() + createRandomCharChain()}` + + if (newKeys[key] || existingKeysInTranslationFile.includes(key)) { + i-- + continue + } + + newKeys[key] = '' + } + + // Happen the new keys to the existing ones in the file + const updatedTranslations = { ...allTranslationsFromFile, ...newKeys } + + // Write the updated translations back to the file + fs.writeFileSync(file, JSON.stringify(updatedTranslations, null, 2), 'utf-8') + }) +} + +/** + * Extract wrapper + */ +async function main() { + try { + const numberOfKeysToAdd = process.argv.slice(2)[0] || '1' + + await addNewTranslationsKey(numberOfKeysToAdd) + + console.info('\u001b[' + 32 + 'm' + '✔ All good' + '\u001b[0m') + } catch (error) { + console.info('\u001b[' + 31 + 'm' + '\nTranslation keys addition failed' + '\u001b[0m', error) + process.exit(1) + } +} + +main() From e15fe022ec11d9333e2a466764a8b488e444056d Mon Sep 17 00:00:00 2001 From: Alexandre Monjol Date: Fri, 16 Aug 2024 15:06:37 -0300 Subject: [PATCH 2/2] misc: move inspect translations script --- .github/workflows/linter.yml | 2 +- package.json | 2 +- scripts/{InspectTranslationKeys.js => translations/inspect.js} | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename scripts/{InspectTranslationKeys.js => translations/inspect.js} (100%) diff --git a/.github/workflows/linter.yml b/.github/workflows/linter.yml index 3971ad58a..23a200c94 100644 --- a/.github/workflows/linter.yml +++ b/.github/workflows/linter.yml @@ -32,4 +32,4 @@ jobs: - name: Run Translation Check run: | - yarn inspectTranslations \ No newline at end of file + yarn translations:inspect \ No newline at end of file diff --git a/package.json b/package.json index efa096223..00014c958 100644 --- a/package.json +++ b/package.json @@ -24,8 +24,8 @@ "codegen:watch": "graphql-codegen --config codegen.yml -r dotenv/config --watch", "test": "jest --config jest.config.js", "test:e2e": "cypress open --config-file cypress/cypress.config.js", - "inspectTranslations": "node scripts/InspectTranslationKeys", "translations:add": "node scripts/translations/add", + "translations:inspect": "node scripts/translations/inspect", "changelog:update": "auto-changelog --template=.changelog/regular.hbs", "postinstall": "npx yarn-deduplicate" }, diff --git a/scripts/InspectTranslationKeys.js b/scripts/translations/inspect.js similarity index 100% rename from scripts/InspectTranslationKeys.js rename to scripts/translations/inspect.js