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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Next Next commit
sync all available sheets
  • Loading branch information
michaelmcmillan committed Mar 29, 2020
commit 6ecc6fabc6beb114add66c60c32f91f36d97001a
27 changes: 19 additions & 8 deletions ops/lost-in-translation.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ async function addUniqueRowsToGoogleSheet(sheet, rowsToAdd) {
const uniqueRowsToAdd = rowsToAdd.filter(rowToAdd =>
!alreadyAddedRows.find(alreadyAddedRow => alreadyAddedRow.key === rowToAdd.key));
await sheet.addRows(uniqueRowsToAdd);
return uniqueRowsToAdd;
}

/**
Expand Down Expand Up @@ -129,15 +130,25 @@ for (const locale of allLocales) {
const doc = new GoogleSpreadsheet('1ILFfc1DX4ujMnLnf9UqhwQGM9Ke3s1cAWciy8VqMHZw');
await doc.useServiceAccountAuth(require('./coronastatus-translation-486cef09736e-credentials.json'));
await doc.loadInfo();
const sheet = doc.sheetsByIndex[0];

const rows = [];
const locale = 'no';
for (const englishTranslationKey of allEnglishTranslationKeys) {
if (!translationKeysByLocale[locale].has(englishTranslationKey)) {
const row = { 'key': englishTranslationKey, translation: '' };
rows.push(row);
const allLocales = retrieveAllLocales('app/locales/');
for (let sheetIndex = 0; sheetIndex < doc.sheetCount; sheetIndex++) {
const sheet = doc.sheetsByIndex[sheetIndex];
for (const locale of allLocales) {
if (sheet.title !== locale) {
Copy link
Member

Choose a reason for hiding this comment

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

If I understand it correctly, there is one sheet per locale?

Is it then necessary to loop through the sheets here, can't we just loop through the locales and try to add them (from the code it seems like it throws an error if it does not exist)?

try {
          await doc.addSheet({ title: locale, headerValues: ['key', 'translation'] });
        } catch (error) {
          // We don't do anything if the sheet for this locale exists.
        }

Copy link
Member Author

Choose a reason for hiding this comment

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

That's correct, take a look at the sheet here: https://docs.google.com/spreadsheets/d/1ILFfc1DX4ujMnLnf9UqhwQGM9Ke3s1cAWciy8VqMHZw/edit#gid=462835362

The reason I'm looping over locales is because that way we will create sheets for new locales automatically. There is no way in the API to retrieve the sheet names in the spreadsheet. This way we don't have to worry about keeping locales and sheets in sync (it will sort it self out).

Copy link
Member Author

Choose a reason for hiding this comment

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

Didn't read your comment thoroughly enough. Yeah, maybe we can get away without that loop.

Copy link
Member Author

Choose a reason for hiding this comment

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

Haha getting late... The reason we have to loop over those are because there is no way of retrieving a sheet by its name. Does that make sense? The doc.addSheet doesn't return the sheet if it doesn't exist, it only throws an exception. So we have to, at some point, look at all the sheets to check if they match the current locale we are looping over.

Copy link
Member

Choose a reason for hiding this comment

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

I see, I thought the for-loop ended after adding a sheet. A little hard to wrap my head around the logic, but as long as it works it is more than good enough.

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree. Just refactored it, better now?

continue;
}

const rows = [];
for (const englishTranslationKey of allEnglishTranslationKeys) {
if (!translationKeysByLocale[locale].has(englishTranslationKey)) {
const row = { 'key': englishTranslationKey, translation: '' };
rows.push(row);
}
}
const addedRows = await addUniqueRowsToGoogleSheet(sheet, rows);
console.log(`Added ${addedRows.length} of ${rows.length} missing translations to the ${locale} sheet.`);
}
}
await addUniqueRowsToGoogleSheet(sheet, rows);

})();