Skip to content

Commit

Permalink
fix: add project ids to translations database based on frontmatter
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkmcadoo committed Nov 23, 2021
1 parent 264fbba commit 0ac8bd7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
45 changes: 36 additions & 9 deletions scripts/actions/add-files-to-translation-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const STATUS = {

const LOCALE_IDS = {
jp: 'ja-JP',
kr: 'kr-KR',
};

const translationDifference = (pendingFiles, prChanges) =>
Expand All @@ -32,15 +33,33 @@ const translationDifference = (pendingFiles, prChanges) =>
const slugIntersection = (pendingFiles, filesToRemove) =>
pendingFiles.filter((file) => filesToRemove.includes(file.slug));

const humanTranslatedProjectID = process.env.HUMAN_TRANSLATION_PROJECT_ID;
const machineTranslatedProjectID = process.env.MACHINE_TRANSLATION_PROJECT_ID;

/**
* Determines if a particular locale should be human or machine translated based on the files frontmatter.
*
* @param {String[]} translateFM
* @returns {Function => String} projectId
*/

const getProjectId = (translateFM) => (locale) => {
return Array.isArray(translateFM) && translateFM.includes(locale)
? humanTranslatedProjectID
: machineTranslatedProjectID;
};

const getLocalizedFileData = (prFile) => {
const contents = fs.readFileSync(path.join(process.cwd(), prFile.filename));
const { data } = frontmatter(contents);
return data.translate
? data.translate.map((locale) => ({
...prFile,
locale: LOCALE_IDS[locale],
}))
: [];
const checkLocale = getProjectId(data.translate);

// Loops over all supported locales, determines translation project via getProjectId func.
return Object.keys(LOCALE_IDS).map((locale) => ({
...prFile,
locale: LOCALE_IDS[locale],
project_id: checkLocale(locale),
}));
};

const removedFiles = (prFiles) =>
Expand All @@ -52,7 +71,7 @@ const main = async () => {
const url = process.argv[2];

const queue = await getTranslations({ status: STATUS.PENDING });

console.log('queue', queue);
const prFileData = await fetchPaginatedGHResults(
url,
process.env.GITHUB_TOKEN
Expand All @@ -63,14 +82,22 @@ const main = async () => {
.filter((f) => f.status !== 'removed');

const allLocalizedFileData = changedMdxFileData.flatMap(getLocalizedFileData);
console.log('allLocalizedFileData', allLocalizedFileData);
const fileDataToAddToQueue = translationDifference(
queue,
allLocalizedFileData
);

console.log('File Data To Add To Queue:', fileDataToAddToQueue);

await Promise.all(
fileDataToAddToQueue.map(({ filename, locale }) =>
addTranslation({ slug: filename, status: STATUS.PENDING, locale })
fileDataToAddToQueue.map(({ filename, locale, project_id }) =>
addTranslation({
slug: filename,
status: STATUS.PENDING,
locale,
project_id,
})
)
);

Expand Down
9 changes: 7 additions & 2 deletions scripts/actions/translation_workflow/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,13 @@ const getStatuses = async (filters = {}) => {
* @param {string} locale - string value of locale enum
* @returns newly created translation
*/
const addTranslation = async ({ slug, status, locale }) => {
const translation = await Models.Translation.create({ slug, status, locale });
const addTranslation = async ({ slug, status, locale, project_id }) => {
const translation = await Models.Translation.create({
slug,
status,
locale,
project_id,
});
return translation;
};

Expand Down

0 comments on commit 0ac8bd7

Please sign in to comment.