`
jrails
  • 浏览: 98536 次
  • 性别: Icon_minigender_1
  • 来自: 珠海
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
Google Google翻译post提交无长度限制的在线翻译例子
/**
 * http://ahomeeye.iteye.com/blog/841298
 * @author ahomeeye
 */
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

public class GoogleTranslate {

    private static HttpPost httppost;
    private static HttpResponse response;
    private static final DefaultHttpClient httpclient = new DefaultHttpClient();

    /*
     * @param text 翻译原文
     * @param lanFrom 源语言
     * @param lanTo 目标语言
     * @return 翻译后的字符串
     */
    public static String translate(String text, String lanFrom, String lanTo) {
        List<NameValuePair> qparams = new ArrayList<NameValuePair>();
        qparams.add(new BasicNameValuePair("hl", "en"));
        qparams.add(new BasicNameValuePair("ie", "UTF-8"));
        qparams.add(new BasicNameValuePair("sl", lanFrom));
        qparams.add(new BasicNameValuePair("text", text));
        qparams.add(new BasicNameValuePair("tl", lanTo));

        //HttpClient提交post请求
        httppost = new HttpPost("http://translate.google.com/translate_t#");

        String responseBody = "";
        String content = "";
        try {
            //将参数封装到post数据包中,
            httppost.setEntity(new UrlEncodedFormEntity(qparams, HTTP.UTF_8));
            response = httpclient.execute(httppost);
            responseBody = EntityUtils.toString(response.getEntity());

            //过滤出所需翻译后的内容
            int tmp1 = responseBody.indexOf("result_box");
            int tmp2 = responseBody.indexOf(">", tmp1);
            int tmp3 = responseBody.indexOf("</div>", tmp2);
            content = responseBody.substring(tmp2 + 1, tmp3).replaceAll("<br>", "\n").replaceAll("<[^>]*>", "");//替换换行符和其他网页标签
        } catch (Exception e) {
            return content;
        } finally {
            httppost.abort();
        }
        return content;
    }

    /*
     * 重载上面的translate()方法,实现文本数组的翻译
     */
    public static String[] translate(String[] text, String lanFrom, String lanTo) {
        if (text == null || text.length < 1) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < text.length; i++) {
            if (i < text.length - 1) {
                sb.append(text[i]).append("BBaaBB");
            } else {
                sb.append(text[i]);
            }
        }
        return translate(sb.toString(), lanFrom, lanTo).split("BBaaBB");
    }

    public static void main(String[] args) {
        String text = "我是一个中国人。";
        String[] arrayText = {"我来自广东梅州。", "我爱你,梅州!"};
        //这里实现多国语言间的翻译,大约支持59种语言,语言代码这里不再列出,有需要可到网上搜索
        //语言字符串为国家或地区语言代码,一般为两位,繁体中文为:zh-TW 法语为:fr
        String tranText = GoogleTranslate.translate(text, "zh-CN", "en");
        String[] tranArray = GoogleTranslate.translate(arrayText, "zh-CN", "en");
        System.out.println("tranText=" + tranText);
        for (int i = 0; i < tranArray.length; i++) {
            System.out.println("tranArray[" + i + "]=" + tranArray[i]);
        }
    }
}
Global site tag (gtag.js) - Google Analytics