using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Text.RegularExpressions;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
TranslateText("hi", "German");
}
private void Form1_Load(object sender, EventArgs e)
{
}
public static string TranslateText(string input, string languagePair)
{
return TranslateText(input, languagePair, System.Text.Encoding.UTF7);
}
/// <summary>
/// Translate Text using Google Translate
/// </summary>
/// <param name="input">The string you want translated</param>
/// <param name="languagePair">2 letter Language Pair, delimited by "|".
/// e.g. "en|da" language pair means to translate from English to Danish</param>
/// <param name="encoding">The encoding.</param>
/// <returns>Translated to String</returns>
public static string TranslateText(string input, string languagePair, Encoding encoding)
{
string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair);
string result = String.Empty;
using (WebClient webClient = new WebClient())
{
webClient.Encoding = encoding;
result = webClient.DownloadString(url);
}
Match m = Regex.Match(result, "(?<=<div id=result_box dir=\"ltr\">)(.*?)(?=</div>)");
if (m.Success)
result = m.Value;
MessageBox.Show(result);
return result;
}
}
}
我在构造函数中添加一行:为什么谷歌翻译代码不起作用?
TranslateText("hi", "German");
而且在底部我说:
MessageBox.Show(result);
我想为测试这个词翻译“喜”德国 但结果即时消息和消息框中是一个非常长的文本,其中包含所有的谷歌网站。
我试图去网站中的字符串url地址和它的工作即时通讯到谷歌翻译网站。
我不明白为什么它剂量的工作。 我希望以后可以将文本文件中的某些文本“嗨”改为“嗨”。
我试着OT使用断点,发现这部分的成功是所有的时间返回false不知道为什么:
if (m.Success)
result = m.Value;
谷歌是不是免费的翻译工具。你所做的是违反条款 – zerkms 2012-04-25 00:28:50