translateionRunner: improve minimalist options (#2835)

* translateionRunner: improve minimalist options

* Properly added flags.
* Added alias: -h, -f

* translationRunner: improve logic and flow

* Show all error messages instead of validate availability / name
  format separately.

* translationRunner: check messageDirectory existance

* translationRunner: changed throw string to Error

* translationRunner: use short cut for boolean
master
Koala Yeung 8 years ago committed by Eugen Rochko
parent aa235318fc
commit 99b9a0e5de
  1. 134
      config/webpack/translationRunner.js

@ -1,20 +1,49 @@
/*eslint no-console: "off"*/ /*eslint no-console: "off"*/
const manageTranslations = require('react-intl-translations-manager').default; const manageTranslations = require('react-intl-translations-manager').default;
const argv = require('minimist')(process.argv.slice(2));
const fs = require('fs'); const fs = require('fs');
const translationsDirectory = 'app/javascript/mastodon/locales'; const testRFC5626 = function (reRFC5646) {
const localeFn = /^([a-z]{2,3}(|\-[A-Z]+))\.json$/; return function (language) {
const reRFC5646 = /^[a-z]{2,3}(|\-[A-Z]+)$/; if (!language.match(reRFC5646)) {
const availableLanguages = fs.readdirSync(`${process.cwd()}/${translationsDirectory}`).reduce((acc, fn) => { throw new Error('Not RFC5626 name');
if (fn.match(localeFn)) { }
acc.push(fn.replace(localeFn, '$1'));
} }
return acc; }
}, []);
// print help message const testAvailability = function (availableLanguages) {
if (argv.help !== undefined) { return function (language) {
if ((argv.force !== true) && availableLanguages.indexOf(language) < 0) {
throw new Error('Not an available language');
}
}
}
const validateLanguages = function (languages, validators) {
let invalidLanguages = languages.reduce((acc, language) => {
try {
for (let validator of validators) {
validator(language);
}
} catch (error) {
acc.push({
language,
error,
});
}
return acc;
}, []);
if (invalidLanguages.length > 0) {
console.log(`\nError: Specified invalid LANGUAGES:`);
for (let {language, error} of invalidLanguages) {
console.error(`* ${language}: ${error}`);
}
console.log(`\nUse yarn "manage:translations -- --help" for usage information\n`);
process.exit(1);
}
}
const printHelpMessages = function () {
console.log( console.log(
`Usage: yarn manage:translations -- [OPTIONS] [LANGUAGES] `Usage: yarn manage:translations -- [OPTIONS] [LANGUAGES]
@ -22,9 +51,9 @@ Manage javascript translation files in mastodon. Generates and update
translations in translationsDirectory: ${translationsDirectory} translations in translationsDirectory: ${translationsDirectory}
OPTIONS OPTIONS
--help show this message -h,--help show this message
--force force using the provided languages. create files if not exists. -f,--force force using the provided languages. create files if not exists.
default: false default: false
LANGUAGES LANGUAGES
The RFC5646 language tag for the language you want to test or fix. If you want The RFC5646 language tag for the language you want to test or fix. If you want
@ -33,50 +62,55 @@ to input multiple languages, separate them with space.
Available languages: Available languages:
${availableLanguages} ${availableLanguages}
`); `);
}
// parse arguments
const argv = require('minimist')(process.argv.slice(2), {
'boolean': [
'force',
'help'
],
'alias': {
'f': 'force',
'h': 'help',
},
});
const translationsDirectory = 'app/javascript/mastodon/locales';
const messagesDirectory = 'build/messages';
const localeFn = /^([a-z]{2,3}(|\-[A-Z]+))\.json$/;
const reRFC5646 = /^[a-z]{2,3}(|\-[A-Z]+)$/;
const availableLanguages = fs.readdirSync(`${process.cwd()}/${translationsDirectory}`).reduce((acc, fn) => {
if (fn.match(localeFn)) {
acc.push(fn.replace(localeFn, '$1'));
}
return acc;
}, []);
// print help message
if (argv.help) {
printHelpMessages();
process.exit(0); process.exit(0);
} }
// check if message directory exists
if (!fs.existsSync(`${process.cwd()}/${messagesDirectory}`)) {
console.error(`\nError: messageDirectory not exists\n(${process.cwd()}/${messagesDirectory})\n`);
console.error(`Try to run "yarn build:development" first`);
process.exit(1);
}
// determine the languages list // determine the languages list
const languages = (argv._.length === 0) ? availableLanguages : argv._; const languages = (argv._.length === 0) ? availableLanguages : argv._;
// check if the languages provided are RFC5626 compliant // validate languages
(function() { validateLanguages(languages, [
let invalidLanguages = languages.reduce((acc, language) => { testRFC5626(reRFC5646),
if (!language.match(reRFC5646)) { testAvailability(availableLanguages),
acc.push(language); ]);
}
return acc;
}, []);
if (invalidLanguages.length > 0) {
console.log(`Error:`);
for (let language of invalidLanguages) {
console.error(`* Not RFC5626 name: ${language}`);
}
console.log(`\nUse yarn "manage:translations -- --help" for usage information\n`);
process.exit(1);
}
})();
// make sure the language exists. Unless force to create locale file.
if (argv.force !== true) {
let invalidLanguages = languages.reduce((acc, language) => {
if (availableLanguages.indexOf(language) < 0) {
acc.push(language);
}
return acc;
}, []);
if (invalidLanguages.length > 0) {
console.log(`Error:`);
for (let language of invalidLanguages) {
console.error(`* Language not available: ${language}`);
}
console.log(`\nIf you want to force creating the language(s) above, please add the --force option.\n`);
process.exit(1);
}
}
// manage translations
manageTranslations({ manageTranslations({
messagesDirectory: 'build/messages', messagesDirectory,
translationsDirectory, translationsDirectory,
detectDuplicateIds: false, detectDuplicateIds: false,
singleMessagesFile: true, singleMessagesFile: true,

Loading…
Cancel
Save