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

feat(script): update l10n files #102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <locale-code>` 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!
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"scripts": {
"compare": "node check_keys.js",
"update-l10n": "node update.js",
"lint:json": "npx jsonlint -qsi locales/**/*.json"
}
}
78 changes: 78 additions & 0 deletions update.js
Original file line number Diff line number Diff line change
@@ -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))