2017-05-06 11:05:38 +09:00
|
|
|
/*eslint no-console: "off"*/
|
2017-05-03 09:04:16 +09:00
|
|
|
const manageTranslations = require('react-intl-translations-manager').default;
|
2017-05-06 11:05:38 +09:00
|
|
|
const fs = require('fs');
|
|
|
|
|
2017-05-07 00:05:13 +09:00
|
|
|
const testRFC5626 = function (reRFC5646) {
|
|
|
|
return function (language) {
|
|
|
|
if (!language.match(reRFC5646)) {
|
|
|
|
throw new Error('Not RFC5626 name');
|
|
|
|
}
|
2017-05-21 00:31:47 +09:00
|
|
|
};
|
|
|
|
};
|
2017-05-06 11:05:38 +09:00
|
|
|
|
2017-05-07 00:05:13 +09:00
|
|
|
const testAvailability = function (availableLanguages) {
|
|
|
|
return function (language) {
|
|
|
|
if ((argv.force !== true) && availableLanguages.indexOf(language) < 0) {
|
|
|
|
throw new Error('Not an available language');
|
|
|
|
}
|
2017-05-21 00:31:47 +09:00
|
|
|
};
|
|
|
|
};
|
2017-05-07 00:05:13 +09:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2017-05-21 00:31:47 +09:00
|
|
|
};
|
2017-05-07 00:05:13 +09:00
|
|
|
|
|
|
|
const printHelpMessages = function () {
|
2017-05-06 11:05:38 +09:00
|
|
|
console.log(
|
|
|
|
`Usage: yarn manage:translations -- [OPTIONS] [LANGUAGES]
|
|
|
|
|
|
|
|
Manage javascript translation files in mastodon. Generates and update
|
|
|
|
translations in translationsDirectory: ${translationsDirectory}
|
|
|
|
|
|
|
|
OPTIONS
|
2017-05-07 00:05:13 +09:00
|
|
|
-h,--help show this message
|
|
|
|
-f,--force force using the provided languages. create files if not exists.
|
|
|
|
default: false
|
2017-05-06 11:05:38 +09:00
|
|
|
|
|
|
|
LANGUAGES
|
|
|
|
The RFC5646 language tag for the language you want to test or fix. If you want
|
|
|
|
to input multiple languages, separate them with space.
|
|
|
|
|
|
|
|
Available languages:
|
|
|
|
${availableLanguages}
|
|
|
|
`);
|
2017-05-21 00:31:47 +09:00
|
|
|
};
|
2017-05-07 00:05:13 +09:00
|
|
|
|
|
|
|
// parse arguments
|
|
|
|
const argv = require('minimist')(process.argv.slice(2), {
|
|
|
|
'boolean': [
|
|
|
|
'force',
|
2017-05-21 00:31:47 +09:00
|
|
|
'help',
|
2017-05-07 00:05:13 +09:00
|
|
|
],
|
|
|
|
'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();
|
2017-05-06 11:05:38 +09:00
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
2017-05-07 00:05:13 +09:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2017-05-06 11:05:38 +09:00
|
|
|
// determine the languages list
|
|
|
|
const languages = (argv._.length === 0) ? availableLanguages : argv._;
|
|
|
|
|
2017-05-07 00:05:13 +09:00
|
|
|
// validate languages
|
|
|
|
validateLanguages(languages, [
|
|
|
|
testRFC5626(reRFC5646),
|
|
|
|
testAvailability(availableLanguages),
|
|
|
|
]);
|
2017-05-03 09:04:16 +09:00
|
|
|
|
2017-05-07 00:05:13 +09:00
|
|
|
// manage translations
|
2017-05-03 09:04:16 +09:00
|
|
|
manageTranslations({
|
2017-05-07 00:05:13 +09:00
|
|
|
messagesDirectory,
|
2017-05-06 11:05:38 +09:00
|
|
|
translationsDirectory,
|
2017-05-03 09:04:16 +09:00
|
|
|
detectDuplicateIds: false,
|
|
|
|
singleMessagesFile: true,
|
2017-05-06 11:05:38 +09:00
|
|
|
languages,
|
2017-05-08 02:55:47 +09:00
|
|
|
jsonOptions: {
|
|
|
|
trailingNewline: true,
|
|
|
|
},
|
2017-05-06 11:05:38 +09:00
|
|
|
});
|