Skip to content

Commit

Permalink
add the ability to check progress for multiple required languages
Browse files Browse the repository at this point in the history
  • Loading branch information
RuslanSivalnev authored and yuriykuzin committed Aug 7, 2023
1 parent e9dcc40 commit a61959f
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 21 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ Crowdin and the Crowdin API are the property of Crowdin, LLC.

- [ ] Restore/fix temporary removed specs

## Changes in 3.0.0

- Added the ability to check progress for multiple required languages
- Breaking changes: remade config variable from `{"languageToCheck": string}` to `{"languagesToCheck": sting[]}`

## Changes in 2.0.0

- Migrated to Crowdin API v2
Expand Down Expand Up @@ -41,7 +46,7 @@ Add crowdin-helper.json to the root of your project. Here is an example:
"token": "my-personal-access-token",
"source": "/src/i18n/en.json",
"translation": "/src/i18n/%two_letters_code%.json",
"languageToCheck": "nl",
"languagesToCheck": ["nl"],
"languagesToAutoTranslate": ["nl", "fi"],
"minutesSinceLastMasterMergeToPurgeSafely": 20,
"disableAutoTranslation": false,
Expand Down
45 changes: 29 additions & 16 deletions lib/commands/check-progress-on-branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,59 @@ async function checkProgressOnBranch() {
await triggerAutoTranslation(MASTER_BRANCH_NAME);

console.log(
`Crowdin: ${COLORS.GREEN}No validation performed since it is a master branch${COLORS.WHITE}`,
`Crowdin: ${COLORS.GREEN}No validation performed since it is a master branch${COLORS.RESET}`,
);

return;
}

console.log(`Crowdin: Checking language: ${configManager.get('languageToCheck')}`);
const requiredLanguageList = configManager.get('languagesToCheck');

console.log(`Crowdin: Checking language(s): ${COLORS.CYAN}${requiredLanguageList.join(`${COLORS.RESET}, ${COLORS.CYAN}`)}${COLORS.RESET}`);

const branchData = await CrowdinApi.getBranchData(crowdinBranchName);

if (!branchData) {
console.log(`Crowdin: ${COLORS.GREEN}Okay, no such branch on crowdin${COLORS.WHITE}`);
console.log(`Crowdin: ${COLORS.GREEN}Okay, no such branch on crowdin${COLORS.RESET}`);

return;
}

if (requiredLanguageList.length === 0) {
console.log(`Crowdin: ${COLORS.GREEN}Okay, no required languages to translate${COLORS.RESET}`);

return;
}

const branchProgress = await CrowdinApi.getBranchProgress(branchData.data.id);

const currentLanguageProgress = branchProgress.data.filter(
(dataLanguage) => dataLanguage.data.languageId === configManager.get('languageToCheck'),
)[0];
const requiredLanguageProgressList = branchProgress.data.filter(
(dataLanguage) => requiredLanguageList.includes(dataLanguage.data.languageId),
);

if (currentLanguageProgress.data.phrases.total === 0) {
console.log(`Crowdin: ${COLORS.GREEN}Okay, no new phrases in this branch${COLORS.WHITE}`);
const hasLanguageWithNewPhrases = requiredLanguageProgressList.some(dataLanguage => dataLanguage.data.phrases.total > 0);
if (!hasLanguageWithNewPhrases) {
console.log(`Crowdin: ${COLORS.GREEN}Okay, no new phrases in this branch${COLORS.RESET}`);

return;
}

if (
currentLanguageProgress.data.phrases.translated === currentLanguageProgress.data.phrases.total
) {
console.log(`Crowdin: ${COLORS.GREEN}Okay, translations are ready${COLORS.WHITE}`);
const isRequiredLanguagesTranslated = requiredLanguageProgressList.every(dataLanguage => dataLanguage.data.phrases.translated === dataLanguage.data.phrases.total);
if (isRequiredLanguagesTranslated) {
console.log(`Crowdin: ${COLORS.GREEN}Okay, translations are ready${COLORS.RESET}`);

return;
}

console.log(
`Crowdin: translated ${currentLanguageProgress.data.phrases.translated} / ${currentLanguageProgress.data.phrases.total}`,
);
console.log(`Crowdin: ${COLORS.RED}Error: There are some missing translations${COLORS.WHITE}`);
requiredLanguageProgressList.forEach(dataLanguage => {
const translationStatusColor = dataLanguage.data.phrases.translated === dataLanguage.data.phrases.total ? COLORS.GREEN : COLORS.RED;

console.log(
`Crowdin: translated ${COLORS.CYAN}${dataLanguage.data.languageId}${COLORS.RESET}: ${translationStatusColor}${dataLanguage.data.phrases.translated} / ${dataLanguage.data.phrases.total}${COLORS.RESET}`,
);
});

console.log(`Crowdin: ${COLORS.RED}Error: There are some missing translations${COLORS.RESET}`);

process.exit(1);
}
Expand Down
2 changes: 2 additions & 0 deletions lib/utilities/colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const COLORS = {
GREEN: '\x1b[32m',
RED: '\x1b[31m',
WHITE: '\x1b[37m',
CYAN: '\x1b[36m',
RESET: '\x1b[0m',
};

module.exports = COLORS;
2 changes: 1 addition & 1 deletion lib/utilities/config-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const configManager = {
token: crowdinHelperJson.token,
sourceFilesPattern: crowdinHelperJson.source.replace(/^\//, ''),
translationPattern: crowdinHelperJson.translation,
languageToCheck: crowdinHelperJson.languageToCheck || '',
languagesToCheck: crowdinHelperJson.languagesToCheck || [],
languagesToAutoTranslate: crowdinHelperJson.languagesToAutoTranslate || [],
minutesSinceLastMasterMergeToPurgeSafely:
crowdinHelperJson.minutesSinceLastMasterMergeToPurgeSafely || 20,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "crowdin-helper",
"version": "2.0.3",
"version": "3.0.0",
"engines": {
"node": ">=10.0"
},
Expand Down
2 changes: 1 addition & 1 deletion test/__mocks__/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const MOCK_CONFIG_FILE_CONTENT_DEFAULT = `
"token": "my-personal-access-token",
"source": "/**/en.json",
"translation": "/sample-translation-folder/%two_letters_code%.json",
"languageToCheck": "nl",
"languagesToCheck": ["nl", "de"],
"languagesToAutoTranslate": ["nl", "fi"],
"minutesSinceLastMasterMergeToPurgeSafely": 20,
"disableAutoTranslation": false
Expand Down
2 changes: 1 addition & 1 deletion test/utilites/config-manager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('config-manager', () => {
expect(configManager.get('translationPattern')).toBe(
'/sample-translation-folder/%two_letters_code%.json',
);
expect(configManager.get('languageToCheck')).toBe('nl');
expect(configManager.get('languagesToCheck')).toStrictEqual(['nl', 'de']);
expect(configManager.get('languagesToAutoTranslate')).toEqual(['nl', 'fi']);
expect(configManager.get('minutesSinceLastMasterMergeToPurgeSafely')).toBe(20);
expect(configManager.get('disableAutoTranslation')).toBeFalsy();
Expand Down

0 comments on commit a61959f

Please sign in to comment.