Last active
July 3, 2019 18:34
-
-
Save deleteman/1ecabd6cb753b281887575234ee8b5b8 to your computer and use it in GitHub Desktop.
Server side version of the auto-translate hook integration for ButterCMS and Google Translate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const READ_TOKEN = "your-read-api-key" | |
const WRITE_TOKEN = 'your-write-api-key' | |
const express = require("express") | |
const butter = require("buttercms")(READ_TOKEN) //read | |
const request = require("request") | |
const bodyParser = require("body-parser") | |
const {Translate} = require('@google-cloud/translate'); | |
const app = express(); | |
// The target language | |
const TARGET_LANG = 'es'; | |
app.use(bodyParser.json()) | |
async function translate(text, target) { | |
let projectId = 'page-translation-example' | |
// Imports the Google Cloud client library | |
// Instantiates a client | |
const translate = new Translate({projectId}); | |
const [translation] = await translate.translate(text, target); | |
console.log(`Text: ${text}`); | |
console.log(`Translation: ${translation}`); | |
return translation | |
} | |
app.post('/webhook1', async function (req, res) { | |
let pageSlug = req.body.data.id | |
let pageType = req.body.data.page_type | |
//get the original English version of the page | |
butter.page.retrieve(pageType, pageSlug, {locale: 'en'}).then( async (page) => { | |
let fields = Object.keys(page.data.data.fields) | |
//Setup the structure and default values for the tranlsated payload | |
let tPage = { | |
fields: { | |
"es": page.data.data.fields | |
}, | |
stauts: "draft", //we're going with a draft for the time being, just to make sure it works | |
"page-type": page.data.data.page_type | |
} | |
let translations = [] | |
for(let i = 0; i < fields.length; i++) { //perform the actual translation | |
let v = await translate(page.data.data.fields[fields[i]], TARGET_LANG) | |
tPage.fields["es"][fields[i]] = v //update the default values to turn them into spanish | |
translations.push(v) | |
} | |
let patchUrl = 'https://api.buttercms.com/v2/pages/*/' + page.data.data.slug + "/" | |
request.patch({ //perform the page Patch | |
url: patchUrl, | |
headers: { | |
"Authorization": "Token " + WRITE_TOKEN | |
}, | |
json: true, | |
body: tPage, | |
}, (err, resp) => { //We're done! | |
if(err) { | |
console.log("There was an error: ", err) | |
return res.json(err) | |
} | |
console.log("Done, patch finished") | |
console.log(resp) | |
res.json(resp) | |
}) | |
}).catch(err => { | |
console.log("There was an error: ", err) | |
res.json(err) | |
}) | |
}) | |
app.listen(3000, function () { | |
console.log('Example app listening on port 3000!'); | |
}); |
Updated the code. Moved the target language outside as a constant. The whole example is about translating content into spanish, that's why it's hardcoded. That being said, now it's more clear where to change things in order to make it dynamic.
As for the russian comment, removed. It was a copy&past mishap.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
async function translate(text)
should accepttarget
parameter so people can specify which language they want to translate to. Right now it's hardcoded.await translate.translate(text, target)
with the target locale passed ines
is Spanish