C# – Translating with Google Translate without Using API

I am trying to build a simple C# project for fun. I needed a translator in my project and I found Google Translate’s API which is very easy to use and clean. But it requires an api key which requires some of your credidental information. For a small fun project, I found it unnecessary. Then I wrote this code, which uses google translate’s mobile page to get translation. The mobile page stores the result in between <div dir=”ltr” class=”t0″>, </div> tags. The code just parses the result between these tags.

public static string Translate(string text, string from, string to)
{
    string page = null;
    try
    {
        WebClient wc = new WebClient();
        wc.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0");
        wc.Headers.Add(HttpRequestHeader.AcceptCharset, "UTF-8");
        wc.Encoding = Encoding.UTF8;

        string url = string.Format(@"http://translate.google.com.tr/m?hl=en&sl={0}&tl={1}&ie=UTF-8&prev=_m&q={2}",
                                    from, to, Uri.EscapeUriString(text));

        page = wc.DownloadString(url);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
        return null;
    }

    page = page.Remove(0, page.IndexOf("<div dir=\"ltr\" class=\"t0\">")).Replace("<div dir=\"ltr\" class=\"t0\">", "");
    int last = page.IndexOf("</div>");
    page = page.Remove(last, page.Length - last);

    return page;
}

Usage is like this:

string translation = Translate("Your text to translate.", "en", "tr");

It supports multiple sentences. I didnt test how long of text it supports. I didnt use any HTML parser to avoid using other libraries.

Just mind that, the code is not stable. If the pages layout changes, the code will fail. And I am not a C# expert(actually I’m not a expert on anything), so feel free to say my mistakes and if you have a better idea, please share in comments.

One Comment

  1. A little bit faster version:

    public static string Translate(string sourceText, string sourceLanguage, string targetLanguage)
    {
    try
    {
    var wc = new WebClient();
    wc.Headers[HttpRequestHeader.UserAgent] = “Mozilla/5.0”;
    wc.Headers[HttpRequestHeader.AcceptCharset] = “UTF-8″;
    wc.Encoding = Encoding.UTF8;

    var page = wc.DownloadString(string.Format(@”http://translate.google.com/m?sl={0}&tl={1}&ie=UTF-8&q={2}”, sourceLanguage, targetLanguage, Uri.EscapeUriString(sourceText)));

    int startIndex = 0, endIndex = 0;
    for (int i = page.IndexOf(“”); i ‘)
    startIndex = i + 1;

    if (startIndex > 0 && endIndex == 0 && page[i] == ‘<')
    {
    endIndex = i;
    break;
    }
    }

    var count = endIndex – startIndex;
    var targetText = new char[count];
    page.CopyTo(startIndex, targetText, 0, count);
    return new string(targetText);
    }
    catch (Exception ex)
    {
    if (DialogResult.Retry == MessageBox.Show(ex.Message.ToString(), "WebClient: Google Translate", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1))
    Translate(sourceText, sourceLanguage, targetLanguage);
    return sourceText;
    }
    }

Leave a Reply