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 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.
using RavSoft.GoogleTranslator;
Translator t = new GoogleTranslator();
t.SourceLanguage = "English";
t.TargetLanguage = "French";
t.SourceText = "Hello, how are you?";
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.
protected override void parseContent()
{
this.Translation = string.Empty;
string strContent = this.Content;
RavSoft.StringParser parser = new RavSoft.StringParser (strContent);
string strTranslation = string.Empty;
if (parser.skipToEndOf ("<span id=result_box")) {
while (parser.skipToEndOf (" önmouseout=\"this.style.backgroundColor='#fff'\">")) {
string translatedSegment = string.Empty;
if (parser.extractTo("</span>", ref translatedSegment)) {
strTranslation += " " + StringParser.removeHtml (translatedSegment);
}
}
}
}
As you can see, the parsing logic is very simple! GoogleTranslator uses the the StringParser class to extract the translated text.
Revision History
- 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.
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.