2017-05-06 12:05:38 +10:00
|
|
|
const fs = require('fs');
|
2017-06-06 21:31:57 +10:00
|
|
|
const path = require('path');
|
|
|
|
const { default: manageTranslations } = require('react-intl-translations-manager');
|
2017-05-06 12:05:38 +10:00
|
|
|
|
2018-01-05 14:43:50 +11:00
|
|
|
const RFC5646_REGEXP = /^[a-z]{2,3}(?:-(?:x|[A-Za-z]{2,4}))*$/;
|
2017-06-06 21:31:57 +10:00
|
|
|
|
|
|
|
const rootDirectory = path.resolve(__dirname, '..', '..');
|
|
|
|
const translationsDirectory = path.resolve(rootDirectory, 'app', 'javascript', 'mastodon', 'locales');
|
|
|
|
const messagesDirectory = path.resolve(rootDirectory, 'build', 'messages');
|
|
|
|
const availableLanguages = fs.readdirSync(translationsDirectory).reduce((languages, filename) => {
|
|
|
|
const basename = path.basename(filename, '.json');
|
|
|
|
if (RFC5646_REGEXP.test(basename)) {
|
|
|
|
languages.push(basename);
|
|
|
|
}
|
|
|
|
return languages;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const testRFC5646 = language => {
|
|
|
|
if (!RFC5646_REGEXP.test(language)) {
|
|
|
|
throw new Error('Not RFC5646 name');
|
|
|
|
}
|
2017-05-21 01:31:47 +10:00
|
|
|
};
|
2017-05-06 12:05:38 +10:00
|
|
|
|
2017-06-06 21:31:57 +10:00
|
|
|
const testAvailability = language => {
|
|
|
|
if (!availableLanguages.includes(language)) {
|
|
|
|
throw new Error('Not an available language');
|
|
|
|
}
|
2017-05-21 01:31:47 +10:00
|
|
|
};
|
2017-05-07 01:05:13 +10:00
|
|
|
|
2017-06-06 21:31:57 +10:00
|
|
|
const validateLanguages = (languages, validators) => {
|
|
|
|
const invalidLanguages = languages.reduce((acc, language) => {
|
2017-05-07 01:05:13 +10:00
|
|
|
try {
|
2017-06-06 21:31:57 +10:00
|
|
|
validators.forEach(validator => validator(language));
|
2017-05-07 01:05:13 +10:00
|
|
|
} catch (error) {
|
2017-06-06 21:31:57 +10:00
|
|
|
acc.push({ language, error });
|
2017-05-07 01:05:13 +10:00
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
if (invalidLanguages.length > 0) {
|
2017-06-06 21:31:57 +10:00
|
|
|
console.error(`
|
|
|
|
Error: Specified invalid LANGUAGES:
|
|
|
|
${invalidLanguages.map(({ language, error }) => `* ${language}: ${error.message}`).join('\n')}
|
|
|
|
|
|
|
|
Use yarn "manage:translations -- --help" for usage information
|
|
|
|
`);
|
2017-05-07 01:05:13 +10:00
|
|
|
process.exit(1);
|
|
|
|
}
|
2017-05-21 01:31:47 +10:00
|
|
|
};
|
2017-05-07 01:05:13 +10:00
|
|
|
|
2017-06-06 21:31:57 +10:00
|
|
|
const { argv } = require('yargs')
|
|
|
|
.usage(`Usage: yarn manage:translations -- [OPTIONS] [LANGUAGES]
|
2017-05-06 12:05:38 +10:00
|
|
|
|
2017-06-06 21:31:57 +10:00
|
|
|
Manage JavaScript translation files in Mastodon. Generates and update translations in translationsDirectory: ${translationsDirectory}
|
2017-05-06 12:05:38 +10:00
|
|
|
|
|
|
|
LANGUAGES
|
2017-06-06 21:31:57 +10:00
|
|
|
The RFC5646 language tag for the language you want to test or fix. If you want to input multiple languages, separate them with space.
|
2017-05-06 12:05:38 +10:00
|
|
|
|
|
|
|
Available languages:
|
2017-06-06 21:31:57 +10:00
|
|
|
${availableLanguages.join(', ')}
|
|
|
|
`)
|
|
|
|
.help('h', 'show this message')
|
|
|
|
.alias('h', 'help')
|
|
|
|
.options({
|
|
|
|
f: {
|
|
|
|
alias: 'force',
|
|
|
|
default: false,
|
|
|
|
describe: 'force using the provided languages. create files if not exists.',
|
|
|
|
type: 'boolean',
|
|
|
|
},
|
|
|
|
});
|
2017-05-06 12:05:38 +10:00
|
|
|
|
2017-05-07 01:05:13 +10:00
|
|
|
// check if message directory exists
|
2017-06-06 21:31:57 +10:00
|
|
|
if (!fs.existsSync(messagesDirectory)) {
|
|
|
|
console.error(`
|
|
|
|
Error: messagesDirectory not exists
|
|
|
|
(${messagesDirectory})
|
|
|
|
Try to run "yarn build:development" first`);
|
2017-05-07 01:05:13 +10:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2017-05-06 12:05:38 +10:00
|
|
|
// determine the languages list
|
2017-06-06 21:31:57 +10:00
|
|
|
const languages = (argv._.length > 0) ? argv._ : availableLanguages;
|
2017-05-06 12:05:38 +10:00
|
|
|
|
2017-05-07 01:05:13 +10:00
|
|
|
// validate languages
|
|
|
|
validateLanguages(languages, [
|
2017-06-06 21:31:57 +10:00
|
|
|
testRFC5646,
|
|
|
|
!argv.force && testAvailability,
|
|
|
|
].filter(Boolean));
|
2017-05-03 10:04:16 +10:00
|
|
|
|
2017-05-07 01:05:13 +10:00
|
|
|
// manage translations
|
2017-05-03 10:04:16 +10:00
|
|
|
manageTranslations({
|
2017-05-07 01:05:13 +10:00
|
|
|
messagesDirectory,
|
2017-05-06 12:05:38 +10:00
|
|
|
translationsDirectory,
|
2017-05-03 10:04:16 +10:00
|
|
|
detectDuplicateIds: false,
|
|
|
|
singleMessagesFile: true,
|
2017-05-06 12:05:38 +10:00
|
|
|
languages,
|
2017-05-08 03:55:47 +10:00
|
|
|
jsonOptions: {
|
|
|
|
trailingNewline: true,
|
|
|
|
},
|
2017-05-06 12:05:38 +10:00
|
|
|
});
|