Free Automatic Translation Within Netsuite using Yandex Translate API
In this challenge I decided to integrate Netsuite with Yandex Translate REST API for the following reasons:
- Free version includes:
- Up to 10 million characters a month (or 322,580 characters per day spread evenly in 31 days)
- Up to 1 million characters in 24 hours (would last 10 days only as max is 10 million per month)
- Translation quality is decent.
- Great for small translation projects.
Let’s say we want to translate a FreeText field, that has a max of 300 characters so that would allow me for a max workload of around 1,075 translations per day, and that’s maxing out all 300 characters per request. Not bad.
Getting started is really easy:
- Go to https://tech.yandex.com/translate/
- Get a free API Key (You might need to register)
- Call the Yandex API within Netuiste (Follow the code example below)
I tested this on an after submit script that translates the memo field and saves the result in another field called “Memo in Spanish”
Don’t forget to check Yandex Translate API documentation to learn more about the languages and more features such as auto detecting languages.
Here’s the code example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\ || This user event script translates Memo text into || || "Memo in Spanish" field after submit || || || || || || Version Date Author Remarks || || 1.0 Dec 01 2016 Adolfo Garza Initial commit || \= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */ var APIKEY = 'trnsl.1.1.20161201T045'; //<< Replace with your key var URL = 'https://translate.yandex.net/api/v1.5/tr.json/translate?key=%API-KEY%'.replace('%API-KEY%', APIKEY); function afterSubmit() { var memo = nlapiGetFieldValue('memo'); var memoInSpanish = translateText(memo, 'en-es'); //<< You can set language here nlapiSubmitField(nlapiGetRecordType(), nlapiGetRecordId(), 'custbody_memo_in_spanish', memoInSpanish); } function translateText(text, lang){ var reqBody = {text: text, lang: lang}; var reqUrl = URL; var myResponse = nlapiRequestURL(reqUrl, reqBody); var responseBody = JSON.parse(myResponse.getBody()); var responseTextArray = responseBody.text || []; var responseText = responseTextArray.join(' '); return responseText; } |