diff --git a/tools/crowdin/actions/hide-non-translated-strings/index.js b/tools/crowdin/actions/hide-non-translated-strings/index.js index 4deafc614c..04bce25bd6 100644 --- a/tools/crowdin/actions/hide-non-translated-strings/index.js +++ b/tools/crowdin/actions/hide-non-translated-strings/index.js @@ -4,31 +4,47 @@ const fs = require('fs'); const path = require('path'); const matter = require('gray-matter'); const { getFiles } = require('../../utils/files'); -const { updateFileStrings } = require('../../utils/strings'); +const { getStrings, updateFileString } = require('../../utils/strings'); + +const createChallengeTitleLookup = ( + lookup, + { fileId, path: crowdinFilePath } +) => { + const challengeFilePath = path.join( + __dirname, + '/../../../../', + crowdinFilePath + ); + try { + const challengeContent = fs.readFileSync(challengeFilePath); + const { + data: { title: challengeTitle } + } = matter(challengeContent); + return { ...lookup, [fileId]: challengeTitle }; + } catch (err) { + console.log(err.name); + console.log(err.message); + } + return lookup; +}; const hideNonTranslatedStrings = async projectId => { - console.log('start hiding non-translated strings...'); + console.log('hide non-translated strings...'); const crowdinFiles = await getFiles(projectId); if (crowdinFiles && crowdinFiles.length) { - for (let { fileId, path: crowdinFilePath } of crowdinFiles) { - const challengeFilePath = path.join( - __dirname, - '/../../../../', - crowdinFilePath - ); - try { - const challengeContent = fs.readFileSync(challengeFilePath); - const { - data: { title: challengeTitle } - } = matter(challengeContent); - await updateFileStrings({ projectId, fileId, challengeTitle }); - } catch (err) { - console.log(err.name); - console.log(err.message); + const challengeTitleLookup = crowdinFiles.reduce( + createChallengeTitleLookup, + {} + ); + const crowdinStrings = await getStrings({ projectId }); + if (crowdinStrings && crowdinStrings.length) { + for (let string of crowdinStrings) { + const challengeTitle = challengeTitleLookup[string.data.fileId]; + await updateFileString({ projectId, string, challengeTitle }); } } } - console.log('hiding non-translated strings complete'); + console.log('complete'); }; const projectId = process.env.CROWDIN_PROJECT_ID; diff --git a/tools/crowdin/utils/strings.js b/tools/crowdin/utils/strings.js index a0769ec183..4786d5ee4b 100644 --- a/tools/crowdin/utils/strings.js +++ b/tools/crowdin/utils/strings.js @@ -13,7 +13,10 @@ const shouldHide = (text, context, challengeTitle) => { const getStrings = async ({ projectId, fileId }) => { let headers = { ...authHeader }; - const endPoint = `projects/${projectId}/strings?fileId=${fileId}&limit=500`; + let endPoint = `projects/${projectId}/strings?limit=500`; + if (fileId) { + endPoint += `&fileId=${fileId}`; + } const strings = await makeRequest({ method: 'get', endPoint, headers }); if (strings.data) { return strings.data; @@ -66,9 +69,27 @@ const updateFileStrings = async ({ projectId, fileId, challengeTitle }) => { } }; +const updateFileString = async ({ projectId, string, challengeTitle }) => { + const { + data: { id: stringId, text, isHidden, context } + } = string; + const hideString = shouldHide(text, context, challengeTitle); + if (!isHidden && hideString) { + await changeHiddenStatus(projectId, stringId, true); + console.log( + `${challengeTitle} - stringId: ${stringId} - changed isHidden to true` + ); + } else if (isHidden && !hideString) { + await changeHiddenStatus(projectId, stringId, false); + console.log( + `${challengeTitle} - stringId: ${stringId} - changed isHidden to false` + ); + } +}; + module.exports = { getStrings, - changeHiddenStatus, updateString, - updateFileStrings + updateFileStrings, + updateFileString };