-
-
Save codeibrahima/81726510be89238cbcb742be28a87af0 to your computer and use it in GitHub Desktop.
Language translation with S3 and AWS Translate
This file contains 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
/* | |
MIT No Attribution | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this | |
software and associated documentation files (the "Software"), to deal in the Software | |
without restriction, including without limitation the rights to use, copy, modify, | |
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to | |
permit persons to whom the Software is furnished to do so. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | |
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
Written by James Beswick (@jbesw) - contact if you have any questions! | |
*/ | |
const AWS = require('aws-sdk') | |
AWS.config.region = process.env.AWS_REGION | |
const s3 = new AWS.S3() | |
const translate = new AWS.Translate() | |
// Entire list of language codes at: https://docs.aws.amazon.com/translate/latest/dg/API_TranslateText.html | |
const targetLanguages = ['fr', 'de', 'it', 'es', 'sv', 'ar', 'pt', 'ru'] | |
// The standard Lambda handler | |
exports.handler = async (event) => { | |
// Don't fire for objects in the translations folder | |
if (event.Records[0].s3.object.key.indexOf('translations') > -1) return | |
// Process for each object in the incoming event | |
await Promise.all( | |
targetLanguages.map(async (targetLanguage) => { | |
await doTranslation(event, targetLanguage) | |
}) | |
) | |
return { | |
statusCode: 200, | |
body: JSON.stringify('OK'), | |
} | |
} | |
// The translation function | |
const doTranslation = async (event, targetLanguage) => { | |
return new Promise(async (resolve, reject) => { | |
// Get original text from object in incoming event | |
const originalText = await getS3object({ | |
Bucket: event.Records[0].s3.bucket.name, | |
Key: event.Records[0].s3.object.key | |
}) | |
// Translate the text | |
const data = await translateText(originalText.Body.toString('utf-8'), targetLanguage) | |
// Save the new translation | |
const baseObjectName = event.Records[0].s3.object.key.replace('.txt','') | |
await putS3object({ | |
Bucket: event.Records[0].s3.bucket.name, | |
Key: `translations/${baseObjectName}-${targetLanguage}.txt`, | |
Body: data.TranslatedText, | |
ContentType: 'text/plain' | |
}) | |
resolve() | |
}) | |
} | |
// Wrapper for AWS Translate functionality | |
const translateText = async (originalText, targetLanguageCode) => { | |
return new Promise((resolve, reject) => { | |
let params = { | |
Text: originalText, | |
SourceLanguageCode: "auto", | |
TargetLanguageCode: targetLanguageCode | |
} | |
try { | |
translate.translateText(params, (err, data) => { | |
if (err) { | |
console.log('translateText error: ', err) | |
reject(err) | |
} | |
console.log('translateText data: ', data) | |
if (data) resolve(data) | |
}) | |
} catch (err) { | |
console.error(err) | |
} | |
}) | |
} | |
// Returns object from S3 | |
const getS3object = async function (params) { | |
return new Promise((resolve, reject) => { | |
s3.getObject(params, function(err, data) { | |
if (err) { | |
console.error('getS3object error: ', err, err.stack) | |
reject(err) | |
} | |
resolve (data) | |
}) | |
}) | |
} | |
// Puts object to S3 // | |
const putS3object = async function (params) { | |
return new Promise((resolve, reject) => { | |
s3.putObject(params, function(err, data) { | |
if (err) { | |
console.error('putS3object error: ', err, err.stack) | |
reject(err) | |
} | |
resolve (data) | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment