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