Click here to Skip to main content
Click here to Skip to main content

Google Translator

, 22 Mar 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
An object that harnesses the power of Google's online natural language translation tools.

GoogleTranslator was originally written when screen scraping was the only way to access Google's online translation tools. As has been rightly pointed out in this article's forum, a more modern approach would be to use Google's AJAX APIs. See this link for more information. This screen scraper version of GoogleTranslator continues to be maintained at the request of CP readers.

This latest version of GoogleTranslator utilizes Google Translate's AJAX APIs to translate text and retrieves the translation by parsing the returned JSON content. Thanks to CPians @Member 9899010 and @bgsjust for pointing me to these APIs. The latest version of the code also includes the ability to speak the translation from the demo app. Because Google limits the speech to common words in a few languages, don't be surprised if the demo plays dumb when you try to speak your translated text!

What is it?

GoogleTranslator in action GoogleTranslator is an object that allows you to translate text using the power of Google's online language tools. GoogleTranslator is an example of a WebResourceProvider and makes use of the StringParser utility class, both of which are published elsewhere at CodeProject.

The demo app also allows you to easily perform a reverse translation. The app can be used as a poor man's resource translator for simple phrases, but you'd be wise to confirm the translation with a native speaker before using the results.

How do I use it?

You use GoogleTranslator by initializing it and calling its Translate() method.

// Initialize the translator
using RavSoft.GoogleTranslator;
Translator t = new GoogleTranslator();
t.SourceLanguage = "English";
t.TargetLanguage = "French";
t.SourceText = "Hello, how are you?";

// Translate the text
Console.WriteLine (t.Translation);
Console.WriteLine ("Translated in " + t.TranslationTime.TotalMilliseconds + " mSec");
Console.WriteLine ("Translated speech = " + t.TranslationSpeakUrl);

How it works

