zoukankan      html  css  js  c++  java
  • google翻译

    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Text;
    using System.Net;
    using System.Text.RegularExpressions;
    
    namespace Web
    {
        public class GoogleTranslate
        {
            private string UrlTemplate = "http://translate.google.com.hk/";                            //google翻译URL模板:POST方式请求
            private string[] Languages = { "en", "zh-CN", "fr", "ja", "ko" ,"ru"};
            #region 常用语言编码
            private string AutoDetectLanguage = "auto"; //google自动判断来源语系
            #endregion
            private static GoogleTranslate translate;
            private GoogleTranslate()
            { }
    
            public GoogleTranslate Instance()
            {
                if (translate == null)
                    translate = new GoogleTranslate();
                return translate;
            }
    
            /// <summary>
            /// 翻译文本[自动检测源语言类型]
            /// </summary>
            /// <param name="sourceText">源文本</param>
            /// <param name="targetLanguageCode">目标语言类型代码,0英语1汉语2法语3日语4韩语5俄语</param>
            /// <returns>翻译结果</returns>
            public string Translate(string sourceText, int targetLanguageCode)
            {
                return Translate(sourceText, AutoDetectLanguage, Languages[targetLanguageCode]);
            }
    
            /// <summary>
            /// 翻译文本
            /// </summary>
            /// <param name="sourceText">源文本</param>
            /// <param name="sourceLanguageCode">源语言类型代码,如:en、zh-CN、zh-TW、ru等</param>
            /// <param name="targetLanguageCode">目标语言类型代码,如:en、zh-CN、zh-TW、ru等</param>
            /// <returns>翻译结果</returns>
            private string Translate(string sourceText, string sourceLanguageCode, string targetLanguageCode)
            {
                if (string.IsNullOrEmpty(sourceText) || Regex.IsMatch(sourceText, @"^s*$"))
                {
                    return sourceText;
                }
    
                string strReturn = string.Empty;
    
                #region POST方式实现,无长度限制
                string url = UrlTemplate;
    
                //组织请求的数据
                string postData = string.Format("langpair={0}&text={1}", HttpUtility.UrlEncode(sourceLanguageCode + "|" + targetLanguageCode), HttpUtility.UrlEncode(sourceText));
                byte[] bytes = Encoding.UTF8.GetBytes(postData);
    
                WebClient client = new WebClient();
                client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                client.Headers.Add("ContentLength", postData.Length.ToString());
                byte[] responseData = client.UploadData(url, "POST", bytes);
                string strResult = Encoding.UTF8.GetString(responseData);    //响应结果 
                #endregion
                #region 获取result_box中的结果
                int index = strResult.IndexOf("result_box");
                if (index > 0)
                {
                    strResult = strResult.Substring(index);
                    index = strResult.IndexOf("<span");
                    strResult = strResult.Substring(index);
                    index = strResult.IndexOf(">");
                    strResult = strResult.Substring(index);
                    index = strResult.IndexOf("</span>");
                    strResult = strResult.Substring(1, index - 1);
                }
                #endregion
                else strResult = "";
                return strResult;
            }
        }
    }
  • 相关阅读:
    WiFi 和蓝牙一体的 ESP32 芯片及乐鑫官方的 Arduino 开发工具包
    给模块添加 USB 支持的 USB Host Shield Mini
    设备间数据通信 —— 串行外设接口(SPI)协议
    ESP32 开发板刷入 MicroPython
    芯片、模组、开发板以及业余爱好者如何选择
    快速了解线路板(PCB)基础知识
    终端发展过程及 tty、pty、pts 的区别
    时间类型和时间戳
    使用Qt实现一个必应壁纸客户端
    给网站添加Let's Encrypt的免费ssl证书
  • 原文地址:https://www.cnblogs.com/mengxingxinqing/p/3284118.html
Copyright © 2011-2022 走看看