Transifex upload (#76)
* add utility method to upload translation strings * use update url when the resource exists * pass token as a parameter to the upload method
This commit is contained in:
parent
9738f36bf2
commit
9ffb883cc1
4 changed files with 2553 additions and 230 deletions
73
dist/bundle.es.js
vendored
73
dist/bundle.es.js
vendored
|
@ -1048,6 +1048,78 @@ function userStateSyncMiddleware() {
|
|||
};
|
||||
}
|
||||
|
||||
const apiBaseUrl = 'https://www.transifex.com/api/2/project';
|
||||
const resource = 'app-strings';
|
||||
function doTransifexUpload(contents, project, token, success, fail) {
|
||||
const url = `${apiBaseUrl}/${project}/resources/`;
|
||||
const updateUrl = `${apiBaseUrl}/${project}/resource/${resource}/content/`;
|
||||
const headers = {
|
||||
Authorization: `Basic ${Buffer.from(`api:${token}`).toString('base64')}`,
|
||||
'Content-Type': 'application/json'
|
||||
};
|
||||
const req = {
|
||||
accept_translations: true,
|
||||
i18n_type: 'KEYVALUEJSON',
|
||||
name: resource,
|
||||
slug: resource,
|
||||
content: contents
|
||||
};
|
||||
|
||||
function handleResponse(text) {
|
||||
let json;
|
||||
|
||||
try {
|
||||
// transifex api returns Python dicts for some reason.
|
||||
// Any way to get the api to return valid JSON?
|
||||
json = JSON.parse(text);
|
||||
} catch (e) {// ignore
|
||||
}
|
||||
|
||||
if (success) {
|
||||
success(json || text);
|
||||
}
|
||||
}
|
||||
|
||||
function handleError(err) {
|
||||
if (fail) {
|
||||
fail(err.message ? err.message : 'Could not upload strings resource to Transifex');
|
||||
}
|
||||
} // check if the resource exists
|
||||
|
||||
|
||||
fetch(updateUrl, {
|
||||
headers
|
||||
}).then(response => response.json()).then(() => {
|
||||
// perform an update
|
||||
fetch(updateUrl, {
|
||||
method: 'PUT',
|
||||
headers,
|
||||
body: JSON.stringify({
|
||||
content: contents
|
||||
})
|
||||
}).then(response => {
|
||||
if (response.status !== 200 && response.status !== 201) {
|
||||
throw new Error('failed to update transifex');
|
||||
}
|
||||
|
||||
return response.text();
|
||||
}).then(handleResponse).catch(handleError);
|
||||
}).catch(() => {
|
||||
// resource doesn't exist, create a fresh resource
|
||||
fetch(url, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: JSON.stringify(req)
|
||||
}).then(response => {
|
||||
if (response.status !== 200 && response.status !== 201) {
|
||||
throw new Error('failed to upload to transifex');
|
||||
}
|
||||
|
||||
return response.text();
|
||||
}).then(handleResponse).catch(handleError);
|
||||
});
|
||||
}
|
||||
|
||||
function doGenerateAuthToken(installationId) {
|
||||
return dispatch => {
|
||||
dispatch({
|
||||
|
@ -3430,6 +3502,7 @@ exports.doSetViewMode = doSetViewMode;
|
|||
exports.doShowSuggestedSubs = doShowSuggestedSubs;
|
||||
exports.doSyncApply = doSyncApply;
|
||||
exports.doSyncEncryptAndDecrypt = doSyncEncryptAndDecrypt;
|
||||
exports.doTransifexUpload = doTransifexUpload;
|
||||
exports.doUpdateUnreadSubscriptions = doUpdateUnreadSubscriptions;
|
||||
exports.doUpdateUploadProgress = doUpdateUploadProgress;
|
||||
exports.doUserCheckEmailVerified = doUserCheckEmailVerified;
|
||||
|
|
2629
dist/bundle.js
vendored
2629
dist/bundle.js
vendored
File diff suppressed because it is too large
Load diff
|
@ -13,6 +13,9 @@ export { LBRYINC_ACTIONS, YOUTUBE_STATUSES };
|
|||
// Lbryio and rewards
|
||||
export { Lbryio, rewards };
|
||||
|
||||
// utils
|
||||
export { doTransifexUpload } from 'util/transifex-upload';
|
||||
|
||||
// actions
|
||||
export { doGenerateAuthToken } from 'redux/actions/auth';
|
||||
export {
|
||||
|
|
78
src/util/transifex-upload.js
Normal file
78
src/util/transifex-upload.js
Normal file
|
@ -0,0 +1,78 @@
|
|||
const apiBaseUrl = 'https://www.transifex.com/api/2/project';
|
||||
const resource = 'app-strings';
|
||||
|
||||
export function doTransifexUpload(contents, project, token, success, fail) {
|
||||
const url = `${apiBaseUrl}/${project}/resources/`;
|
||||
const updateUrl = `${apiBaseUrl}/${project}/resource/${resource}/content/`;
|
||||
const headers = {
|
||||
Authorization: `Basic ${Buffer.from(`api:${token}`).toString('base64')}`,
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
|
||||
const req = {
|
||||
accept_translations: true,
|
||||
i18n_type: 'KEYVALUEJSON',
|
||||
name: resource,
|
||||
slug: resource,
|
||||
content: contents,
|
||||
};
|
||||
|
||||
function handleResponse(text) {
|
||||
let json;
|
||||
try {
|
||||
// transifex api returns Python dicts for some reason.
|
||||
// Any way to get the api to return valid JSON?
|
||||
json = JSON.parse(text);
|
||||
} catch (e) {
|
||||
// ignore
|
||||
}
|
||||
|
||||
if (success) {
|
||||
success(json || text);
|
||||
}
|
||||
}
|
||||
|
||||
function handleError(err) {
|
||||
if (fail) {
|
||||
fail(err.message ? err.message : 'Could not upload strings resource to Transifex');
|
||||
}
|
||||
}
|
||||
|
||||
// check if the resource exists
|
||||
fetch(updateUrl, { headers })
|
||||
.then(response => response.json())
|
||||
.then(() => {
|
||||
// perform an update
|
||||
fetch(updateUrl, {
|
||||
method: 'PUT',
|
||||
headers,
|
||||
body: JSON.stringify({ content: contents }),
|
||||
})
|
||||
.then(response => {
|
||||
if (response.status !== 200 && response.status !== 201) {
|
||||
throw new Error('failed to update transifex');
|
||||
}
|
||||
|
||||
return response.text();
|
||||
})
|
||||
.then(handleResponse)
|
||||
.catch(handleError);
|
||||
})
|
||||
.catch(() => {
|
||||
// resource doesn't exist, create a fresh resource
|
||||
fetch(url, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: JSON.stringify(req),
|
||||
})
|
||||
.then(response => {
|
||||
if (response.status !== 200 && response.status !== 201) {
|
||||
throw new Error('failed to upload to transifex');
|
||||
}
|
||||
|
||||
return response.text();
|
||||
})
|
||||
.then(handleResponse)
|
||||
.catch(handleError);
|
||||
});
|
||||
}
|
Loading…
Add table
Reference in a new issue