Your application may need to have translation feature. However building your own translation machine takes a long time and very costly, especially if you have to handle a lot of languages.
There are some translation services with multiple language supports in the Internet, one of the most populars is Google Translate. Google provides an API to use their translation service. That makes it easy for developer to integrate Google Translate on applications. Of course you've to pay to use their service. In this tutorial, I'm going to show you how to use Google Translate API in Node.js
Preparation
1. Create or select a Google Cloud project
A Google Cloud project is required to use this service. Open Google Cloud console, then create a new project or select existing project
2. Enable billing for the project
Like other cloud platforms, Google requires you to enable billing for your project. If you haven't set up billing, open billing page.
3. Enable Google Translate API
To use an API, you must enable it first. Open this page to enable Translate API.
4. Set up service account for authentication
As for authentication, you need to create a new service account. Create a new one on the service account management page and download the credentials, or you can use your already created service account.
In your .env
file, you have to add a new variable
GOOGLE_APPLICATION_CREDENTIALS=/path/to/the/credentials
The .env
file should be loaded of course, so you need to use a module for reading .env
such as dotenv
.
Dependencies
This tutorial uses @google-cloud/translate
. Add the following dependencies to your package.json
and run npm install
"@google-cloud/translate": "~1.1.0"
"dotenv": "~4.0.0"
1. Translate
To translate, just provide the text you want to translate and the target language.
require('dotenv').config();
const Translate = require('@google-cloud/translate');
const translate = new Translate({
projectId: process.env.GOOGLE_CLOUD_PROJECT_ID,
});
// Translates some text into Indonesian
const text = 'Hello, world!';
const target = 'id';
translate
.translate(text, target)
.then(results => {
console.log(results[0]);
})
.catch(err => {
console.error('ERROR:', err);
});
2. Detect Language
For detecting the language of the text, here is the example.
require('dotenv').config();
const Translate = require('@google-cloud/translate');
const translate = new Translate({
projectId: process.env.GOOGLE_CLOUD_PROJECT_ID,
});
const text = 'Apakah saya gila?';
translate
.detect(text)
.then(results => {
console.log(results[0]);
})
.catch(err => {
console.error('ERROR:', err);
});
And here is the result of the example above.
{ confidence: 0.8850894570350647,
language: 'id',
input: 'Apakah saya gila?' }
3. Get supported Languages
The list of supported lanuages may change without notice. Below is the code to get the list of current supported languges.
require('dotenv').config();
const Translate = require('@google-cloud/translate');
const translate = new Translate({
projectId: process.env.GOOGLE_CLOUD_PROJECT_ID,
});
translate
.getLanguages()
.then(results => {
console.log(results[0]);
})
.catch(err => {
console.error('ERROR:', err);
});
Below is the list of supported languages at the time this post was written.
Code | Language |
af | Afrikaans |
sq | Albanian |
am | Amharic |
ar | Arabic |
hy | Armenian |
az | Azerbaijani |
eu | Basque |
be | Belarusian |
bn | Bengali |
bs | Bosnian |
bg | Bulgarian |
ca | Catalan |
ceb | Cebuano |
ny | Chichewa |
zh | Chinese (Simplified) |
zh-TW | Chinese (Traditional) |
co | Corsican |
hr | Croatian |
cs | Czech |
da | Danish |
nl | Dutch |
en | English |
eo | Esperanto |
et | Estonian |
tl | Filipino |
fi | Finnish |
fr | French |
fy | Frisian |
gl | Galician |
ka | Georgian |
de | German |
el | Greek |
gu | Gujarati |
ht | Haitian Creole |
ha | Hausa |
haw | Hawaiian |
iw | Hebrew |
hi | Hindi |
hmn | Hmong |
hu | Hungarian |
is | Icelandic |
ig | Igbo |
id | Indonesian |
ga | Irish |
it | Italian |
ja | Japanese |
jw | Javanese |
kn | Kannada |
kk | Kazakh |
km | Khmer |
ko | Korean |
ku | Kurdish (Kurmanji) |
ky | Kyrgyz |
lo | Lao |
la | Latin |
lv | Latvian |
lt | Lithuanian |
lb | Luxembourgish |
mk | Macedonian |
mg | Malagasy |
ms | Malay |
ml | Malayalam |
mt | Maltese |
mi | Maori |
mr | Marathi |
mn | Mongolian |
my | Myanmar (Burmese) |
ne | Nepali |
no | Norwegian |
ps | Pashto |
fa | Persian |
pl | Polish |
pt | Portuguese |
pa | Punjabi |
ro | Romanian |
ru | Russian |
sm | Samoan |
gd | Scots Gaelic |
sr | Serbian |
st | Sesotho |
sn | Shona |
sd | Sindhi |
si | Sinhala |
sk | Slovak |
sl | Slovenian |
so | Somali |
es | Spanish |
su | Sundanese |
sw | Swahili |
sv | Swedish |
tg | Tajik |
ta | Tamil |
te | Telugu |
th | Thai |
tr | Turkish |
uk | Ukrainian |
ur | Urdu |
uz | Uzbek |
vi | Vietnamese |
cy | Welsh |
xh | Xhosa |
yi | Yiddish |
yo | Yoruba |
zu | Zulu |
That's all about how to use Google Translte API in Node.js. Considering the price is not cheap, it's very recommended to store the translation result, so you don't have to send the same request multiple times for translating the same text into the same language.