MATLAB Answers

Using google translate API in Matlab

39 views (last 30 days)
Amit Madahar
Amit Madahar on 27 Jun 2019
Commented: Amit Madahar on 3 Jul 2019
I am trying to use google translate API in matlab but struggling. I have implemented alternative method detailed below for the time being which works well but buggy with extra-long string, may be due to urlencoding, I am not sure. So want to implemented translation using Google Java APIs. I have done the following steps and hope experts here would be able to help me.
Google Translate API Set-up
1) Followed the google could translation API quick start guide at
2) Set GCP project > set up environment variables on local machine > installed Google Cloud SDK > enabled Cloud Translation API > set up API credential key.
3) Downloaded google-could-translate API jar to provide Matlab /Java bridge to installed SDK and added it to dynamic path using javaaddpath.
Dry run in Java (IntelliJ)
4) Tried the following in java first to see if the above set-up works. It works!
public class gtAPI {
public static void main(String... args) throws Exception {
// Instantiates a client
Translate translate = TranslateOptions.getDefaultInstance().getService();
// The text to translate
String text = "Text to Translate";
// Translates some text into Russian
Translation translation =
System.out.printf("Text: %s%n", text);
System.out.printf("Translation: %s%n", translation.getTranslatedText());
Implementing above in Matlab Now I successfully imported the following:
now when I try to call
I get the following error message.
Undefined variable "Translate" or class "Translate.TranslateOptions.getDefaultInstance"
which I am boggled by as the class does exist.
The idea is that I should be able use these directly into matlab just like other jar libraries, but somehow I am missing something. In Alternate Method below, I use Jsoup libraries to parse and query htmlDOM and all works great. So it is possible to do it directly in Matlab but I am missing something.
Alternatively I can write a java class that takes care of the translation and returns the translated string. Basically adapting the above java code to return me the translation but I wanted to do all that from within Matlab. Any ideas how?
Alternate Method - Currently Implemented
Currently working with the following methods, which works great but when it come to extra long strings, it struggles. I then have to chop the strings to be translated into manageable sizes for translation to proceed. That means I have to call this function many times adding to run time.
function output = translationAPI(foreignText,fromLang,toLang)
%% Uses google API to make the translations
% Input : foreignText - Text to be translated
% fromLang - Language of the text to be trnaslated, use google
% language tags. See GoogleTranslateAPI doccumentation.
% toLang - Language to be translated to
import org.jsoup.*
foreignText = char(,'UTF-8'));
qryStr = ['',...
conn = Jsoup.connect(qryStr);
conn = conn.timeout(20*1000);
conn = conn.referrer("");
conn = conn.followRedirects(true);
conn = conn.userAgent("Chrome");
conn = conn.ignoreContentType(true); % Ignore conent type should be set to
% true when extracting rss xml to
% avoid errors
objectDOMStr = string(conn.get().toString);
objectDOMStr = strrep(objectDOMStr,'"','');
output = strtrim(string(regexp(objectDOMStr,'translatedText:(.*?)}','tokens')));


Guillaume on 27 Jun 2019
Can you provide,
  • the portion of your code that works so far.
  • the bit that causes the error
  • the full text of the error message
Amit Madahar
Amit Madahar on 3 Jul 2019
Hi Guillaume - Sorry it was a very badly formed question the first time. I hope this time it explains the problem I am having.

Sign in to comment.

Answers (0)

Sign in to answer this question.