2017-05-06 04:05:38 +02:00
|
|
|
const fs = require('fs');
|
2017-06-06 13:31:57 +02:00
|
|
|
const path = require('path');
|
2022-12-21 22:13:14 +01:00
|
|
|
const { default: manageTranslations, readMessageFiles } = require('react-intl-translations-manager');
|
2017-05-06 04:05:38 +02:00
|
|
|
|
2018-01-05 04:43:50 +01:00
|
|
|
const RFC5646_REGEXP = /^[a-z]{2,3}(?:-(?:x|[A-Za-z]{2,4}))*$/;
|
2017-06-06 13:31:57 +02:00
|
|
|
|
|
|
|
const rootDirectory = path.resolve(__dirname, '..', '..');
|
2022-12-21 22:13:14 +01:00
|
|
|
const externalDefaultMessages = path.resolve(rootDirectory, 'app', 'javascript', 'mastodon', 'locales', 'defaultMessages.json');
|
|
|
|
const translationsDirectory = path.resolve(rootDirectory, 'app', 'javascript', 'flavours', 'glitch', 'locales');
|
2017-06-06 13:31:57 +02:00
|
|
|
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-20 17:31:47 +02:00
|
|
|
};
|
2017-05-06 04:05:38 +02:00
|
|
|
|
2017-06-06 13:31:57 +02:00
|
|
|
const testAvailability = language => {
|
|
|
|
if (!availableLanguages.includes(language)) {
|
|
|
|
throw new Error('Not an available language');
|
|
|
|
}
|
2017-05-20 17:31:47 +02:00
|
|
|
};
|
2017-05-06 17:05:13 +02:00
|
|
|
|
2017-06-06 13:31:57 +02:00
|
|
|
const validateLanguages = (languages, validators) => {
|
|
|
|
const invalidLanguages = languages.reduce((acc, language) => {
|
2017-05-06 17:05:13 +02:00
|
|
|
try {
|
2017-06-06 13:31:57 +02:00
|
|
|
validators.forEach(validator => validator(language));
|
2017-05-06 17:05:13 +02:00
|
|
|
} catch (error) {
|
2017-06-06 13:31:57 +02:00
|
|
|
acc.push({ language, error });
|
2017-05-06 17:05:13 +02:00
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
if (invalidLanguages.length > 0) {
|
2017-06-06 13:31:57 +02: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-06 17:05:13 +02:00
|
|
|
process.exit(1);
|
|
|
|
}
|
2017-05-20 17:31:47 +02:00
|
|
|
};
|
2017-05-06 17:05:13 +02:00
|
|
|
|
2019-03-15 03:53:38 +01:00
|
|
|
const usage = `Usage: yarn manage:translations [OPTIONS] [LANGUAGES]
|
2017-05-06 04:05:38 +02:00
|
|
|
|
2017-06-06 13:31:57 +02:00
|
|
|
Manage JavaScript translation files in Mastodon. Generates and update translations in translationsDirectory: ${translationsDirectory}
|
2017-05-06 04:05:38 +02:00
|
|
|
|
|
|
|
LANGUAGES
|
2017-06-06 13:31:57 +02: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 04:05:38 +02:00
|
|
|
|
|
|
|
Available languages:
|
2017-06-06 13:31:57 +02:00
|
|
|
${availableLanguages.join(', ')}
|
2019-03-15 03:53:38 +01:00
|
|
|
`;
|
|
|
|
|
|
|
|
const { argv } = require('yargs')
|
|
|
|
.usage(usage)
|
|
|
|
.option('f', {
|
|
|
|
alias: 'force',
|
|
|
|
default: false,
|
|
|
|
describe: 'force using the provided languages. create files if not exists.',
|
|
|
|
type: 'boolean',
|
2017-06-06 13:31:57 +02:00
|
|
|
});
|
2017-05-06 04:05:38 +02:00
|
|
|
|
2017-05-06 17:05:13 +02:00
|
|
|
// check if message directory exists
|
2017-06-06 13:31:57 +02:00
|
|
|
if (!fs.existsSync(messagesDirectory)) {
|
|
|
|
console.error(`
|
|
|
|
Error: messagesDirectory not exists
|
|
|
|
(${messagesDirectory})
|
|
|
|
Try to run "yarn build:development" first`);
|
2017-05-06 17:05:13 +02:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2017-05-06 04:05:38 +02:00
|
|
|
// determine the languages list
|
2017-06-06 13:31:57 +02:00
|
|
|
const languages = (argv._.length > 0) ? argv._ : availableLanguages;
|
2017-05-06 04:05:38 +02:00
|
|
|
|
2017-05-06 17:05:13 +02:00
|
|
|
// validate languages
|
|
|
|
validateLanguages(languages, [
|
2017-06-06 13:31:57 +02:00
|
|
|
testRFC5646,
|
|
|
|
!argv.force && testAvailability,
|
|
|
|
].filter(Boolean));
|
2017-05-03 02:04:16 +02:00
|
|
|
|
2022-12-21 22:13:14 +01:00
|
|
|
// Override `provideExtractedMessages` to ignore translation strings provided upstream already
|
|
|
|
const provideExtractedMessages = () => {
|
|
|
|
const extractedMessages = readMessageFiles(messagesDirectory);
|
|
|
|
const originalExtractedMessages = JSON.parse(fs.readFileSync(externalDefaultMessages, 'utf8'));
|
|
|
|
const originalKeys = new Set();
|
|
|
|
|
|
|
|
originalExtractedMessages.forEach(file => {
|
|
|
|
file.descriptors.forEach(descriptor => {
|
|
|
|
originalKeys.add(descriptor.id)
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
extractedMessages.forEach(file => {
|
|
|
|
file.descriptors = file.descriptors.filter((descriptor) => !originalKeys.has(descriptor.id));
|
|
|
|
});
|
|
|
|
|
|
|
|
return extractedMessages.filter((file) => file.descriptors.length > 0);
|
|
|
|
};
|
|
|
|
|
2017-05-06 17:05:13 +02:00
|
|
|
// manage translations
|
2017-05-03 02:04:16 +02:00
|
|
|
manageTranslations({
|
2017-05-06 17:05:13 +02:00
|
|
|
messagesDirectory,
|
2017-05-06 04:05:38 +02:00
|
|
|
translationsDirectory,
|
2017-05-03 02:04:16 +02:00
|
|
|
detectDuplicateIds: false,
|
|
|
|
singleMessagesFile: true,
|
2017-05-06 04:05:38 +02:00
|
|
|
languages,
|
2017-05-07 19:55:47 +02:00
|
|
|
jsonOptions: {
|
|
|
|
trailingNewline: true,
|
|
|
|
},
|
2022-12-21 22:13:14 +01:00
|
|
|
overrideCoreMethods: {
|
|
|
|
provideExtractedMessages,
|
|
|
|
},
|
2017-05-06 04:05:38 +02:00
|
|
|
});
|