diff --git a/README.md b/README.md index 5e6615f..708afe5 100644 --- a/README.md +++ b/README.md @@ -28,8 +28,9 @@ All native speakers are welcome to contribute! Please do not make pull requests - Fork this repo - Download your fork - In your downloaded directory, install dependencies by running `yarn` -- If starting a new locale, create the directory named after your language tag within `locales` and copy over files from the `locales/en-US` directory -- Make your changes/additions +- Localize + - If starting a new locale, create the directory named after your language tag within `locales` and copy over files from the `locales/en-US` directory + - Run `yarn run --locale ` automatically will be appended the new content in the bottom of all the files - Review them visually (see Verify Your Changes below) - Run `yarn lint:json` to validate everything is alphabetized and formatted properly - Commit, push, and open a pull request! diff --git a/package.json b/package.json index 2e08c1f..f491378 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ }, "scripts": { "compare": "node check_keys.js", + "update-l10n": "node update.js", "lint:json": "npx jsonlint -qsi locales/**/*.json" } } diff --git a/update.js b/update.js new file mode 100755 index 0000000..2feadd2 --- /dev/null +++ b/update.js @@ -0,0 +1,78 @@ +#!/usr/bin/env nodejs +const parseArgs = require('minimist') +const chalk = require('chalk') +const fs = require('fs'); + +const args = parseArgs(process.argv.slice(2)) + +if (!args.locale) { + console.error(chalk.red(`Missing 'locale' argument. Exiting`)) + process.exit(1) +} + +if (!args.baseLocale) { + console.error(chalk.yellow(`Missing 'baseLocale' argument. Will fall back to 'en-US'`)) +} + +const baseLocale = args.baseLocale || 'en-US' +const locale = args.locale + +function compareAndGenerate(file1, file2) { + const base = JSON.parse(fs.readFileSync(file1, 'utf-8')); + const locale_file = JSON.parse(fs.readFileSync(file2, 'utf-8')); + + const missingKeys = findMissingKeys(base, locale_file); + + const output = addMissingKeys(base, locale_file, missingKeys); + + fs.writeFileSync(file2, JSON.stringify(output, null, 2), 'utf-8'); +} + +function findMissingKeys(obj1, obj2) { + const missingKeys = []; + + for (const key in obj1) { + if (obj1.hasOwnProperty(key)) { + if (!obj2.hasOwnProperty(key)) { + missingKeys.push(key); + } else if (typeof obj1[key] === 'object' && typeof obj2[key] === 'object') { + const nestedMissingKeys = findMissingKeys(obj1[key], obj2[key]); + if (nestedMissingKeys.length > 0) { + missingKeys.push(key); + } + } + } + } + + return missingKeys; +} + +function addMissingKeys(obj1, obj2, missingKeys) { + for (const key of missingKeys) { + if (obj1[key] !== undefined && typeof obj1[key] === 'object') { + obj2[key] = addMissingKeys(obj1[key], obj2[key] || {}, Object.keys(obj1[key])); + } else { + if (!obj2.hasOwnProperty(key)) { + obj2[key] = obj1[key]; + } + } + } + return obj2; +} + +const checkFile = filename => { + console.log(chalk.blue(`Updating ${filename}.json...`)) + + compareAndGenerate(`./locales/${baseLocale}/${filename}.json`, `./locales/${locale}/${filename}.json`); +} + +console.log(chalk.yellow(`Updating ${locale} to ${baseLocale}...`)) + +const filenames = [ + 'general', + 'proton-report', + 'protondb-content', + 'questionnaire' +] + +filenames.map(filename => checkFile(filename))