Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C#

Google Translator

Rate me:
Please Sign up or sign in to vote.
4.94/5 (142 votes)
17 Mar 2016CPOL2 min read 1.7M   40.1K   278   482
An object that harnesses the power of Google's online natural language translation tools.

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.

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 @Airstriker82, @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 in action 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 use GoogleTranslator by constructing it and calling its Translate() method.

C#
 
    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.

C#
 
    // 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.googleapis.com/translate_a/single?client=gtx&sl={0}&tl={1}&dt=t&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.googleapis.com/translate_tts?ie=UTF-8&q={0}&tl={1}&total=1&idx=0&textlen={2}&client=gtx",
                                                       HttpUtility.UrlEncode (translation),
                                                       Translator.LanguageEnumToIdentifier (targetLanguage),
                                                       translation.Length);
        }
    }
    catch (Exception ex) {
        this.Error = ex;
    }

    // Return result
    this.TranslationTime = DateTime.Now - tmStart;
    return translation;

As you can see, the logic used to parse the JSON result is very simple!

Speaking the translation

The Translator object retrieves the URL that will stream the spoken version of the translation. The demo app speaks this content by navigating to this URL in a hidden browser control. As mentioned in the preamble, 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!

Revision History

  • 18 Mar 2016
    Switched to Google Translate plugin APIs.  Fix identified by User-12366202.  Thank you!
  • 6 Aug 2015
    Corrected parsing logic.  Fix identified by Member 11019371.  Thank you!
  • 6 May 2015
    Corrected parsing logic to fix translation of single words.
  • 5 May 2015
    Corrected Google URL.
    Removed all external dependencies.
  • 9 Mar 2014
    Switched to using Google's JSON translation APIs.
    Added TranslationTime and TranslationSpeakUrl properties.
    Tweaked demo app UI to assist in reverse translation and resetting an English source and target.
  • 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.

License

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


Written By
Technical Lead
Canada Canada
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, HCI 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 3 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.

Comments and Discussions

 
Questiontranslate.googleapis.com Pin
jung-kreidler14-Feb-17 23:37
jung-kreidler14-Feb-17 23:37 
AnswerRe: translate.googleapis.com Pin
Ravi Bhavnani16-Feb-17 4:55
professionalRavi Bhavnani16-Feb-17 4:55 
QuestionRegarding Language Detection Pin
Member 1299758414-Feb-17 2:40
Member 1299758414-Feb-17 2:40 
AnswerRe: Regarding Language Detection Pin
Ravi Bhavnani14-Feb-17 11:29
professionalRavi Bhavnani14-Feb-17 11:29 
BugProblem with various symbols Pin
Sandl Okino31-Jul-16 21:39
Sandl Okino31-Jul-16 21:39 
GeneralRe: Problem with various symbols Pin
Ravi Bhavnani1-Aug-16 5:55
professionalRavi Bhavnani1-Aug-16 5:55 
GeneralRe: Problem with various symbols Pin
Sandl Okino19-Sep-16 17:13
Sandl Okino19-Sep-16 17:13 
GeneralRe: Problem with various symbols Pin
bgsjust4-Jan-17 10:57
professionalbgsjust4-Jan-17 10:57 
I remember in my code i had to make several changes in the text before and after translation here is the code it may need some adaptations:

private static Dictionary<string, string=""> ReplacementsA = null;
private static Dictionary<string, string=""> ReplacementsB = null;
private static Dictionary<string, string=""> ReplacementsC = null;


// Initialize replacement arrays
private static void Init_Replacements()
{
ReplacementsA = new Dictionary<string, string="">
{
{"%", "<p0c>"},
{"&", "<c0e>"},
{";", "<p0v>"},
{"[", "<sb0l>"},
{"]", "<sb0r>"}
};

ReplacementsB = new Dictionary<string, string="">
{
{" @", "@"},
{"@ ", "@"},
{"'", "'"},
{"'", "'"},
{""", "\""},
{"& lt ;", "<"},
{"<", "<"},
{">", ">"},
{"& gt ;", ">"},
{"\u00ae", "®"},
{"%0d", "\n"},
{"%0D", "\n"},
{"%0a", "\n"},
{"☺", "\n"}, // mymemory end of line replacement
{"\\u003c", "<"},
{"\\u003e", ">"}
};

ReplacementsC = new Dictionary<string, string="">
{
{"<p0c>", "%"},
{"<p0c>", "%"},
{"<c0e>", "&"},
{"<c0e>", "&"},
{"<p0v>", ";"},
{"<p0v>", ";"},
{"<p0v>", ";"},
{"<> P0V", ";"},
{"<פּ0וו>", ";"},
{"",""},
{"",""},
{"<sb0l>","["},
{"<sb0l>","["},
{"<sb0l>","["},
{"<sb0r>","]"},
{"<sb0r>","]"},
{"<sb0r>","]"},
{" ;",";"},
{"//","/"}
};
}

public static string replace_chars_before(string storeplace)
{
return StringBuilderReplace(new StringBuilder(storeplace), ReplacementsA);
}

public static string replace_chars_after(string storeplace)
{
while (storeplace.IndexOf(" ;") != -1) storeplace = storeplace.Replace(" ;", ";");
storeplace = StringBuilderReplace(new StringBuilder(storeplace), ReplacementsB);
while (storeplace.IndexOf(" >") != -1) storeplace = storeplace.Replace(" >", ">");
while (storeplace.IndexOf("< ") != -1) storeplace = storeplace.Replace("< ", "<");
return StringBuilderReplace(new StringBuilder(storeplace), ReplacementsC);
}
AnswerRe: Problem with various symbols Pin
sanslash3323-Feb-18 5:14
sanslash3323-Feb-18 5:14 
Questionlanguage detection Pin
Member 1234190823-May-16 12:37
Member 1234190823-May-16 12:37 
AnswerRe: language detection Pin
Ravi Bhavnani23-May-16 18:07
professionalRavi Bhavnani23-May-16 18:07 
QuestionRegards Pin
Member 1246528120-Apr-16 17:07
Member 1246528120-Apr-16 17:07 
AnswerRe: Regards Pin
Ravi Bhavnani21-Apr-16 2:58
professionalRavi Bhavnani21-Apr-16 2:58 
QuestionExcellent Article - 5 Star Pin
Garry Lowther24-Mar-16 5:18
Garry Lowther24-Mar-16 5:18 
AnswerRe: Excellent Article - 5 Star Pin
Ravi Bhavnani24-Mar-16 5:23
professionalRavi Bhavnani24-Mar-16 5:23 
GeneralMy vote of 5 Pin
Arkitec22-Mar-16 15:16
professionalArkitec22-Mar-16 15:16 
GeneralRe: My vote of 5 Pin
Ravi Bhavnani23-Mar-16 2:30
professionalRavi Bhavnani23-Mar-16 2:30 
PraiseGreat little project Pin
Woody D21-Mar-16 13:42
Woody D21-Mar-16 13:42 
GeneralRe: Great little project Pin
Ravi Bhavnani21-Mar-16 14:35
professionalRavi Bhavnani21-Mar-16 14:35 
GeneralRe: Great little project Pin
Woody D21-Mar-16 15:03
Woody D21-Mar-16 15:03 
GeneralUnderstatement of the year Pin
Stephen McCafferty21-Mar-16 3:00
Stephen McCafferty21-Mar-16 3:00 
GeneralRe: Understatement of the year Pin
Ravi Bhavnani21-Mar-16 7:22
professionalRavi Bhavnani21-Mar-16 7:22 
SuggestionMessage Closed Pin
17-Mar-16 12:23
Julian Sychowski17-Mar-16 12:23 
PraiseRe: working version Pin
Ravi Bhavnani17-Mar-16 18:40
professionalRavi Bhavnani17-Mar-16 18:40 
GeneralRe: working version Pin
bgsjust21-Mar-16 13:11
professionalbgsjust21-Mar-16 13:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.