0

First of all, is not a solution for me to pay for a GoogleTranslate API.

I'm trying to translate with a Get method a simple phrase that contains a especial character "&".

"Me & You"

This is the method I wrote:

Public Function Google_Translate(ByVal Input As String, _
                                 ByVal From_Language As Languages, _
                                 ByVal To_Language As Languages) As String

    Dim webClient As New System.Net.WebClient

    Dim str = webClient.DownloadString( _
    "http://translate.google.com/translate_a/t?client=t&text=" & Input & _
    "&sl=" & Formatted_From_Language & _
    "&tl=" & Formatted_To_Language & "")

    ' Debug: MsgBox(str)

    Return (str.Substring(4, str.Length - 4).Split(ControlChars.Quote).First)

End Function

This is a usage example:

Google_Translate("Me and you", Languages.en, Languages.en)

The result is the same string 'cause I've translated from english to english:

"Me and you"

The problem is when I try to use any especial HTML character, for example the "&":

Google_Translate("Me & you", Languages.en, Languages.en)

Result:

"Me"

This is the string without the split:

[[["Me","Me","",""]],,"en",,,,,,,0]

This is all I've tried:

Unicode identifiers:

Google_Translate("Me \u0026 you")

HTML Entities:

Google_Translate("Me & you")

HTML Escaped entities:

Google_Translate("Me &H38; you")

...And HTML percents:

Google_Translate("Me %26 you")

...Calling the method using the percents I get a string with an Unicode identifier:

[[["Me \u0026 you","Me \u0026 you","",""]],,"en",,,,,,[["en"]],0]

That maybe will mean the only thing that I need to do is to get the string from Google and translate the unicode identifier and that's all? ...NOT! 'cause if I call Google using other special character, I don't get any unicode identifier:

Google_Translate("Hello·"" World¿?", GoogleTranslate_Languages.en, GoogleTranslate_Languages.en)

Result:

"Hello·\" World¿?"

Result without the split:

[[["Hello·\" World¿?","Hello·\" World¿?","",""]],,"en",,,,,,[["en"]],0]

What I'm missing?

How I can send/get the data in the correct way using especial characters as &%$"¿? ?

ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
  • First of all, is not a solution for me to pay for a car. Whenever I try to get into `Elektro Hacker's` car, the alarm goes off. What I'm missing? – Robert McKee Jun 02 '13 at 06:06
  • @Robert McKee Really I can't understand your sarcasm, What's your problem?, The Google web service is free, why I will not try to use it?, a good programmer searchs for alternative ways to do the things. Some people don't need to pay for all the functionalities of the API, I want to use it in a sporadic way, I don't want to pay 20$ to use GoogleTranslate between 1 or 2 times each month only, also the API usage is limited to 20$ every 1.000.000 of translated characters (thiefs). – ElektroStudios Jun 02 '13 at 08:20
  • The google translate API REST web service is not free: https://developers.google.com/translate/ You are attempting to use the v1 API without paying for it, when it is a paid for service, which is indeed stealing. – Robert McKee Jun 03 '13 at 02:06
  • In addition, please read the last sentence of section 5 entitled "Fees": https://developers.google.com/translate/v2/terms, which states `You may not access the Translate API in a manner intended to avoid incurring fees.` – Robert McKee Jun 03 '13 at 02:11

3 Answers3

2

Firstly by using their web interface for automated queries you are almost certainly violating Google's Terms of use and also will make your application extremely brittle as there is nothing stopping Google from changing their front end code as they do regularly. Chances are if you are only doing two or three translations a month you will only get twenty translation before it breaks. Depending on how you value your time you may spend more time on fixing it to respond to Google's changes than using the API would cost. The API is cheap compared to most translation services.

If cost is a big issue it may be worth looking at the bing translation API which is free.

Finally the correct way to send the data is to use URI encoding (which you call html percents) for anything other than alphanumeric characters. The response is a JSON encoded string. Use a framework like JSON.NET to deserialize it.

user1937198
  • 4,987
  • 4
  • 20
  • 31
  • Thankyou for your answer, but I've tried the "percents", maybe can you show me a example using the URI? – ElektroStudios Jun 02 '13 at 11:41
  • @ElektroHacker Given hints on decoding however i strongly advise against what you are doing – user1937198 Jun 02 '13 at 11:51
  • Here is an example of using URL/URI encoding: https://www.googleapis.com/language/translate/v2/detect?q=Me+%26+You&source=en&target=en – Robert McKee Jun 03 '13 at 02:16
  • @Robert McKee thanks but only a question, that url requires the API key license true? I tried to open it and it says error code 403 unautorized use, I've tried the same codification in my code-example answer and don't works – ElektroStudios Jun 03 '13 at 08:22
1

Here's how (in C#):

First, you create your name value collection using the inputs you need like:

var nvc = new NameValueCollection
                {
                    {"q", input},
                    {"source", "en"},
                    {"target", "en"},
                    {"key","Your translate API key here"}
                };

Then you can call a function like this one:

internal string Post(string url, ref CookieContainer cookieJar, NameValueCollection nvc, string referer = null)
{
    var postdata = string.Join("&", Array.ConvertAll(nvc.AllKeys, key => string.Format("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(nvc[key]))));
    var request = (HttpWebRequest)WebRequest.Create(url);
    request.CookieContainer = cookieJar;
    request.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0";
    request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
    request.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
    request.Headers.Add("Accept-Encoding", "gzip, deflate");
    request.Headers.Add("Accept-Language", "en-us");
    request.Method = "POST";
    request.KeepAlive = true;
    request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = postdata.Length;
    if (!string.IsNullOrEmpty(referer))
        request.Referer = referer;

    var writer = new StreamWriter(request.GetRequestStream());
    writer.Write(postdata);
    writer.Close();

    var response = (HttpWebResponse)request.GetResponse();
    var resp = (new StreamReader(response.GetResponseStream())).ReadToEnd();
    return resp;
}

Here's the call:

var result=Post("https://www.googleapis.com/language/translate/v2/detect",ref new CookieContainer(),nvc);

for small values of input, you can use GET instead of POST, just append postdata onto the url after a ?.

For google translate API, the response is in JSON format, there are many posts on how to parse JSON responses, so I won't go into that here, but these should help you get started: How to decode a JSON string using C#? Convert JSON File to C# Object

Community
  • 1
  • 1
Robert McKee
  • 21,305
  • 1
  • 43
  • 57
  • thanks but I have a few issues, 1. the code requires the API purchase (I need to specify the key?)?, 2. I can't use the solutions for translate JSon 'cause JavaScriptSerializer is now obsolete, I found this: http://json.codeplex.com/ That will works to decode the string? – ElektroStudios Jun 03 '13 at 08:39
  • With the current terms, yes, you need to set up an billing account for the API to work, and you get the key through there. As for JSON, there are a few different ways to deserialize it, JSON.NET is one of the more popular ways, and you can install it through nuget. – Robert McKee Jun 03 '13 at 09:06
0

input: Web.HttpUtility.UrlEncode("You & Me")

output: Web.HttpUtility.HtmlDecode(result)

D T
  • 3,522
  • 7
  • 45
  • 89