1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Net;
namespace
traducteur
{
class googleTranslator {
// <summary>
/// Translate Text using Google Translate API's
/// Google URL - http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}
/// </summary>
/// <param name="input"> Input string </param>
/// <param name="languagePair"> 2 letter Language Pair, delimited by "|".
/// E.g. "ar|en" language pair means to translate from Arabic to English </param>
/// <returns> Translated to String </returns>
public static string TranslateText( string input, string languagePair){
string url = String .Format( "http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}" , input, languagePair);
WebClient webClient = new WebClient ();
webClient.Encoding = System.Text.
Encoding .UTF8;
string result = webClient.DownloadString(url);
MessageBox .Show(result.Length.ToString()+ " | " +result.IndexOf( "id=result_box" ).ToString());
result = result.Substring(result.IndexOf(
"id=result_box" ) + 20, result.IndexOf( "id=result_box" ) + 500);
result = result.Substring(0, result.IndexOf(
"</div" ));
return result;
} |