Click here to Skip to main content
6,597,576 members and growing! (21,339 online)
Email Password   helpLost your password?
General Programming » Internet / Network » Utilities     Intermediate License: The Code Project Open License (CPOL)

Google Translator

By Ravi Bhavnani

An object that harnesses the power of Google's online natural language translation tools.
C#, Windows, .NET 1.1, .NET 2.0, .NET 3.0, .NET 3.5VS2005, Dev
Posted:15 Jan 2006
Updated:28 Mar 2008
Views:82,014
Bookmarked:76 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
18 votes for this article.
Popularity: 5.15 Rating: 4.10 out of 5
1 vote, 5.6%
1
2 votes, 11.1%
2
1 vote, 5.6%
3
3 votes, 16.7%
4
11 votes, 61.1%
5

What is it?

GoogleTranslator in action GoogleTranslator is an object that allows you to translate English text to French, German, Italian, Spanish or Portugese and back by harnessing 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 its translating mode and calling its translate() method.

// Translate from English to French
GoogleTranslator gt =
  new GoogleTranslator (GoogleTranslator.Mode.EnglishToFrench);
string strEnglish = "Hello, how are you?";
string strFrench = gt.translate (strEnglish);

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>
  /// Provides the derived class to parse the fetched content.
  /// </summary>
  protected override void parseContent()
  {
    m_strTranslation = "";
    string strContent = Content;
    RavSoft.StringParser  parser = new RavSoft.StringParser (strContent);
    if (parser.skipToStartOfNoCase ("<textarea name=q ")) {
        if (parser.skipToEndOf (">")) {
            bool bStatus = parser.extractToNoCase ("</textarea>", 
                                                   ref m_strTranslation);
        }
    }
  }

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

Revision History

  • 28 Mar 2008
    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


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, enterprise marketing automation software, EDA tools, a platform to help people find, analyze and understand information, and trading software for institutional investors. He currently works for a company that provides advanced data visualization solutions for Microsoft technologies.

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 is a Microsoft C# MVP. 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.
Occupation: Software Developer (Senior)
Company: Dundas Software
Location: Canada Canada

Other popular Internet / Network articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 48 (Total in Forum: 48) (Refresh)FirstPrevNext
Generalwhich version of vb should I open the code in? PinmemberDibyendu Biswas10:50 5 Sep '09  
GeneralRe: which version of vb should I open the code in? PinmemberRavi Bhavnani10:52 5 Sep '09  
GeneralRe: which version of vb should I open the code in? PinmemberDibyendu Biswas11:00 5 Sep '09  
GeneralRe: which version of vb should I open the code in? PinmemberDibyendu Biswas11:05 5 Sep '09  
GeneralRe: which version of vb should I open the code in? PinmemberRavi Bhavnani11:29 5 Sep '09  
GeneralRe: which version of vb should I open the code in? PinmemberRavi Bhavnani11:28 5 Sep '09  
GeneralJapanese Support PinmemberAlien8119:01 15 Jun '09  
GeneralUnicode support (added code) Pinmemberron47713:15 31 Mar '09  
GeneralRe: Unicode support (added code) PinmemberRavi Bhavnani13:17 31 Mar '09  
QuestionJapanese support PinmemberDavidA20052:15 6 Mar '09  
AnswerRe: Japanese support PinmemberDavidA20052:41 6 Mar '09  
GeneralRe: Japanese support PinmemberRavi Bhavnani4:29 6 Mar '09  
QuestionIs there a VB Version Pinmemberelchalateco3:27 8 Jul '08  
AnswerRe: Is there a VB Version PinmemberRavi Bhavnani4:54 8 Jul '08  
GeneralRe: Is there a VB Version Pinmemberelchalateco5:21 8 Jul '08  
GeneralNoticed something strange Pinmemberjwaldner7:07 4 Apr '08  
GeneralRe: Noticed something strange PinmemberRavi Bhavnani7:31 4 Apr '08  
GeneralRe: Noticed something strange Pinmemberjwaldner8:44 7 Apr '08  
GeneralDoesn't Work Anymore PinmemberScott Bruhnsen7:59 27 Mar '08  
GeneralRe: Doesn't Work Anymore PinmemberRavi Bhavnani11:45 27 Mar '08  
GeneralRe: Doesn't Work Anymore PinmemberRavi Bhavnani12:53 28 Mar '08  
GeneralUnicode complaince required - then we can support 6 more languages PinmemberYogesh P. Dhakad21:36 20 May '07  
GeneralRe: Unicode complaince required - then we can support 6 more languages PinmemberRavi Bhavnani6:11 21 May '07  
GeneralRe: Unicode complaince required - then we can support 6 more languages PinmemberYogesh P. Dhakad21:10 18 Jun '07  
GeneralRe: Unicode complaince required - then we can support 6 more languages PinmemberRavi Bhavnani3:35 19 Jun '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 Mar 2008
Editor: Ravi Bhavnani
Copyright 2006 by Ravi Bhavnani
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project