Click here to Skip to main content
Licence CPOL
First Posted 15 Jan 2006
Views 176,059
Downloads 5,163
Bookmarked 118 times

Google Translator

By Ravi Bhavnani | 10 Mar 2010
An object that harnesses the power of Google's online natural language translation tools.
3 votes, 10.3%
1
3 votes, 10.3%
2
1 vote, 3.4%
3
4 votes, 13.8%
4
18 votes, 62.1%
5
4.25/5 - 29 votes
3 removed
μ 3.90, σa 2.60 [?]

Note:
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.

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 performs a reverse translation, which can often appear amusing when compared to the original text. 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
t.Translate();
Console.WriteLine (t.Translation);

How it works

GoogleTranslator works by posting a request to Google's 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 scraper
      this.Translation = string.Empty;
      string strContent = this.Content;
      RavSoft.StringParser parser = new RavSoft.StringParser (strContent);

      // Scrape the translation
      string strTranslation = string.Empty;
      if (parser.skipToEndOf ("")) {
              if (parser.extractTo("", ref strTranslation)) {
                  strTranslation = StringParser.removeHtml (strTranslation);
              }
          }
      }
  }

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

Revision History

  • 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.

License

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

About the Author

Ravi Bhavnani

Software Developer (Senior)

Canada Canada

Member
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, CHI 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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Question[My vote of 2] Not Working, PinmemberMendor8123:47 19 Jan '12  
AnswerRe: [My vote of 2] Not Working, PinmemberRavi Bhavnani7:12 20 Jan '12  
QuestionRe: [My vote of 2] Not Working, PinmemberRavi Bhavnani16:30 22 Jan '12  
AnswerRe: [My vote of 2] Not Working, PinmemberMendor813:12 30 Jan '12  
GeneralRe: [My vote of 2] Not Working, PinmemberRavi Bhavnani3:15 30 Jan '12  
GeneralRe: [My vote of 2] Not Working, PinmemberMendor813:18 2 Feb '12  
AnswerRe: [My vote of 2] Not Working, PinmemberRavi Bhavnani6:41 4 Feb '12  
GeneralRe: [My vote of 2] Not Working, PinmemberMendor8123:37 8 Feb '12  
GeneralRe: [My vote of 2] Not Working, PinmemberRavi Bhavnani2:22 9 Feb '12  
Questionplease check this error PinmemberMember 85217062:55 15 Jan '12  
AnswerRe: please check this error PinmemberRavi Bhavnani5:02 15 Jan '12  
Questionquestion Pinmemberrorirami11:15 11 Nov '11  
AnswerRe: question PinmemberRavi Bhavnani7:35 12 Nov '11  
GeneralMy vote of 5 PinmemberOmar Gamil0:09 9 Feb '11  
GeneralRe: My vote of 5 PinmemberRavi Bhavnani7:12 20 Jan '12  
QuestionAlways show result ??? Pinmemberevilchong6:21 4 Dec '10  
AnswerRe: Always show result ??? PinmemberBalkrishna Talele0:07 6 Jan '12  
GeneralSwap between languages PinmemberIon Florian4:52 30 Nov '10  
GeneralGoogle translator toolkit PinmemberABlokha7714:03 11 May '10  
AnswerRe: Google translator toolkit PinmemberRavi Bhavnani15:51 11 May '10  
GeneralRe: Google translator toolkit Pinmembersivakrishnabachu19:44 28 Dec '10  
GeneralRe: Google translator toolkit PinmemberRavi Bhavnani19:49 28 Dec '10  
Generallength limited! Pinmemberswanhfs5:47 6 Apr '10  
GeneralRe: length limited! PinmemberRavi Bhavnani17:09 6 Apr '10  
GeneralRe: length limited! Pinmemberswanhfs22:48 6 Apr '10  

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.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120209.1 | Last Updated 10 Mar 2010
Article Copyright 2006 by Ravi Bhavnani
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid