Skip to content
This repository has been archived by the owner on May 2, 2022. It is now read-only.

Sync missing translation keys in app/locales/*.json files to a Google Sheet #481

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
port lost-in-translation.py to javascript for consistency
  • Loading branch information
michaelmcmillan committed Mar 29, 2020
commit 5a50bc14d1123414f86b8164d6417919cef42a44
93 changes: 93 additions & 0 deletions ops/lost-in-translation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
const { spawnSync } = require('child_process');

/**
* Calls git cli with arguments and returns stdout.
*/
function git(...args) {
const { stdout } = spawnSync('git', args, { encoding: 'utf-8'});
return stdout;
}

/**
* Returns the commit hashes for a file path across all local branches.
*/
function findCommitHashesForFile(filePath) {
const output = git('log', '--pretty=format:"%h"', '--all', '--', filePath);
const hashes = output.split('\n').map(line => line.replace(/"/g, ''));
return hashes;
}

/**
* Returns the JSON representation of the contents of a file at certain commit hash.
*/
function retrieveJSONForFileAtCommitHash(filePath, commitHash) {
const contentsAtCommitHash = git('show', `${commitHash}:${filePath}`);
try {
return JSON.parse(contentsAtCommitHash);
} catch (error) {
// Sometimes the file is not in a proper JSON format. Simply return nothing in that case.
return {};
}
}

/**
* Step 1: Find all the (english) translation keys across all branches and PRs.
*
* If a Dutch developer has made a feature in a branch, we expect that him/her added a key
* to one or many locale files (usually the english locale (en.json) or to their own locale
* file (nl.json), or both).
*
* But they probably haven't added translations to all the other locales, because they don't
* speak the other languages. And this is the problem, because now we need find people to help
* translate the added keys to all the other locales.
*
* So what we do here is simply to find *all* the english translation keys across the entire
* project. That means looking at all the english translations keys in all the locale files
* since the start of the project across all branches and PRs. We throw them into a set that
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds unnecessary to me, as it would also include WIP's and stale branches. But it might be some cases where this makes sense that I have not thought about?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's the part that sucks, but having it would make sure that all translations are added and (hopefully) translated by the time we want to merge it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, I understand. So a possible solution would be to run this if this run as a cronjob or something, so that the sheet is updated as soon as there are new texts in the repo?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, so in the case with Stano's PR, those translations are now added to the sheet (even though his PR is WIP). Not sure if we want that or not, but we could try it out?

* we use in the next step.
*/
allLocales = new Set(['no', 'se']);
allEnglishTranslationKeys = new Set([]);
for (const locale of allLocales) {
const filePath = `../app/locales/${locale}.json`;
for (const commitHash of findCommitHashesForFile(filePath)) {
const translation = retrieveJSONForFileAtCommitHash(filePath, commitHash);
for (const translationKey of Object.keys(translation)) {
allEnglishTranslationKeys.add(translationKey);
}
}
}

/**
* Step 2: For each locale file check if there are any missing english translation keys
* across all branches and PRs and all commits/changes.
*
* If there's a missing english translation key, we know that we're most likely missing
* a translation for that locale. So what we need to do is to translate it, or get help
* to translate it.
*
* We throw it into a dictionary where the key is the locale and the value is a set of
* missing translations from english to that locale.
*/
const translationKeysByLocale = {};
for (const locale of allLocales) {
const filePath = `../app/locales/${locale}.json`;
for (const commitHash of findCommitHashesForFile(filePath)) {
const translation = retrieveJSONForFileAtCommitHash(filePath, commitHash);
if (translationKeysByLocale[locale] === undefined) {
translationKeysByLocale[locale] = new Set([]);
}
translationKeysByLocale[locale] = new Set([...translationKeysByLocale[locale], ...Object.keys(translation)]);
}
}

/**
* Step 3: Print out the missing keys for each locale.
*/
for (const locale of allLocales) {
for (const englishTranslationKey of allEnglishTranslationKeys) {
if (!translationKeysByLocale[locale].has(englishTranslationKey)) {
console.log(locale, 'missing translation for:', englishTranslationKey);
}
}
}