Friday, October 16, 2009

Delphi: Using Google for online translating

Google provides its Translation API that looks very promising and useful. So I was very enthusiastic when we decided to use it in our Localizer project.
Unfortunately I can’t say that API is well documented. And of course there is no tutorial that describes how to use it in Delphi 2009 as we need to do.
I spent some time searching the web and found a few articles concerning this task but not a single of them give me a solution.
All found articles suggest to use the URL to access the translation service. It is not quite correct. First, this URL is used for end-user requests via browser. Its parameters are not documented and can be changed any time. Second, the response is a regular web page with a lot of unnecessary tags, text etc. It is a kind of headache to extract the result from it. And, as well as the URL, the response layout can be (I’d even say it definitely will be) changed in the future.

Google describes the Translation API and gives another way to solve the task. The correct URL is the following: In this case the response format is a JSON encoded result with embedded status codes.
All we need to do is to construct a properly constructed URL with all necessary CGI arguments, send an http referer header that accurately identifies our application (Google terms of use requirement), and be able to process the JSON encoded response.
So far so good. Let’s try to write the Delphi function that translate some input string. We will use the Indy TidHttp component to send the http requests.
As I defined after investigating the argument part of the constructed URL should be converted to UTF8 and then encoded. As Google says “the value of a CGI argument must be properly escaped (e.g., via the functional equivalent of Javascript's encodeURIComponent() method)”. I tried to use some standard or third-party URL-encoding functions but not a single of them do it correctly in terms of Google expectations. The main problem is that all available functions encode the source string char-by-char when Google expects the string encoded byte-by-byte. So I had to do it myself.
function URLEncode(const S: RawByteString): RawByteString;
    NoConversion = ['A'..'Z', 'a'..'z', '*', '@', '.', '_', '-', '/', ':', '=', '?'];
    i, idx, len: Integer;

  function DigitToHex(Digit: Integer): AnsiChar;
    case Digit of
      0..9: Result := AnsiChar(Chr(Digit + Ord('0')));
      10..15: Result := AnsiChar(Chr(Digit - 10 + Ord('A')));
      Result := '0';
  end; // DigitToHex

  len := 0;
  for i := 1 to Length(S) do
    if S[i] in NoConversion then
      len := len + 1
      len := len + 3;
  SetLength(Result, len);
  idx := 1;
  for i := 1 to Length(S) do
    if S[i] in NoConversion then
      Result[idx] := S[i];
      idx := idx + 1;
      Result[idx] := '%';
      Result[idx + 1] := DigitToHex(Ord(S[i]) div 16);
      Result[idx + 2] := DigitToHex(Ord(S[i]) mod 16);
      idx := idx + 3;
end; // URLEncode

The next question is how to extract the translation from the response we get. In our case the response format is a simple JSON object similar to the snippet shown below:
  "responseData" : {
    "translatedText" : the-translated-text,
  "responseDetails" : null | string-on-error,
  "responseStatus" : 200 | error-code

The best way is to use some library that works with JSON structures. For example, you may download and use the uJson unit.
For demonstration purpose it will be enough to process the response as regular string. We need to extract the status of the response (200 = OK), the translation text and the error string if status != 200.
// source - the string to be translated
// langpair - the string that defines the source and target language in special format, 
//     i.e. “en|ru”. The list of available languages and their abbreviations 
//     you may find in Translation API description
// resultString - the translation
// result - the error message if any. Empty result means that 
//     the function has been executed successfully
function googleTranslate(source : string; langpair : string; var resultString : string) : string;
  url, s, status : String;
  utfs : UTF8String;
  http : TidHttp;

  result := '';

  http := TidHttp.Create;

    utfs := UTF8String(source);
    utfs := URLEncode(utfs);
    url := '' + String(utfs) + '&langpair=' + langpair;

    http.Request.Referer := '';
    http.Request.UserAgent := 'Our Application';
    s := http.Get(url);

    status := Copy(s, pos('"responseStatus":', s)+18, length(s));
    status := Copy(status, 0, pos('}', status)-1);

    if (status = '200') then begin //status is OK
      s := Copy(s, pos('"translatedText":', s)+18, length(s));
      resultString := Copy(s, 0, pos('"}, "responseDetails"', s)-1);
    else begin //an error occured
      s := Copy(s, pos('"responseDetails":', s)+20, length(s));
      resultString := '';
      result := Copy(s, 0, pos('", "responseStatus"', s)-1);


At last we can try to translate something. Say, we are to translate “Hello world!” from English to Ukrainian.
  res, strValue : string;
res := googleTranslate('Hello world!', 'en|uk', strValue);
if (res = '') then //translation is OK
  ShowMessage('Translation: ' + strValue)
else //error
  ShowMessage('Error: ' + res);


  1. Thanks for sharing excellent information. keep on sharing such useful info E Cigarette Australia

  2. Great Article. Thank you for sharing! Really an awesome post for every one.

    IEEE Final Year projects Project Centers in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. Final Year Project Domains for IT It gives you tips and rules that is progressively critical to consider while choosing any final year project point.

    Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
    Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai

  3. Thank you so much for sharing such a useful information. I will definitely share this with others....Helpful for website i like the article...Indian RRB Recruitment 2020 Board has its own perks and advantages which is why it is preferred by young people. If you get a job in Indian railway recruitment, you can expect career growth and promotions...

  4. Thanks for sharing information. Choosing computer accessories from leading IT store offer great discount and value for your money Computer Store Australia | All in One Pc Australia

  5. developer tools blog!Thanks for sharing blogs with us and keep blogging. I would highly recommend website development in chennai | seo company in Chennai

  6. KBC is india's Most awaiting TV Show to give you chance to win bumper cash prizes in KBC Show, Register in KBC and Win extra cash to change your fate. Its all about KBC. find your name in KBC Winner List 2020

  7. It is really a very informative post for all those budding entreprenuers planning to take advantage of post for business expansions. You always share such a wonderful articlewhich helps us to gain knowledge .Thanks for sharing such a wonderful article, It will be deinitely helpful and fruitful article.
    Cyber Security Training Course in Chennai | Certification | Cyber Security Online Training Course | Ethical Hacking Training Course in Chennai | Certification | Ethical Hacking Online Training Course | CCNA Training Course in Chennai | Certification | CCNA Online Training Course | RPA Robotic Process Automation Training Course in Chennai | Certification | RPA Training Course Chennai | SEO Training in Chennai | Certification | SEO Online Training Course

  8. Very interesting blog. Many blogs I see these days do not really provide anything that attracts others, but believe me the way you interact is literally awesome.You can also check my articles as well.

    Security Guard License
    Ontario Security License
    Security License Ontario
    Security License

    Thank you..

  9. Amazing Article,Really useful information to all So, I hope you will share more information to be check and share here.thanks for sharing .
    website: Vietnam package tours

  10. it is really a great and helpful piece of info. I am glad that you shared this helpful information with us. Please keep us informed like this. Thank you for sharing.
    malaysia embassy singapore

  11. I enjoyed over read your blog post. This was actually what i was looking for and i am glad to came here!
    Website: Imitation jewellery manufacturers

  12. Looking for best English to Tamil typing online, make use of our site to enjoy Tamil typing and directly share on your social media handle. Tamil typing Software Download | tamil typing keyboard practice

  13. I have translated many times with google, but I didn't this way. How can you do it? I would see youtube video. If you want to get youtube views for your video just navigate here

  14. Post is very good its amzazing post I love them thanks for sharing.
    visit here- police thug life video

  15. an interesting article, it was pleasant to read, well written, I myself sometimes write articles, and it helps me

  16. Very interesting blog. Many blogs I see these days do not really provide anything that attracts others, but believe me the way you interact is literally awesome.You can also check my website as well.

    Freelancers Marketplace

    Thank you..

  17. Students have many types of problems related to college assignments. So, we have a well-educated expert for writing assignments. If you have any problem with writing assignments. then Don’t worry because we provide the best online assignment help and free plagiarism assignment.
    Online Assignment Help

  18. Useful article which was very helpful. also interesting and contains good information.
    to know about python training course , use the below link.

    Python Training in chennai

    Python Course in chennai

  19. Good write-up, I am regular visitor of one's site, maintain up the nice operate, and It's going to be a regular visitor for a lengthy time.
    Visit here :- Top Seo Company In Gwalior

  20. it is really a great and helpful piece of info. I am glad that you shared this helpful information with us. Please keep us informed like this. Thank you for sharing.
    Website: Online punjab state lottery

  21. Play Fantasy Cricket Leagues & Sports Game Play Fantasy Cricket Game Online & Win Real Cash prize Play Daily Fantasy Cricket Leagues & Kabaddi Games India. fantasy cricket app

  22. Thank you for your informative article, I have been doing research on this subject, and for three days I keep entering sites that are supposed to have what I am searching for, only to be discouraged with the lack of what I needed. Thank you again.

    Java Training in Chennai

    Java Course in Chennai

  23. We are appreciate with your valuable information. Visit OGEN Infosystem for professional Website Designing and PPC Services at affordable price.
    Website Designing Company in Delhi

  24. I am new here. I like your post very much. It is very usefull post for me.
    website: 5G Infrastructure Market in US

  25. Thanks for another great post. Where else could anyone get that kind of information in such a perfect way of writing? I've a presentation next week, and I'm on the look for such information.
    buy electronics online

  26. Present in the advanced market for in any event 20 years, we have improved the destinations of in excess of 400 customers. Do you need a unique, reasonable and significant Site creation and referencing ? You are in the perfect spot! With ID Design ( Marketing agency ) we acknowledge your activities. We cautiously dissect your objectives and solicitations. We start work after a few trades.

    We will probably assemble an imaginative and productive site to expand your turnover . We subsequently focus on straightforwardness and furnish you with all our advanced promoting abilities. Come see us or call us, you won’t be frustrated!

  27. Thanks for sharing the information... You write very interesting and well... Get Chandigarh escort service

    Reach on

    Chandigarh escort
    Chandigarh call girls

  28. Precision Outdoors provides lightning fast shipping on the most trusted brands in gun parts & accessories like Ar 15 accessories, Ar stock, Law tactical folders, red dot sights, rifle scopes, magazines, frames, barrels, triggers, stocks, and much more...

    So don't wait anymore Visit us now!

  29. one stop renovations
    Thanks for sharing the information... You write very interesting and well... Get Chandigarh escort service

  30. I always like to read a quality content having accurate information regarding the subject and the same thing I found in this post.

    Website Development Company |

  31. Great information, i was searching of this kind of information, thank you very much for sharing with us.

    Craigslist Posting Service for Car Dealers |

  32. This is really amazing website that I have been found on google regarding website Blog Commenting sites. and I would like to thank admin who also given us to post the link on his side.

    Lubbock moving company |

  33. This was something I was looking for, really helpful, and great work is done. Thank you so much for sharing such valuable information.

    Car Auction Software |

  34. It’s really a cool and helpful piece of information. I am glad that you shared this useful information with us. Please keep us up to date like this. Thanks for sharing.

    Best CRM for Small Businesses |

  35. I am really like it very much for the interesting info in this blog that to this website is providing the wonderful info in this blog that to utilize the great technology in this blog.

    Web Development Company in Gwalior |

  36. Avalanche provides a host of online services to businesses & influencers in Ireland & around the world including web design in Kerry, Cork, Limerick and pretty much all over Ireland, eCommerce solutions, SEO, graphic design, social media marketing, video production & more. We capture our client's unique personalities in their website as it is so important to stand out in today's saturated online world.

    Avalanche is much more than a service provider, we provide high end technical support to all our clients & become their digital partners to ensure that they are successful online. We would love to hear from you and help in any way we can.

  37. Recently I have read from this post that everyone can get many likes for any video. I think you can test it now. Make video about delphi google online translating and get many tiktok likes for it

  38. Appslure is an award-winning mobile app development company building feature-packed and interactive mobile applications for startups, medium and large enterprises.

  39. Thank you for sharing such nice content so keep posting.
    microsoft solitaire online

  40. Very nice article. I enjoyed reading your post. very nice share. I want to twit this to my followers. Thanks
    รับทำ seo
    wm casino
    poker online

  41. Such a very useful blog. Very Interesting to read this blog. I would like to thank you for the efforts you had made for writing this awesome blog. Great work.
    kardinal stick

  42. Watch movies online, watch new movies, series Netflix HD 4K, ดูหนังออนไลน์ watch free movies on your mobile phone, Tablet, watch movies on the web.

    SEE4K Watch movies, watch movies, free series, load without interruption, sharp images in HD FullHD 4k, all matters, ดูหนังใหม่ all tastes, see anywhere, anytime, on mobile phones, tablets, computers.

    GangManga read manga, read manga, read manga online for free, fast loading, clear images in HD quality, all titles, อ่านการ์ตูน anywhere, anytime, on mobile, tablet, computer.

    Watch live football live24th, watch football online, ผลบอลสด a link to watch live football, watch football for free.

  43. SamudraBet Situs Judi Online Terbaik, Terpercaya dan Terbesar di Indonesia. Menyediakan permainan :

    Judi bola terbaik

    Judi slot online terpercaya

    Poker online terbaru

    Live casino online

    Judi sabung ayam

  44. Thank you so much for sharing all this wonderful information !!!! It is so appreciated!! You have good humor in your blogs. So much helpful and easy to read!
    Java course in Nagpur
    Java course in Latur
    Java course in Aurangabad
    Java course in Nashik
    Java course in Dhule
    Java course in Sangli

    Very Informative and useful... Keep it up the great work. I really appreciate your post.

  46. CBD isolates. CBD (cannabidiol) is an active ingredient found in th CBD Cannabis plant that does not cause psychoactive "high". Instead, the active ingredient provides soothing energy, such as a stronger Ginkgo Biloba tea