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 20bc50ccb..00014c958 100644 --- a/package.json +++ b/package.json @@ -24,7 +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/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() 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