Affordable (free) Text Translation APIs with nodeJS
I've been digging around for some good and cheap APIs to translate thousands of words from one language to another for a tool I've been building to help me learn vocabulary in a foreign language. Turns out there isn't a definitive service that's a winner here.
After a few hours of research, I decided to go with either Google or Microsoft cloud as they have known free translation services working on the web, they both offered many supported languages, and I knew their translation quality would be reasonable from what they already provide in the web.
Google Translate API
I would have thought google translate would be the winner but their API is limited compared to what is shown on the web. It won't give you synonyms or example sentences. Just a translation for a given string.
The pricing is also at $20 per million characters.
Why Microsoft?
Microsoft has both a text translation API as well as a text to speech API. What's cool here is they not only offer a basic translation API that google offers, but additionally an examples API and a dictionary lookup API.
The pricing also turned out more reasonable and I was able to translate thousands of words for free. They have a free plan of up to 2 million characters per month, and its $10 per million after that. Standard translation here is using their standard machine learning models, which is all I needed, as compared to models you can train yourself.
Step 1: Make azure account and enable Text to
Alright, so now that I've gone through the reasonings lets get this set up and running. Surprisingly the most complicated part was getting an account set up on azure and figure out what endpoint I have to call :P
Make an account for the translator text API. It seems like Azure is catered for larger enterprises so getting set up for a small project is not intuitive, but you should be able to get to creating a resource - the endpoint and location you configure here will be the API domain you are supposed to hit. Once you have it, get your API key from the console under Keys and Endpoint.
Following the docs naively ended up giving me the wrong URL - make sure that you are using the endpoint that gets created for you when you created the service:
The APIs are as follows for translation, dictionary lookup, and examples
// Replace {your-endpoint} with your custom name you created for your resource
const translateURI =
"https://{your-endpoint}.cognitiveservices.azure.com/translator/text/v3.0/translate"
const dictionaryURI =
"https://{your-endpoint}.cognitiveservices.azure.com/translator/text/v3.0/dictionary/lookup"
const examplesURI =
"https://{your-endpoint}.cognitiveservices.azure.com/translator/text/v3.0/dictionary/examples"
Now, using the node-fetch library:
npm install node-fetch
import fetch from "node-fetch"
async fetchTranslation(type, text, to, from, original) {
const typeToURI = {
translate: translateURI,
dictionary: dictionaryURI,
examples: examplesURI,
}
const url = new URL(typeToURI[type])
const body = [{ Text: text, Translation: original }]
const res = await fetch(url, {
method: "post",
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
// Your API key from azure console
"Ocp-Apim-Subscription-Key": "{your-api-key-here}",
},
})
return await res.json()
}
Now calling this is pretty straightforward:
fetchTranslation(
'translate',
'do',
'es',
'en'
).then(result => console.log(JSON.stringify(result, null, 2))
fetchTranslation(
'dictionary',
'do',
'es',
'en'
).then(result => console.log(JSON.stringify(result, null, 2))
// For examples you need the translation already, so something like:
fetchTranslation(
'examples',
'hacer',
'es',
'en',
'do'
).then(result => console.log(JSON.stringify(result, null, 2))