Skip to content

Commit

Permalink
feat: use transifex v3 api to pull translations
Browse files Browse the repository at this point in the history
  • Loading branch information
Cori Hudson committed Nov 18, 2022
1 parent eb25805 commit c05b28b
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 20 deletions.
30 changes: 10 additions & 20 deletions i18n/sync_tx_translations.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,16 @@ if (!process.env.TX_TOKEN || process.argv.length !== 2) {
const fs = require('fs');
const path = require('path');
const assert = require('assert');
const transifex = require('transifex');
const locales = require('scratch-l10n').default;
const txPull = require('./transifex.js');

// Globals
const PATH_OUTPUT = path.resolve(__dirname, '../msg');
const PROJECT = 'scratch-editor'
const RESOURCE = 'blocks';
const MODE = {mode: 'reviewed'};
const MODE = 'reviewed';


const TX = new transifex({
project_slug: PROJECT,
credential: 'api:' + process.env.TX_TOKEN
});

let en = fs.readFileSync(path.resolve(__dirname, '../msg/json/en.json'));
en = JSON.parse(en);
Expand Down Expand Up @@ -88,20 +85,13 @@ let localeMap = {
'zh-tw': 'zh_TW'
};

function getLocaleData (locale) {
let txLocale = localeMap[locale] || locale;
return new Promise (function (resolve, reject) {
TX.translationInstanceMethod(PROJECT, RESOURCE, txLocale, MODE, function (err, data) {
if (err) {
reject(err);
} else {
resolve({
locale: locale,
translations: JSON.parse(data)
});
}
})
})
const getLocaleData = async function (locale) {
let txLocale = localeMap[locale] || locale;
const data = await txPull(PROJECT, RESOURCE, txLocale, MODE);
return {
locale: locale,
translations: data
};
};

Promise.all(Object.keys(locales).map(getLocaleData)).then(function (values) {
Expand Down
88 changes: 88 additions & 0 deletions i18n/transifex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/usr/bin/env babel-node

/**
* @fileoverview
* Utilities for interfacing with Transifex API 3.
* TODO: add functions for pushing to Transifex
*/

const transifexApi = require('@transifex/api').transifexApi;
const download = require('download');

const ORG_NAME = 'llk';
const SOURCE_LOCALE = 'en';

try {
transifexApi.setup({
auth: process.env.TX_TOKEN
});
} catch (err) {
if (!process.env.TX_TOKEN) {
throw new Error('TX_TOKEN is not defined.');
}
throw err;
}

/**
* Creates a download event for a specific project, resource, and locale.
* @param {string} projectSlug - project slug (for example, "scratch-editor")
* @param {string} resourceSlug - resource slug (for example, "blocks")
* @param {string} localeCode - language code (for example, "ko")
* @param {string} mode - translation status of strings to include
* @returns {string} - id of the created download event
*/
const downloadResource = async function (projectSlug, resourceSlug, localeCode, mode = 'default') {
const resource = {
data: {
id: `o:${ORG_NAME}:p:${projectSlug}:r:${resourceSlug}`,
type: 'resources'
}
};

// if locale is English, create a download event of the source file
if (localeCode === SOURCE_LOCALE) {
return await transifexApi.ResourceStringsAsyncDownload.download({
resource
});
}

const language = {
data: {
id: `l:${localeCode}`,
type: 'languages'
}
};

// if locale is not English, create a download event of the translation file
return await transifexApi.ResourceTranslationsAsyncDownload.download({
mode,
resource,
language
});
};

/**
* Pulls a translation json from transifex, for a specific project, resource, and locale.
* @param {string} project - project slug (for example, "scratch-editor")
* @param {string} resource - resource slug (for example, "blocks")
* @param {string} locale - language code (for example, "ko")
* @param {string} mode - translation status of strings to include
* @returns {object} - JSON object of translated resource strings (or, of the original resourse
* strings, if the local is the source language)
*/
const txPull = async function (project, resource, locale, mode = 'default') {
const url = await downloadResource(project, resource, locale, mode);
let buffer;
for (let i = 0; i < 5; i++) {
try {
buffer = await download(url);
return JSON.parse(buffer.toString());
} catch (e) {
process.stdout.write(`got ${e.message}, retrying after ${i + 1} failed attempt(s)\n`);
}
}
throw Error('failed to pull after 5 retries');
};

module.exports = txPull;

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@
"scratch-l10n": "3.15.20221024032103"
},
"devDependencies": {
"@transifex/api": "4.2.5",
"async": "2.6.4",
"chromedriver": "105.0.1",
"copy-webpack-plugin": "4.6.0",
"download": "^8.0.0",
"eslint": "4.19.1",
"event-stream": "3.3.5",
"gh-pages": "0.12.0",
Expand Down

0 comments on commit c05b28b

Please # to comment.