sforkowany z mirror/soapbox
Merge branch 'translation-runner-ts' into 'develop'
translationRunner TypeScript See merge request soapbox-pub/soapbox!1875x-truth-ads
commit
8208518e2c
|
@ -0,0 +1,2 @@
|
|||
[
|
||||
]
|
|
@ -20,7 +20,7 @@
|
|||
"dev": "${npm_execpath} run start",
|
||||
"build": "npx webpack",
|
||||
"jsdoc": "npx jsdoc -c jsdoc.conf.js",
|
||||
"manage:translations": "node ./translationRunner.js",
|
||||
"manage:translations": "npx ts-node ./translationRunner.ts",
|
||||
"test": "npx cross-env NODE_ENV=test npx jest",
|
||||
"test:coverage": "${npm_execpath} run test --coverage",
|
||||
"test:all": "${npm_execpath} run test:coverage && ${npm_execpath} run lint",
|
||||
|
|
|
@ -1,12 +1,19 @@
|
|||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
const parser = require('intl-messageformat-parser');
|
||||
const { default: manageTranslations, readMessageFiles } = require('react-intl-translations-manager'); // eslint-disable-line import/order
|
||||
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,
|
||||
}
|
||||
|
||||
const RFC5646_REGEXP = /^[a-z]{2,3}(?:-(?:x|[A-Za-z]{2,4}))*$/;
|
||||
|
||||
const rootDirectory = path.resolve(__dirname, '..');
|
||||
const rootDirectory = path.resolve(__dirname);
|
||||
const translationsDirectory = path.resolve(rootDirectory, 'app', 'soapbox', 'locales');
|
||||
const messagesDirectory = path.resolve(rootDirectory, 'build', 'messages');
|
||||
const availableLanguages = fs.readdirSync(translationsDirectory).reduce((languages, filename) => {
|
||||
|
@ -15,29 +22,29 @@ const availableLanguages = fs.readdirSync(translationsDirectory).reduce((languag
|
|||
languages.push(basename);
|
||||
}
|
||||
return languages;
|
||||
}, []);
|
||||
}, [] as string[]);
|
||||
|
||||
const testRFC5646 = language => {
|
||||
const testRFC5646: Validator = (language) => {
|
||||
if (!RFC5646_REGEXP.test(language)) {
|
||||
throw new Error('Not RFC5646 name');
|
||||
}
|
||||
};
|
||||
|
||||
const testAvailability = language => {
|
||||
const testAvailability: Validator = (language) => {
|
||||
if (!availableLanguages.includes(language)) {
|
||||
throw new Error('Not an available language');
|
||||
}
|
||||
};
|
||||
|
||||
const validateLanguages = (languages, validators) => {
|
||||
const invalidLanguages = languages.reduce((acc, language) => {
|
||||
const validateLanguages = (languages: string[], validators: Validator[]): void => {
|
||||
const invalidLanguages = languages.reduce((acc, language): LanguageResult[] => {
|
||||
try {
|
||||
validators.forEach(validator => validator(language));
|
||||
} catch (error) {
|
||||
acc.push({ language, error });
|
||||
}
|
||||
return acc;
|
||||
}, []);
|
||||
}, [] as LanguageResult[]);
|
||||
|
||||
if (invalidLanguages.length > 0) {
|
||||
console.error(`
|
||||
|
@ -80,13 +87,18 @@ Try to run "yarn build" first`);
|
|||
}
|
||||
|
||||
// determine the languages list
|
||||
const languages = (argv._.length > 0) ? argv._ : availableLanguages;
|
||||
const languages: string[] = (argv._.length > 0) ? argv._ : availableLanguages;
|
||||
|
||||
const validators: Validator[] = [
|
||||
testRFC5646,
|
||||
];
|
||||
|
||||
if (!argv.force) {
|
||||
validators.push(testAvailability);
|
||||
}
|
||||
|
||||
// validate languages
|
||||
validateLanguages(languages, [
|
||||
testRFC5646,
|
||||
!argv.force && testAvailability,
|
||||
].filter(Boolean));
|
||||
validateLanguages(languages, validators);
|
||||
|
||||
// manage translations
|
||||
manageTranslations({
|
||||
|
@ -104,8 +116,8 @@ manageTranslations({
|
|||
// used in translations which are not used in the default message.
|
||||
/* eslint-disable no-console */
|
||||
|
||||
function findVariablesinAST(tree) {
|
||||
const result = new Set();
|
||||
function findVariablesinAST(tree: parser.MessageFormatElement[]): Set<string> {
|
||||
const result = new Set<string>();
|
||||
tree.forEach((element) => {
|
||||
switch (element.type) {
|
||||
case parser.TYPE.argument:
|
||||
|
@ -130,14 +142,14 @@ function findVariablesinAST(tree) {
|
|||
return result;
|
||||
}
|
||||
|
||||
function findVariables(string) {
|
||||
function findVariables(string: string): Set<string> {
|
||||
return findVariablesinAST(parser.parse(string));
|
||||
}
|
||||
|
||||
const extractedMessagesFiles = readMessageFiles(translationsDirectory);
|
||||
const extractedMessages = extractedMessagesFiles.reduce((acc, messageFile) => {
|
||||
messageFile.descriptors.forEach((descriptor) => {
|
||||
descriptor.descriptors.forEach((item) => {
|
||||
descriptor.descriptors?.forEach((item) => {
|
||||
const variables = findVariables(item.defaultMessage);
|
||||
acc.push({
|
||||
id: item.id,
|
||||
|
@ -147,20 +159,25 @@ const extractedMessages = extractedMessagesFiles.reduce((acc, messageFile) => {
|
|||
});
|
||||
});
|
||||
return acc;
|
||||
}, []);
|
||||
}, [] as ExtractedDescriptor[]);
|
||||
|
||||
const translations = languages.map((language) => {
|
||||
interface Translation {
|
||||
language: string,
|
||||
data: Record<string, string>,
|
||||
}
|
||||
|
||||
const translations: Translation[] = languages.map((language: string) => {
|
||||
return {
|
||||
language: language,
|
||||
data: JSON.parse(fs.readFileSync(path.join(translationsDirectory, language + '.json'), 'utf8')),
|
||||
};
|
||||
});
|
||||
|
||||
function difference(a, b) {
|
||||
return new Set([...a].filter(x => !b.has(x)));
|
||||
function difference<T>(a: Set<T>, b: Set<T>): Set<T> {
|
||||
return new Set(Array.from(a).filter(x => !b.has(x)));
|
||||
}
|
||||
|
||||
function pushIfUnique(arr, newItem) {
|
||||
function pushIfUnique<T>(arr: T[], newItem: T): void {
|
||||
if (arr.every((item) => {
|
||||
return (JSON.stringify(item) !== JSON.stringify(newItem));
|
||||
})) {
|
||||
|
@ -168,18 +185,25 @@ function pushIfUnique(arr, newItem) {
|
|||
}
|
||||
}
|
||||
|
||||
const problems = translations.reduce((acc, translation) => {
|
||||
interface Problem {
|
||||
language: string,
|
||||
id: ExtractedDescriptor['id'],
|
||||
severity: 'error' | 'warning',
|
||||
type: string,
|
||||
}
|
||||
|
||||
const problems: Problem[] = translations.reduce((acc, translation) => {
|
||||
extractedMessages.forEach((message) => {
|
||||
try {
|
||||
const translationVariables = findVariables(translation.data[message.id]);
|
||||
if ([...difference(translationVariables, message.variables)].length > 0) {
|
||||
const translationVariables = findVariables(translation.data[message.id!]);
|
||||
if (Array.from(difference(translationVariables, message.variables)).length > 0) {
|
||||
pushIfUnique(acc, {
|
||||
language: translation.language,
|
||||
id: message.id,
|
||||
severity: 'error',
|
||||
type: 'missing variable ',
|
||||
});
|
||||
} else if ([...difference(message.variables, translationVariables)].length > 0) {
|
||||
} else if (Array.from(difference(message.variables, translationVariables)).length > 0) {
|
||||
pushIfUnique(acc, {
|
||||
language: translation.language,
|
||||
id: message.id,
|
||||
|
@ -197,7 +221,7 @@ const problems = translations.reduce((acc, translation) => {
|
|||
}
|
||||
});
|
||||
return acc;
|
||||
}, []);
|
||||
}, [] as Problem[]);
|
||||
|
||||
if (problems.length > 0) {
|
||||
console.error(`${problems.length} messages found with errors or warnings:`);
|
|
@ -0,0 +1,154 @@
|
|||
declare module 'react-intl-translations-manager' {
|
||||
import type { MessageDescriptor } from 'react-intl';
|
||||
|
||||
export interface ExtractedDescriptor extends Omit<MessageDescriptor, 'defaultMessage'> {
|
||||
variables: Set<any>,
|
||||
descriptors?: ExtractedDescriptor[],
|
||||
defaultMessage: string,
|
||||
}
|
||||
|
||||
export interface ExtractedMessage {
|
||||
path: string,
|
||||
descriptors: ExtractedDescriptor[],
|
||||
}
|
||||
|
||||
export interface ManageTranslationsConfig {
|
||||
/**
|
||||
* Directory where the babel plugin puts the extracted messages. This path is relative to your projects root.
|
||||
*
|
||||
* example: `src/locales/extractedMessages`
|
||||
*/
|
||||
messagesDirectory: string,
|
||||
/**
|
||||
* Directory of the translation files the translation manager needs to maintain.
|
||||
*
|
||||
* example: `src/locales/lang`
|
||||
*/
|
||||
translationsDirectory: string,
|
||||
/**
|
||||
* Directory of the whitelist files the translation manager needs to maintain. These files contain the key of translations that have the exact same text in a specific language as the defaultMessage. Specifying this key will suppress `unmaintained translation` warnings.
|
||||
*
|
||||
* example: `Dashboard` in english is also accepted as a valid translation for dutch.
|
||||
*
|
||||
* (optional, default: `translationsDirectory`)
|
||||
*/
|
||||
whitelistsDirectory?: string,
|
||||
/**
|
||||
* What languages the translation manager needs to maintain. Specifying no languages actually doesn't make sense, but won't break the translationManager either. (Please do not include the default language, react-intl will automatically include it.)
|
||||
*
|
||||
* example: for `['nl', 'fr']` the translation manager will maintain a `nl.json`, `fr.json`, `whitelist_nl.json` and a w`hitelist_fr.json` file
|
||||
*
|
||||
* (optional, default: `[]`)
|
||||
*/
|
||||
languages?: string[],
|
||||
/**
|
||||
* Option to output a single JSON file containing the aggregate of all extracted messages, grouped by the file they were extracted from.
|
||||
*
|
||||
* example:
|
||||
*
|
||||
* ```json
|
||||
* [
|
||||
* {
|
||||
* "path": "src/components/foo.json",
|
||||
* "descriptors": [
|
||||
* {
|
||||
* "id": "bar",
|
||||
* "description": "Text for bar",
|
||||
* "defaultMessage": "Bar"
|
||||
* }
|
||||
* ]
|
||||
* }
|
||||
* ]
|
||||
* ```
|
||||
*
|
||||
* (optional, default: `false`)
|
||||
*/
|
||||
singleMessagesFile?: boolean,
|
||||
/**
|
||||
* If you want the translationManager to log duplicate message ids or not
|
||||
*
|
||||
* (optional, default: `true`)
|
||||
*/
|
||||
detectDuplicateIds?: boolean,
|
||||
/**
|
||||
* If you want the translationManager to sort it's output, both json and console output
|
||||
*
|
||||
* (optional, default: `true`)
|
||||
*/
|
||||
sortKeys?: boolean,
|
||||
/** (optional, default: `{ space: 2, trailingNewline: false }`)) */
|
||||
jsonOptions?: any,
|
||||
/**
|
||||
* Here you can specify custom logging methods. If not specified a default printer is used.
|
||||
*
|
||||
* Possible printers to configure:
|
||||
*
|
||||
* ```js
|
||||
* const printers = {
|
||||
* printDuplicateIds: duplicateIds => {
|
||||
* console.log(`You have ${duplicateIds.length} duplicate IDs`);
|
||||
* },
|
||||
* printLanguageReport: report => {
|
||||
* console.log('Log report for a language');
|
||||
* },
|
||||
* printNoLanguageFile: lang => {
|
||||
* console.log(
|
||||
* `No existing ${lang} translation file found. A new one is created.`
|
||||
* );
|
||||
* },
|
||||
* printNoLanguageWhitelistFile: lang => {
|
||||
* console.log(`No existing ${lang} file found. A new one is created.`);
|
||||
* }
|
||||
* };
|
||||
* ```
|
||||
*
|
||||
* (optional, default: `{}`)
|
||||
*/
|
||||
overridePrinters?: any,
|
||||
/**
|
||||
* Here you can specify overrides for the core hooks. If not specified, the default methods will be used.
|
||||
*
|
||||
* Possible overrides to configure:
|
||||
*
|
||||
* ```js
|
||||
* const overrideCoreMethods = {
|
||||
* provideExtractedMessages: () => {},
|
||||
* outputSingleFile: () => {},
|
||||
* outputDuplicateKeys: () => {},
|
||||
* beforeReporting: () => {},
|
||||
* provideLangTemplate: () => {},
|
||||
* provideTranslationsFile: () => {},
|
||||
* provideWhitelistFile: () => {},
|
||||
* reportLanguage: () => {},
|
||||
* afterReporting: () => {}
|
||||
* };
|
||||
* ```
|
||||
*/
|
||||
overrideCoreMethods?: any,
|
||||
}
|
||||
|
||||
/** This will maintain all translation files. Based on your config you will get output for duplicate ids, and per specified language you will get the deleted translations, added messages (new messages that need to be translated), and not yet translated messages. It will also maintain a whitelist file per language where you can specify translation keys where the translation is identical to the default message. This way you can avoid untranslated message warnings for these messages. */
|
||||
export default function manageTranslations(config: ManageTranslationsConfig): void;
|
||||
|
||||
/**
|
||||
* This is a `babel-plugin-react-intl` specific helper method. It will read all extracted JSON file for the specified directory, filter out all files without any messages, and output an array with all messages.
|
||||
*
|
||||
* Example output:
|
||||
*
|
||||
* ```js
|
||||
* const extractedMessages = [
|
||||
* {
|
||||
* path: 'src/components/Foo.json',
|
||||
* descriptors: [
|
||||
* {
|
||||
* id: 'foo_ok',
|
||||
* description: 'Ok text',
|
||||
* defaultMessage: 'OK'
|
||||
* }
|
||||
* ]
|
||||
* }
|
||||
* ];
|
||||
* ```
|
||||
*/
|
||||
export function readMessageFiles(messagesDirectory: string): ExtractedMessage[];
|
||||
}
|
Ładowanie…
Reference in New Issue