soapbox/scripts/translationRunner.ts

242 wiersze
6.7 KiB
TypeScript
Czysty Zwykły widok Historia

import fs from 'fs';
import path from 'path';
import * as parser from 'intl-messageformat-parser';
import manageTranslations, { readMessageFiles, ExtractedDescriptor } from 'react-intl-translations-manager';
type Validator = (language: string) => void;
interface LanguageResult {
language: string,
error: any,
}
2020-03-27 20:59:38 +00:00
const RFC5646_REGEXP = /^[a-z]{2,3}(?:-(?:x|[A-Za-z]{2,4}))*$/;
2022-12-29 04:59:39 +00:00
const rootDirectory = path.resolve(__dirname, '..');
2020-05-28 22:52:07 +00:00
const translationsDirectory = path.resolve(rootDirectory, 'app', 'soapbox', 'locales');
2020-03-27 20:59:38 +00: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;
}, [] as string[]);
2020-03-27 20:59:38 +00:00
const testRFC5646: Validator = (language) => {
2020-03-27 20:59:38 +00:00
if (!RFC5646_REGEXP.test(language)) {
throw new Error('Not RFC5646 name');
}
};
const testAvailability: Validator = (language) => {
2020-03-27 20:59:38 +00:00
if (!availableLanguages.includes(language)) {
throw new Error('Not an available language');
}
};
const validateLanguages = (languages: string[], validators: Validator[]): void => {
const invalidLanguages = languages.reduce((acc, language): LanguageResult[] => {
2020-03-27 20:59:38 +00:00
try {
validators.forEach(validator => validator(language));
} catch (error) {
acc.push({ language, error });
}
return acc;
}, [] as LanguageResult[]);
2020-03-27 20:59:38 +00:00
if (invalidLanguages.length > 0) {
console.error(`
Error: Specified invalid LANGUAGES:
${invalidLanguages.map(({ language, error }) => `* ${language}: ${error.message}`).join('\n')}
Use yarn "manage:translations -- --help" for usage information
`);
process.exit(1);
}
};
const usage = `Usage: yarn manage:translations [OPTIONS] [LANGUAGES]
2022-08-29 16:20:56 +00:00
Manage JavaScript translation files in Soapbox. Generates and update translations in translationsDirectory: ${translationsDirectory}
2020-03-27 20:59:38 +00: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.join(', ')}
`;
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',
});
// check if message directory exists
if (!fs.existsSync(messagesDirectory)) {
console.error(`
Error: messagesDirectory not exists
(${messagesDirectory})
2020-06-06 18:22:58 +00:00
Try to run "yarn build" first`);
2020-03-27 20:59:38 +00:00
process.exit(1);
}
// determine the languages list
const languages: string[] = (argv._.length > 0) ? argv._ : availableLanguages;
2020-03-27 20:59:38 +00:00
const validators: Validator[] = [
2020-03-27 20:59:38 +00:00
testRFC5646,
];
if (!argv.force) {
validators.push(testAvailability);
}
// validate languages
validateLanguages(languages, validators);
2020-03-27 20:59:38 +00:00
// manage translations
manageTranslations({
messagesDirectory,
translationsDirectory,
detectDuplicateIds: false,
singleMessagesFile: true,
languages,
jsonOptions: {
trailingNewline: true,
},
});
// Check variable interpolations and print error messages if variables are
// used in translations which are not used in the default message.
/* eslint-disable no-console */
2022-11-08 21:33:47 +00:00
function findVariablesinAST(tree: parser.MessageFormatElement[]): Set<string> {
const result = new Set<string>();
2020-03-27 20:59:38 +00:00
tree.forEach((element) => {
switch (element.type) {
2022-05-11 21:06:35 +00:00
case parser.TYPE.argument:
case parser.TYPE.number:
result.add(element.value);
break;
case parser.TYPE.plural:
result.add(element.value);
Object.values(element.options)
.map(option => option.value)
.forEach(subtree =>
findVariablesinAST(subtree)
.forEach(variable => result.add(variable)));
break;
case parser.TYPE.literal:
break;
default:
console.log('unhandled element=', element);
break;
2020-03-27 20:59:38 +00:00
}
});
return result;
}
2022-11-08 21:33:47 +00:00
function findVariables(string: string): Set<string> {
2020-03-27 20:59:38 +00:00
return findVariablesinAST(parser.parse(string));
}
const extractedMessagesFiles = readMessageFiles(translationsDirectory);
const extractedMessages = extractedMessagesFiles.reduce((acc, messageFile) => {
messageFile.descriptors.forEach((descriptor) => {
descriptor.descriptors?.forEach((item) => {
const variables = findVariables(item.defaultMessage);
2020-03-27 20:59:38 +00:00
acc.push({
id: item.id,
defaultMessage: item.defaultMessage,
variables: variables,
});
});
});
return acc;
}, [] as ExtractedDescriptor[]);
2020-03-27 20:59:38 +00:00
2022-11-08 21:33:47 +00:00
interface Translation {
language: string,
data: Record<string, string>,
}
const translations: Translation[] = languages.map((language: string) => {
2020-03-27 20:59:38 +00:00
return {
language: language,
data: JSON.parse(fs.readFileSync(path.join(translationsDirectory, language + '.json'), 'utf8')),
2020-03-27 20:59:38 +00:00
};
});
2022-11-08 21:33:47 +00:00
function difference<T>(a: Set<T>, b: Set<T>): Set<T> {
return new Set(Array.from(a).filter(x => !b.has(x)));
2020-03-27 20:59:38 +00:00
}
function pushIfUnique<T>(arr: T[], newItem: T): void {
2020-03-27 20:59:38 +00:00
if (arr.every((item) => {
return (JSON.stringify(item) !== JSON.stringify(newItem));
})) {
arr.push(newItem);
}
}
interface Problem {
language: string,
id: ExtractedDescriptor['id'],
severity: 'error' | 'warning',
type: string,
}
const problems: Problem[] = translations.reduce((acc, translation) => {
2020-03-27 20:59:38 +00:00
extractedMessages.forEach((message) => {
try {
const translationVariables = findVariables(translation.data[message.id!]);
if (Array.from(difference(translationVariables, message.variables)).length > 0) {
2020-03-27 20:59:38 +00:00
pushIfUnique(acc, {
language: translation.language,
id: message.id,
severity: 'error',
type: 'missing variable ',
});
} else if (Array.from(difference(message.variables, translationVariables)).length > 0) {
2020-03-27 20:59:38 +00:00
pushIfUnique(acc, {
language: translation.language,
id: message.id,
severity: 'warning',
type: 'inconsistent variables',
});
}
} catch (error) {
pushIfUnique(acc, {
language: translation.language,
id: message.id,
severity: 'error',
type: 'syntax error ',
});
}
});
return acc;
}, [] as Problem[]);
2020-03-27 20:59:38 +00:00
if (problems.length > 0) {
console.error(`${problems.length} messages found with errors or warnings:`);
console.error('\nLoc\tIssue \tMessage ID');
console.error('-'.repeat(60));
problems.forEach((problem) => {
const color = (problem.severity === 'error') ? '\x1b[31m' : '';
2020-03-27 20:59:38 +00:00
console.error(`${color}${problem.language}\t${problem.type}\t${problem.id}\x1b[0m`);
});
console.error('\n');
if (problems.find((item) => {
return item.severity === 'error';
})) {
process.exit(1);
}
2021-08-03 19:22:51 +00:00
}