Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

misc: new translations scripts #1670

Merged
merged 2 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ jobs:

- name: Run Translation Check
run: |
yarn inspectTranslations
yarn translations:inspect
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
72 changes: 72 additions & 0 deletions scripts/translations/add.js
Original file line number Diff line number Diff line change
@@ -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()
File renamed without changes.
Loading