2022-11-08 18:57:12 +00:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
2022-01-10 22:25:06 +00:00
|
|
|
|
2022-11-08 19:10:13 +00:00
|
|
|
import * as parser from 'intl-messageformat-parser';
|
2022-11-08 18:57:12 +00:00
|
|
|
import manageTranslations, { readMessageFiles, ExtractedDescriptor } from 'react-intl-translations-manager';
|
2022-12-29 05:21:28 +00:00
|
|
|
import yargs from 'yargs';
|
2022-11-08 18:57:12 +00:00
|
|
|
|
|
|
|
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;
|
2022-11-08 18:57:12 +00:00
|
|
|
}, [] as string[]);
|
2020-03-27 20:59:38 +00:00
|
|
|
|
2022-11-08 18:57:12 +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');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-11-08 18:57:12 +00:00
|
|
|
const testAvailability: Validator = (language) => {
|
2020-03-27 20:59:38 +00:00
|
|
|
if (!availableLanguages.includes(language)) {
|
|
|
|
throw new Error('Not an available language');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-11-08 18:57:12 +00:00
|
|
|
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;
|
2022-11-08 18:57:12 +00:00
|
|
|
}, [] 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(', ')}
|
|
|
|
`;
|
|
|
|
|
2022-12-29 05:21:28 +00:00
|
|
|
const argv = yargs
|
2020-03-27 20:59:38 +00:00
|
|
|
.usage(usage)
|
|
|
|
.option('f', {
|
|
|
|
alias: 'force',
|
|
|
|
default: false,
|
|
|
|
describe: 'force using the provided languages. create files if not exists.',
|
|
|
|
type: 'boolean',
|
2022-12-29 05:21:28 +00:00
|
|
|
})
|
|
|
|
.parseSync();
|
2020-03-27 20:59:38 +00:00
|
|
|
|
|
|
|
// 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
|
2022-12-29 05:21:28 +00:00
|
|
|
const languages: string[] = (argv._.length > 0) ? argv._.map(String) : availableLanguages;
|
2020-03-27 20:59:38 +00:00
|
|
|
|
2022-11-08 18:57:12 +00:00
|
|
|
const validators: Validator[] = [
|
2020-03-27 20:59:38 +00:00
|
|
|
testRFC5646,
|
2022-11-08 18:57:12 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
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) => {
|
2022-11-08 18:57:12 +00:00
|
|
|
descriptor.descriptors?.forEach((item) => {
|
2021-08-03 17:10:42 +00:00
|
|
|
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;
|
2022-11-08 18:57:12 +00:00
|
|
|
}, [] 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,
|
2021-11-24 17:28:35 +00:00
|
|
|
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> {
|
2022-11-08 18:57:12 +00:00
|
|
|
return new Set(Array.from(a).filter(x => !b.has(x)));
|
2020-03-27 20:59:38 +00:00
|
|
|
}
|
|
|
|
|
2022-11-08 18:57:12 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-08 18:57:12 +00:00
|
|
|
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 {
|
2022-11-08 18:57:12 +00:00
|
|
|
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 ',
|
|
|
|
});
|
2022-11-08 18:57:12 +00:00
|
|
|
} 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;
|
2022-11-08 18:57:12 +00:00
|
|
|
}, [] 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) => {
|
2021-08-03 17:10:42 +00:00
|
|
|
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
|
|
|
}
|