Note:
This latest version of
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 is an object that
allows you to translate text using the power of Google's online language
tools. 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 useGoogleTranslator
by constructing it and calling its Translate()
method.
Hide Copy Code
using RavSoft.GoogleTranslator;
Translator t = new GoogleTranslator();
string translation = t.Translate ("Hello, how are you?", "English", "French");
Console.WriteLine (translation);
Console.WriteLine ("Translated in " + t.TranslationTime.TotalMilliseconds + " mSec");
Console.WriteLine ("Translated speech = " + t.TranslationSpeechUrl);
How it works
GoogleTranslator works by directly invoking Google's translation API called by its online translation form and parsing the results.
Hide Shrink Copy Code
// Initialize
this.Error = null;
this.TranslationSpeechUrl = null;
this.TranslationTime = TimeSpan.Zero;
DateTime tmStart = DateTime.Now;
string translation = string.Empty;
try {
// Download translation
string url = string.Format ("https://translate.google.com/translate_a/single?client=t&sl={0}" +
"&tl={1}&hl=en&dt=bd&dt=ex&dt=ld&dt=md&dt=qc&dt=rw&dt=rm&dt=ss&dt=t" +
"&dt=at&ie=UTF-8&oe=UTF-8&source=btn&ssel=0&tsel=0&kc=0&q={2}",
Translator.LanguageEnumToIdentifier (sourceLanguage),
Translator.LanguageEnumToIdentifier (targetLanguage),
HttpUtility.UrlEncode (sourceText));
string outputFile = Path.GetTempFileName();
using (WebClient wc = new WebClient ()) {
wc.Headers.Add ("user-agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 " +
"(KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
wc.DownloadFile(url, outputFile);
}
// Get translated text
if (File.Exists (outputFile)) {
// Get phrase collection
string text = File.ReadAllText(outputFile);
int index = text.IndexOf (string.Format(",,\"{0}\"", Translator.LanguageEnumToIdentifier (sourceLanguage)));
if (index == -1) {
// Translation of single word
int startQuote = text.IndexOf('\"');
if (startQuote != -1) {
int endQuote = text.IndexOf('\"', startQuote + 1);
if (endQuote != -1) {
translation = text.Substring(startQuote + 1, endQuote - startQuote - 1);
}
}
}
else {
// Translation of phrase
text = text.Substring(0, index);
text = text.Replace("],[", ",");
text = text.Replace("]", string.Empty);
text = text.Replace("[", string.Empty);
text = text.Replace("\",\"", "\"");
}
// Get translated phrases
string[] phrases = text.Split (new[] { '\"' }, StringSplitOptions.RemoveEmptyEntries);
for (int i=0; (i < phrases.Count()); i += 2) {
string translatedPhrase = phrases[i];
if (translatedPhrase.StartsWith(",,")) {
i--;
continue;
}
translation += translatedPhrase + " ";
}
// Fix up translation
translation = translation.Trim();
translation = translation.Replace(" ?", "?");
translation = translation.Replace(" !", "!");
translation = translation.Replace(" ,", ",");
translation = translation.Replace(" .", ".");
translation = translation.Replace(" ;", ";");
// And translation speech URL
this.TranslationSpeechUrl = string.Format ("https://translate.google.com/translate_tts?ie=UTF-8"+
"&q={0}&tl={1}&total=1&idx=0&textlen={2}&client=t",
HttpUtility.UrlEncode (translation),
Translator.LanguageEnumToIdentifier (targetLanguage),
translation.Length);
}
}
catch (Exception ex) {
this.Error = ex;
}
// Return result
this.TranslationTime = DateTime.Now - tmStart;
return translation;
No comments:
Post a Comment