GoogleTranslator works by directly invoking Google's translation API called by its online translation form and parsing the results. All the "heavy lifting" is done by the WebResourceProvider class, described elsewhere at CodeProject. WebResourceProvider fetches the content from Google and offers GoogleTranslator an opportunity to parse it.

  /// <summary>
  /// Parses the fetched content.
  /// </summary>
  protected override void parseContent()
    // Initialize the parser
    this.Translation = string.Empty;
    string strContent = this.Content;
    RavSoft.StringParser parser = new RavSoft.StringParser (strContent);

    // Extract the translation
    string strTranslation = string.Empty;
    if (parser.skipToEndOf ("[[[\"")) {
        bool morePhrasesRemaining = true;
        do {
            string translatedPhrase = null;
            if (parser.extractTo ("\",\"", ref translatedPhrase)) {
                strTranslation += translatedPhrase;
            morePhrasesRemaining = parser.skipToEndOf (",\"\",\"\"],[\"");
        while (morePhrasesRemaining);
    this.Translation = strTranslation.Replace (" .", ".")
                                     .Replace (" ?", "?")
                                     .Replace (" ,", ",")
                                     .Replace (" ;", ";")
                                     .Replace (" !", "!");

    // Set the translation speak url
    this.TranslationSpeakUrl =
        string.Format ("{0}&q={1}",
                       Translator.LanguageEnumToIdentifier (this.TargetLanguage),
                       HttpUtility.UrlEncode (this.Translation));

As you can see, the logic used to parse the JSON result is very simple! GoogleTranslator uses the the StringParser class to extract the translated text.

Speaking the translation

The Translator object retrieves the URL that will stream the spoken version of the translation. The demo app speaks this content by navigating to this URL in a hidden browser control. As mentioned in the preamble, because Google limits the speech to common words in a few languages, don't be surprised if the demo plays dumb when you try to speak your translated text!

Revision History

  • 9 Mar 2014
    Switched to using Google's JSON translation APIs.
    Added TranslationTime and TranslationSpeakUrl properties.
    Tweaked demo app UI to assist in reverse translation and resetting an English source and target.
  • 13 Jan 2013
    Added support for current full language set.
    Refixed bug that limited translation to first sentence.
    Fixed a bug that caused reverse translation to fail when accented characters were present.
  • 10 Mar 2010
    Added support for current full language set.
    Fixed bug that limited translation to first sentence.
  • 15 Feb 2010
    Even more parsing tweakage.
  • 28 Mar 2009
    More parsing tweakage.
  • 20 Mar 2007
    Tweaked parsing logic to conform to changes at Google's website.
  • 15 Jan 2006
    Initial version.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ravi Bhavnani
Technical Lead
Canada Canada
Ravi Bhavnani is an ardent fan of Microsoft technologies who loves building Windows apps, especially PIMs, system utilities, and things that go bump on the Internet. During his career, Ravi has developed expert systems, desktop imaging apps, marketing automation software, EDA tools, a platform to help people find, analyze and understand information, trading software for institutional investors and advanced data visualization solutions. He currently works for a company that provides enterprise workforce management solutions to large clients.
His interests include the .NET framework, reasoning systems, financial analysis and algorithmic trading, NLP, HCI and UI design. Ravi holds a BS in Physics and Math and an MS in Computer Science and was a Microsoft MVP (C++ and C# in 2006 and 2007). He is also the co-inventor of 2 patents on software security and generating data visualization dashboards. His claim to fame is that he crafted CodeProject's "joke" forum post icon.
Ravi's biggest fear is that one day he might actually get a life, although the chances of that happening seem extremely remote.
Follow on   Google+   LinkedIn

Comments and Discussions

GeneralRe: end of paragraphs or carriage return Pinmemberbgsjust27-May-13 12:46 
GeneralRe: end of paragraphs or carriage return PinprofessionalRavi Bhavnani27-May-13 12:56 
QuestionProxy Pinmemberanga15-Jan-13 23:11 
AnswerRe: Proxy PinmemberRavi Bhavnani16-Jan-13 3:03 
GeneralRe: Proxy Pinmemberanga16-Jan-13 3:05 
GeneralRe: Proxy Pinmemberanga6-Feb-13 22:34 
AnswerRe: Proxy PinmemberRavi Bhavnani7-Feb-13 8:36 
GeneralRe: Proxy Pinmemberanga7-Feb-13 20:09 
GeneralRe: Proxy Pinmemberanga12-May-13 22:39 
GeneralRe: Proxy PinprofessionalRavi Bhavnani13-May-13 3:22 
AnswerRe: Proxy [modified] Pinprofessionalbgsjust28-Aug-13 8:44 
GeneralRe: Proxy PinprofessionalRavi Bhavnani28-Aug-13 8:45 
GeneralRe: Proxy Pinprofessionalbgsjust28-Aug-13 9:02 
Questionenglish into urdu PinmemberMember 930213510-Jan-13 9:44 
how can I convert english text into urdu text using this application???
AnswerRe: english into urdu PinmemberRavi Bhavnani10-Jan-13 9:53 
GeneralRe: english into urdu PinmemberMember 930213513-Jan-13 8:18 
GeneralRe: english into urdu PinmemberRavi Bhavnani13-Jan-13 9:40 
AnswerRe: english into urdu Pinprofessionalbgsjust29-Aug-13 14:58 
GeneralRe: english into urdu PinprofessionalRavi Bhavnani30-Aug-13 3:46 
QuestionStringParser not found PinmemberBhavika.R27-Dec-12 1:25 
AnswerRe: StringParser not found PinmemberRavi Bhavnani27-Dec-12 6:38 
QuestionGreat job!! Pinmemberdeddy8316-Dec-12 21:24 
AnswerRe: Great job!! PinmemberRavi Bhavnani17-Dec-12 2:41 
AnswerRe: Great job!! PinmemberRavi Bhavnani7-Feb-13 8:38 
QuestionJapanese~ Pinmemberkuronekoneko9-Dec-12 17:28 
AnswerRe: Japanese~ PinmemberRavi Bhavnani10-Dec-12 8:56 
GeneralRe: Japanese~ Pinmemberkuronekoneko11-Dec-12 8:31 
GeneralRe: Japanese~ PinmemberRavi Bhavnani11-Dec-12 8:43 
QuestionNice Pinmemberario rakip29-Nov-12 3:27 
AnswerRe: Nice PinmemberRavi Bhavnani29-Nov-12 3:30 
QuestionRequest in VB.Net Pinmemberirvieboy19-Nov-12 22:17 
AnswerRe: Request in VB.Net PinmemberRavi Bhavnani20-Nov-12 3:15 
QuestionGreat work - Here is a little bug fix ... PinmemberJohn Whiteman21-Aug-12 3:06 
AnswerRe: Great work - Here is a little bug fix ... PinmemberRavi Bhavnani21-Aug-12 5:18 
QuestionAuto-detect PinmemberKendoTM31-Jul-12 1:21 
AnswerRe: Auto-detect PinmemberRavi Bhavnani31-Jul-12 3:17 
BugThe bug that limits the translation of the first sentence again? PinmemberMember 462172825-Jul-12 3:18 
GeneralRe: The bug that limits the translation of the first sentence again? PinmemberMember 462172825-Jul-12 3:30 
GeneralRe: The bug that limits the translation of the first sentence again? PinmemberRavi Bhavnani25-Jul-12 4:26 
AnswerRe: The bug that limits the translation of the first sentence again? PinmemberRavi Bhavnani13-Jan-13 9:42 
QuestionChinese to English - 2012年06月13日 Pinmemberdevvvy14-Jun-12 0:45 
AnswerRe: Chinese to English - 2012年06月13日 PinmemberRavi Bhavnani14-Jun-12 2:38 
QuestionReally great work! PinmemberVali Radu29-May-12 5:56 
AnswerRe: Really great work! PinmemberRavi Bhavnani29-May-12 5:58 
QuestionGoogle Translator PinmemberMiguel.Thewolf27-May-12 2:26 
AnswerRe: Google Translator PinmemberRavi Bhavnani27-May-12 8:27 
QuestionPortuguese to english translation problems Pinmemberbgsjust18-Apr-12 3:24 
AnswerRe: Portuguese to english translation problems PinmemberRavi Bhavnani18-Apr-12 7:05 
GeneralRe: Portuguese to english translation problems Pinmemberbgsjust18-Apr-12 10:08 
GeneralRe: Portuguese to english translation problems PinmemberRavi Bhavnani18-Apr-12 10:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150327.1 | Last Updated 22 Mar 2014
Article Copyright 2006 by Ravi Bhavnani
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid