mirror of
https://github.com/scratchfoundation/scratch-l10n.git
synced 2025-03-14 07:00:36 -04:00
Merge pull request #175 from corihudson/ce-180-push-transifex-v3
CE-180: Push source strings with Transifex v3 API
This commit is contained in:
commit
50ce3a1772
2 changed files with 78 additions and 30 deletions
|
@ -3,7 +3,6 @@
|
|||
/**
|
||||
* @fileoverview
|
||||
* Utilities for interfacing with Transifex API 3.
|
||||
* TODO: add functions for pushing to Transifex
|
||||
*/
|
||||
|
||||
const transifexApi = require('@transifex/api').transifexApi;
|
||||
|
@ -104,4 +103,61 @@ const txResources = async function (project) {
|
|||
return slugs;
|
||||
};
|
||||
|
||||
module.exports = {txPull, txResources};
|
||||
/**
|
||||
* Uploads English source strings to a resource in transifex
|
||||
* @param {string} project - project slug (for example, "scratch-editor")
|
||||
* @param {string} resource - resource slug (for example, "blocks")
|
||||
* @param {object} sourceStrings - json of source strings
|
||||
*/
|
||||
const txPush = async function (project, resource, sourceStrings) {
|
||||
const resourceObj = {
|
||||
data: {
|
||||
id: `o:${ORG_NAME}:p:${project}:r:${resource}`,
|
||||
type: 'resources'
|
||||
}
|
||||
};
|
||||
|
||||
await transifexApi.ResourceStringsAsyncUpload.upload({
|
||||
resource: resourceObj,
|
||||
content: JSON.stringify(sourceStrings)
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new resource, and then uploads source strings to it if they are provided
|
||||
* @param {string} project - project slug (for example, "scratch-editor")
|
||||
* @param {object} resource - object of resource information
|
||||
* @param {string} resource.slug - resource slug (for example, "blocks")
|
||||
* @param {string} resource.name - resource name
|
||||
* @param {string} resource.i18nType - i18n format id
|
||||
* @param {object} resource.sourceStrings - json object of source strings
|
||||
*/
|
||||
const txCreateResource = async function (project, {slug, name, i18nType, sourceStrings}) {
|
||||
const i18nFormat = {
|
||||
data: {
|
||||
id: i18nType || 'KEYVALUEJSON',
|
||||
type: 'i18n_formats'
|
||||
}
|
||||
};
|
||||
|
||||
const projectObj = {
|
||||
data: {
|
||||
id: `o:${ORG_NAME}:p:${project}`,
|
||||
type: 'projects'
|
||||
}
|
||||
};
|
||||
|
||||
await transifexApi.Resource.create({
|
||||
attributes: {slug: slug, name: name},
|
||||
relationships: {
|
||||
i18n_format: i18nFormat,
|
||||
project: projectObj
|
||||
}
|
||||
});
|
||||
|
||||
if (sourceStrings) {
|
||||
await txPush(project, slug, sourceStrings);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {txPull, txPush, txResources, txCreateResource};
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const transifex = require('transifex');
|
||||
const {txPush, txCreateResource} = require('../lib/transifex.js');
|
||||
|
||||
const args = process.argv.slice(2);
|
||||
|
||||
|
@ -33,10 +33,6 @@ if (args.length < 3 || !process.env.TX_TOKEN) {
|
|||
// Globals
|
||||
const PROJECT = args[0];
|
||||
const RESOURCE = args[1];
|
||||
const TX = new transifex({
|
||||
project_slug: PROJECT,
|
||||
credential: 'api:' + process.env.TX_TOKEN
|
||||
});
|
||||
|
||||
let en = fs.readFileSync(path.resolve(args[2]));
|
||||
en = JSON.parse(en);
|
||||
|
@ -76,33 +72,29 @@ const getResourceType = (project, resource) => {
|
|||
};
|
||||
|
||||
// update Transifex with English source
|
||||
TX.uploadSourceLanguageMethod(PROJECT, RESOURCE, {content: JSON.stringify(en)}, (err) => {
|
||||
if (err && err.response.statusCode !== 404) {
|
||||
process.stdout.write(`Transifex Error: ${err.message}\n`);
|
||||
process.stdout.write(
|
||||
`Transifex Error ${err.response.statusCode.toString()}: ${err.response.body}\n`);
|
||||
process.exitCode = 1;
|
||||
return;
|
||||
}
|
||||
// file not found - create it, but also give message
|
||||
if (err && err.response.statusCode === 404) {
|
||||
const pushSource = async function () {
|
||||
try {
|
||||
await txPush(PROJECT, RESOURCE, en);
|
||||
} catch (err) {
|
||||
if (err.statusCode !== 404) {
|
||||
process.stdout.write(`Transifex Error: ${err.message}\n`);
|
||||
process.stdout.write(
|
||||
`Transifex Error ${err.response.statusCode.toString()}: ${err.response.body}\n`);
|
||||
process.exitCode = 1;
|
||||
return;
|
||||
}
|
||||
// file not found - create it, but also give message
|
||||
process.stdout.write(`Transifex Resource not found, creating: ${RESOURCE}\n`);
|
||||
const resourceData = {
|
||||
slug: RESOURCE,
|
||||
name: RESOURCE,
|
||||
priority: 0, // default to normal priority
|
||||
i18n_type: getResourceType(PROJECT, RESOURCE),
|
||||
content: JSON.stringify(en)
|
||||
i18nType: getResourceType(PROJECT, RESOURCE),
|
||||
content: en
|
||||
};
|
||||
TX.resourceCreateMethod(PROJECT, resourceData, (err1) => {
|
||||
if (err1) {
|
||||
process.stdout.write(`Transifex Error: ${err1.message}\n`);
|
||||
process.stdout.write(
|
||||
`Transifex Error ${err1.response.statusCode.toString()}: ${err1.response.body}\n`);
|
||||
process.exitCode = 1;
|
||||
return;
|
||||
}
|
||||
});
|
||||
await txCreateResource(PROJECT, resourceData);
|
||||
process.exitCode = 0;
|
||||
}
|
||||
process.exitCode = 0;
|
||||
});
|
||||
};
|
||||
|
||||
pushSource();
|
||||
|
|
Loading…
Reference in a new issue