Created
March 21, 2020 16:28
-
-
Save jordanrios94/55afea97ce5d74299ad4a9bd726984a7 to your computer and use it in GitHub Desktop.
A simple script that uses Google Translate API to translate content form JS object into different languages.
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 fs = require('fs'); | |
const keys = require('../config/keys'); | |
const { Translate } = require('@google-cloud/translate').v2; | |
const translate = new Translate({ | |
projectId: keys.googleProjectKey, | |
keyFilename: '../google_authentication.json' | |
}); | |
const appProperties = { | |
imageUploadError: 'Error with uploading image', | |
imageReadError: 'Could not read image', | |
cameraPermissionsError: | |
'Sorry, we need camera roll permissions to make this work!' | |
}; | |
const languages = { | |
es: { | |
name: 'Spanish', | |
nativeName: 'español, castellano' | |
}, | |
it: { | |
name: 'Italian', | |
nativeName: 'Italiano' | |
}, | |
pt: { | |
name: 'Portuguese', | |
nativeName: 'Português' | |
}, | |
fr: { | |
name: 'French', | |
nativeName: 'français' | |
}, | |
de: { | |
name: 'German', | |
nativeName: 'Deutsch' | |
}, | |
el: { | |
name: 'Greek', | |
nativeName: 'Ελληνικά' | |
}, | |
nl: { | |
name: 'Dutch', | |
nativeName: 'Nederlands, Vlaams' | |
}, | |
fi: { | |
name: 'Finnish', | |
nativeName: 'suomi, suomen kieli' | |
}, | |
sk: { | |
name: 'Slovak', | |
nativeName: 'slovenčina' | |
}, | |
sl: { | |
name: 'Slovene', | |
nativeName: 'slovenščina' | |
}, | |
hu: { | |
name: 'Hungarian', | |
nativeName: 'Magyar' | |
}, | |
uk: { | |
name: 'Ukrainian', | |
nativeName: 'українська' | |
}, | |
ru: { | |
name: 'Russian', | |
nativeName: 'русский язык' | |
} | |
}; | |
const translateText = async (text, target) => { | |
try { | |
const [translations] = await translate.translate(text.trim(), target); | |
const translation = Array.isArray(translations) | |
? translations[0] | |
: translations; | |
return translation; | |
} catch (error) { | |
console.log(error); | |
return ''; | |
} | |
}; | |
const translateLanguages = async () => { | |
for (let languageCode in languages) { | |
const languageElement = languages[languageCode]; | |
console.log('TRANSLATING:', languageElement.name.toUpperCase()); | |
const translation = {}; | |
for (let appProperty in appProperties) { | |
const translatedText = await translateText( | |
appProperties[appProperty], | |
languageCode | |
); | |
console.log('TRANSLATING KEY', appProperty); | |
console.log('FROM', appProperties[appProperty]); | |
console.log('TO', translatedText); | |
translation[appProperty] = translatedText; | |
} | |
const fileName = './' + languageElement.name.toUpperCase() + '.json'; | |
fs.writeFile(fileName, JSON.stringify(translation, null, 2), error => { | |
if (error) { | |
throw error; | |
} | |
console.log(fileName + ' is created successfully.'); | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment