Skip to content
This repository has been archived by the owner on May 22, 2021. It is now read-only.

Generate production locales using 'compare-locales' #367

Merged
merged 3 commits into from
Aug 2, 2017
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
26 changes: 14 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,33 +46,30 @@
"license": "MPL-2.0",
"repository": "mozilla/send",
"availableLanguages": [
"en-US",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dannycoates Are these available languages in a "specific" order? I think my script just blindly A-z the locales, but wasn't sure if having "en-US" at the front is important, or if it serves as a fallback locale.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not really, they're in the order the pontoon ui puts them. a-z is fine

"zh-TW",
"zh-CN",
"cs",
"cy",
"de",
"dsb",
"en-US",
"es-MX",
"fr",
"fy-NL",
"de",
"hsb",
"hu",
"it",
"ja",
"kab",
"ms",
"nb-NO",
"nn-NO",
"pt-PT",
"pt-BR",
"pt-PT",
"ru",
"sk",
"sl",
"dsb",
"hsb",
"es-CL",
"es-ES",
"es-MX",
"sv-SE",
"tr",
"cy"
"zh-CN",
"zh-TW"
],
"scripts": {
"build": "npm-run-all build:*",
Expand All @@ -82,9 +79,14 @@
"build:l10n": "cp node_modules/l20n/dist/web/l20n.min.js public",
"dev": "npm run build && npm start",
"format": "prettier '{frontend/src/,scripts/,server/,test/**/!(bundle)}*.js' 'public/*.css' --single-quote --write",
"get-prod-locales": "node scripts/get-prod-locales",
"get-prod-locales:write": "npm run get-prod-locales -- --write",
"lint": "npm-run-all lint:*",
"lint:css": "stylelint 'public/*.css'",
"lint:js": "eslint .",
"lint-locales": "node scripts/lint-locales",
"lint-locales:dev": "npm run lint-locales",
"lint-locales:prod": "npm run lint-locales -- --production",
"start": "node server/server",
"test": "npm-run-all test:*",
"test:unit": "mocha test/unit",
Expand Down
6 changes: 6 additions & 0 deletions scripts/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
rules:
node/shebang: off
security/detect-child-process: off

no-console: off
no-process-exit: off
49 changes: 49 additions & 0 deletions scripts/get-prod-locales.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env node

const cp = require('child_process');
const { promisify } = require('util');
const fs = require('fs');
const pkg = require('../package.json');

const availableLanguages = pkg.availableLanguages.sort();
const exec = promisify(cp.exec);

const arrayDiff = (current, package) =>
current.filter(locale => !package.includes(locale));

const cmd = 'compare-locales l10n.toml . `ls public/locales` --data=json';

exec(cmd)
.then(({ stdout }) => JSON.parse(stdout))
.then(({ summary }) => {
const locales = Object.keys(summary)
.filter(locale => {
const loc = summary[locale];
const hasMissing = loc.hasOwnProperty('missing');
const hasErrors = loc.hasOwnProperty('errors');
return !hasMissing && !hasErrors;
})
.sort();

if (locales.join(',') !== availableLanguages.join(',')) {
const missingLanguages = arrayDiff(locales, availableLanguages);

console.log('current 100%:', JSON.stringify(locales));
console.log('package.json:', JSON.stringify(availableLanguages));
console.log('missing prod:', JSON.stringify(missingLanguages));

if (process.argv.includes('--write')) {
const pkgPath = require.resolve('../package.json');
pkg.availableLanguages = locales;
const str = JSON.stringify(pkg, null, 2) + '\n';
console.log('Updating /package.json availableLanguages');
fs.writeFileSync(pkgPath, str, 'utf-8');
}
} else {
console.log('Production locales are up to date!');
}
})
.catch(err => {
console.error(err);
process.exit(1);
});
51 changes: 51 additions & 0 deletions scripts/lint-locales.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env node

const cp = require('child_process');
const { promisify } = require('util');
const pkg = require('../package.json');
const conf = require('../server/config');

const exec = promisify(cp.exec);
const cmd = `compare-locales l10n.toml . ${getLocales()} --data=json`;

console.log(cmd);

exec(cmd)
.then(({ stdout }) => JSON.parse(stdout))
.then(({ details }) => filterErrors(details))
.then(results => {
if (results.length) {
results.forEach(({ locale, data }) => {
console.log(locale);
data.forEach(msg => console.log(`- ${msg}`));
console.log('');
});
process.exit(2);
}
})
.catch(err => {
console.error(err);
process.exit(1);
});

function filterErrors(details) {
return Object.keys(details)
.sort()
.map(locale => {
const data = details[locale]
.filter(item => item.hasOwnProperty('error'))
.map(({ error }) => error);
return { locale, data };
})
.filter(({ data }) => data.length);
}

function getLocales() {
// If we're in a "production" env (or passed the `--production` flag), only
// check the locales from the package.json file's `availableLanguages` array.
if (conf.env === 'production' || process.argv.includes('--production')) {
return pkg.availableLanguages.sort().join(' ');
}
// Lint all the locales.
return '`ls public/locales`';
}