fix(tools): Integrate certificate and comment dictionaries into the Curriculum project on Crowdin (#40872)

* fix: integrate certificate and dictionary files to Crowdin

* fix: correct linting issue

* fix: improved the isReserveredHeading function
This commit is contained in:
Randell Dawson
2021-02-02 20:50:04 -07:00
committed by GitHub
parent 4d48fb5ce7
commit 2f207c3827
4 changed files with 83 additions and 25 deletions

View File

@ -10,8 +10,15 @@ files: [
"source" : "/curriculum/challenges/english/**/*.md", "source" : "/curriculum/challenges/english/**/*.md",
"translation" : "/curriculum/challenges/%language%/**/%original_file_name%", "translation" : "/curriculum/challenges/%language%/**/%original_file_name%",
"ignore": [ "ignore": [
"/**/part-[0-9][0-9][0-9].md", "/**/part-[0-9][0-9][0-9].md"
"/curriculum/challenges/english/[0-9][0-9]-certificates/**/*.*"
] ]
} },
{
"source" : "/curriculum/challenges/english/12-certificates/**/*.yml",
"translation" : "/curriculum/challenges/%language%/12-certificates/**/%original_file_name%",
},
{
"source" : "/curriculum/dictionaries/english/comments.json",
"translation" : "/curriculum/dictionaries/%language%/%original_file_name%"
},
] ]

View File

@ -1,5 +1,4 @@
require('dotenv').config({ path: `${__dirname}/../../.env` }); require('dotenv').config({ path: `${__dirname}/../../.env` });
// const core = require('@actions/core');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const matter = require('gray-matter'); const matter = require('gray-matter');
@ -20,7 +19,13 @@ const createChallengeTitleLookup = (
const { const {
data: { title: challengeTitle } data: { title: challengeTitle }
} = matter(challengeContent); } = matter(challengeContent);
return { ...lookup, [fileId]: challengeTitle }; return {
...lookup,
[fileId]: {
crowdinFilePath,
challengeTitle
}
};
} catch (err) { } catch (err) {
console.log(err.name); console.log(err.name);
console.log(err.message); console.log(err.message);
@ -39,8 +44,15 @@ const hideNonTranslatedStrings = async projectId => {
const crowdinStrings = await getStrings({ projectId }); const crowdinStrings = await getStrings({ projectId });
if (crowdinStrings && crowdinStrings.length) { if (crowdinStrings && crowdinStrings.length) {
for (let string of crowdinStrings) { for (let string of crowdinStrings) {
const challengeTitle = challengeTitleLookup[string.data.fileId]; const { crowdinFilePath, challengeTitle } = challengeTitleLookup[
await updateFileString({ projectId, string, challengeTitle }); string.data.fileId
];
await updateFileString({
projectId,
string,
challengeTitle,
crowdinFilePath
});
} }
} }
} }

View File

@ -1,5 +1,4 @@
const makeRequest = require('./make-request'); const makeRequest = require('./make-request');
const delay = require('./delay');
const authHeader = require('./auth-header'); const authHeader = require('./auth-header');
const addFile = async (projectId, filename, fileContent, directoryId) => { const addFile = async (projectId, filename, fileContent, directoryId) => {
@ -88,7 +87,6 @@ const getFiles = async projectId => {
let files = []; let files = [];
while (!done) { while (!done) {
const endPoint = `projects/${projectId}/files?limit=500&offset=${offset}`; const endPoint = `projects/${projectId}/files?limit=500&offset=${offset}`;
await delay(1000);
const response = await makeRequest({ const response = await makeRequest({
method: 'get', method: 'get',
endPoint, endPoint,

View File

@ -1,11 +1,36 @@
const authHeader = require('./auth-header'); const authHeader = require('./auth-header');
const makeRequest = require('./make-request'); const makeRequest = require('./make-request');
const isHeading = str => /\h\d/.test(str); const isReservedHeading = (context, str) => {
const reservedHeadings = [
'after-user-code',
'answers',
'before-user-code',
'description',
'fcc-editable-region',
'hints',
'instructions',
'question',
'seed',
'seed-contents',
'solutions',
'text',
'video-solution'
];
const captureGroupStr = `(${reservedHeadings.join('|')})`;
const regex = new RegExp(`--${captureGroupStr}--`);
return !!(context.match(/^Headline/) && str.match(regex));
};
const isCode = str => /^\/pre\/code|\/code$/.test(str); const isCode = str => /^\/pre\/code|\/code$/.test(str);
const shouldHide = (text, context, challengeTitle) => { const isTitle = str => /^(tests\s*->\s*\d+\s*)?->\s*title/.test(str);
if (isHeading(context) || isCode(context)) {
const shouldHide = (text, context, challengeTitle, crowdinFilePath) => {
if (crowdinFilePath.endsWith('.yml')) {
return !isTitle(context);
}
if (isReservedHeading(context, text) || isCode(context)) {
return true; return true;
} }
return text !== challengeTitle && context.includes('id=front-matter'); return text !== challengeTitle && context.includes('id=front-matter');
@ -13,18 +38,29 @@ const shouldHide = (text, context, challengeTitle) => {
const getStrings = async ({ projectId, fileId }) => { const getStrings = async ({ projectId, fileId }) => {
let headers = { ...authHeader }; let headers = { ...authHeader };
let endPoint = `projects/${projectId}/strings?limit=500`; let done = false;
if (fileId) { let offset = 0;
endPoint += `&fileId=${fileId}`; let strings = [];
} while (!done) {
const strings = await makeRequest({ method: 'get', endPoint, headers }); let endPoint = `projects/${projectId}/strings?limit=500&offset=${offset}`;
if (strings.data) { if (fileId) {
return strings.data; endPoint += `&fileId=${fileId}`;
} else { }
const { error, errors } = strings; const response = await makeRequest({ method: 'get', endPoint, headers });
console.error(error ? error : errors); if (response.data) {
return null; if (response.data.length) {
strings = [...strings, ...response.data];
offset += 500;
} else {
done = true;
return strings;
}
} else {
const { error, errors } = response;
console.error(error ? error : errors);
}
} }
return null;
}; };
const updateString = async ({ projectId, stringId, propsToUpdate }) => { const updateString = async ({ projectId, stringId, propsToUpdate }) => {
@ -69,11 +105,16 @@ const updateFileStrings = async ({ projectId, fileId, challengeTitle }) => {
} }
}; };
const updateFileString = async ({ projectId, string, challengeTitle }) => { const updateFileString = async ({
projectId,
string,
challengeTitle,
crowdinFilePath
}) => {
const { const {
data: { id: stringId, text, isHidden, context } data: { id: stringId, text, isHidden, context }
} = string; } = string;
const hideString = shouldHide(text, context, challengeTitle); const hideString = shouldHide(text, context, challengeTitle, crowdinFilePath);
if (!isHidden && hideString) { if (!isHidden && hideString) {
await changeHiddenStatus(projectId, stringId, true); await changeHiddenStatus(projectId, stringId, true);
console.log( console.log(