2017-05-05 22:05:38 -04:00
|
|
|
const fs = require('fs');
|
2017-06-06 07:31:57 -04:00
|
|
|
const path = require('path');
|
2022-12-21 16:13:14 -05:00
|
|
|
const { default: manageTranslations, readMessageFiles } = require('react-intl-translations-manager');
|
2017-05-05 22:05:38 -04:00
|
|
|
|
2018-01-04 22:43:50 -05:00
|
|
|
const RFC5646_REGEXP = /^[a-z]{2,3}(?:-(?:x|[A-Za-z]{2,4}))*$/;
|
2017-06-06 07:31:57 -04:00
|
|
|
|
|
|
|
const rootDirectory = path.resolve(__dirname, '..', '..');
|
2022-12-21 16:13:14 -05: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 07:31:57 -04: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 11:31:47 -04:00
|
|
|
};
|
2017-05-05 22:05:38 -04:00
|
|
|
|
2017-06-06 07:31:57 -04:00
|
|
|
const testAvailability = language => {
|
|
|
|
if (!availableLanguages.includes(language)) {
|
|
|
|
throw new Error('Not an available language');
|
|
|
|
}
|
2017-05-20 11:31:47 -04:00
|
|
|
};
|
2017-05-06 11:05:13 -04:00
|
|
|
|
2017-06-06 07:31:57 -04:00
|
|
|
const validateLanguages = (languages, validators) => {
|
|
|
|
const invalidLanguages = languages.reduce((acc, language) => {
|
2017-05-06 11:05:13 -04:00
|
|
|
try {
|
2017-06-06 07:31:57 -04:00
|
|
|
validators.forEach(validator => validator(language));
|
2017-05-06 11:05:13 -04:00
|
|
|
} catch (error) {
|
2017-06-06 07:31:57 -04:00
|
|
|
acc.push({ language, error });
|
2017-05-06 11:05:13 -04:00
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
if (invalidLanguages.length > 0) {
|
2017-06-06 07:31:57 -04: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 11:05:13 -04:00
|
|
|
process.exit(1);
|
|
|
|
}
|
2017-05-20 11:31:47 -04:00
|
|
|
};
|
2017-05-06 11:05:13 -04:00
|
|
|
|
2019-03-14 22:53:38 -04:00
|
|
|
const usage = `Usage: yarn manage:translations [OPTIONS] [LANGUAGES]
|
2017-05-05 22:05:38 -04:00
|
|
|
|
2017-06-06 07:31:57 -04:00
|
|
|
Manage JavaScript translation files in Mastodon. Generates and update translations in translationsDirectory: ${translationsDirectory}
|
2017-05-05 22:05:38 -04:00
|
|
|
|
|
|
|
LANGUAGES
|
2017-06-06 07:31:57 -04: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-05 22:05:38 -04:00
|
|
|
|
|
|
|
Available languages:
|
2017-06-06 07:31:57 -04:00
|
|
|
${availableLanguages.join(', ')}
|
2019-03-14 22:53:38 -04: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 07:31:57 -04:00
|
|
|
});
|
2017-05-05 22:05:38 -04:00
|
|
|
|
2017-05-06 11:05:13 -04:00
|
|
|
// check if message directory exists
|
2017-06-06 07:31:57 -04:00
|
|
|
if (!fs.existsSync(messagesDirectory)) {
|
|
|
|
console.error(`
|
|
|
|
Error: messagesDirectory not exists
|
|
|
|
(${messagesDirectory})
|
|
|
|
Try to run "yarn build:development" first`);
|
2017-05-06 11:05:13 -04:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2017-05-05 22:05:38 -04:00
|
|
|
// determine the languages list
|
2017-06-06 07:31:57 -04:00
|
|
|
const languages = (argv._.length > 0) ? argv._ : availableLanguages;
|
2017-05-05 22:05:38 -04:00
|
|
|
|
2017-05-06 11:05:13 -04:00
|
|
|
// validate languages
|
|
|
|
validateLanguages(languages, [
|
2017-06-06 07:31:57 -04:00
|
|
|
testRFC5646,
|
|
|
|
!argv.force && testAvailability,
|
|
|
|
].filter(Boolean));
|
2017-05-02 20:04:16 -04:00
|
|
|
|
2022-12-21 16:13:14 -05: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 11:05:13 -04:00
|
|
|
// manage translations
|
2017-05-02 20:04:16 -04:00
|
|
|
manageTranslations({
|
2017-05-06 11:05:13 -04:00
|
|
|
messagesDirectory,
|
2017-05-05 22:05:38 -04:00
|
|
|
translationsDirectory,
|
2017-05-02 20:04:16 -04:00
|
|
|
detectDuplicateIds: false,
|
|
|
|
singleMessagesFile: true,
|
2017-05-05 22:05:38 -04:00
|
|
|
languages,
|
2017-05-07 13:55:47 -04:00
|
|
|
jsonOptions: {
|
|
|
|
trailingNewline: true,
|
|
|
|
},
|
2022-12-21 16:13:14 -05:00
|
|
|
overrideCoreMethods: {
|
|
|
|
provideExtractedMessages,
|
|
|
|
},
|
2017-05-05 22:05:38 -04:00
|
|
|
});
|