5,698,535 members and growing! (17,765 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.5, .NETVisual Studio, VS2005, Dev

Posted: 15 Jan 2006
Updated: 28 Mar 2008
Views: 61,510
Bookmarked: 64 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
16 votes for this Article.
Popularity: 4.89 Rating: 4.06 out of 5
1 vote, 6.3%
1
2 votes, 12.5%
2
1 vote, 6.3%
3
3 votes, 18.8%
4
9 votes, 56.3%
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


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, intelligent agents, 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
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 36 (Total in Forum: 36) (Refresh)FirstPrevNext
QuestionIs there a VB Versionmemberelchalateco3:27 8 Jul '08  
AnswerRe: Is there a VB VersionmemberRavi Bhavnani4:54 8 Jul '08  
GeneralRe: Is there a VB Versionmemberelchalateco5:21 8 Jul '08  
GeneralNoticed something strangememberjwaldner7:07 4 Apr '08  
GeneralRe: Noticed something strangememberRavi Bhavnani7:31 4 Apr '08  
GeneralRe: Noticed something strangememberjwaldner8:44 7 Apr '08  
GeneralDoesn't Work AnymorememberScott Bruhnsen7:59 27 Mar '08  
GeneralRe: Doesn't Work AnymorememberRavi Bhavnani11:45 27 Mar '08  
GeneralRe: Doesn't Work AnymorememberRavi Bhavnani12:53 28 Mar '08  
GeneralUnicode complaince required - then we can support 6 more languagesmemberYogesh P. Dhakad21:36 20 May '07  
GeneralRe: Unicode complaince required - then we can support 6 more languagesmemberRavi Bhavnani6:11 21 May '07  
GeneralRe: Unicode complaince required - then we can support 6 more languagesmemberYogesh P. Dhakad21:10 18 Jun '07  
GeneralRe: Unicode complaince required - then we can support 6 more languagesmemberRavi Bhavnani3:35 19 Jun '07  
GeneralProblems using unicodememberdiesel16004:45 7 May '07  
GeneralRe: Problems using unicodememberRavi Bhavnani6:44 7 May '07  
Jokeone jokememberSalman07860:09 23 Mar '07  
GeneralAllow me to abuse pleasememberbrahmma21:36 4 Apr '07  
QuestionDoesnt workmemberUnRusoDeCaracas20:40 20 Mar '07  
AnswerRe: Doesnt workmemberRavi Bhavnani5:30 21 Mar '07  
GeneralRe: Doesnt workmembertaketwo22:49 21 Mar '07  
GeneralRe: Doesnt workmemberRavi Bhavnani5:05 22 Mar '07  
GeneralProblemmemberhitesh_sharma8:59 20 Mar '07  
AnswerRe: ProblemmemberRavi Bhavnani10:00 20 Mar '07  
GeneralRe: Problemmemberhitesh_sharma10:18 20 Mar '07  
GeneralTranslation Cutting Off Last Lettermemberpgorman6:46 16 Oct '06  

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-2008
Web11 | Advertise on the Code Project