Пример #1
0
        public string[] Translate(string[] words)
        {
            if (words == null || words.Length == 0) return null;
            bool multi = words.Length > 1;
            string text = multi ? string.Join("\u0000", words) : words[0];

            StringBuilder url = new StringBuilder(UrlPrefix);
            url.AppendFormat("/Translate.ashx?Text={0}&Kind={1}&OneTrans=&NoWords=", HttpUtility.UrlEncode(text), "1");
            if (multi)
            {
                url.Append("&MultiText=");
            }
            TranslateResult result = null;
            try
            {
                using (WebClient web = new WebClient())
                {
                    byte[] buffer = web.DownloadData(url.ToString());
                    using (MemoryStream stream = new MemoryStream(buffer))
                    {
                        XmlReaderX reader = new XmlReaderX();
                        reader.Stream = stream;
                        result = reader.ReadObject(typeof(TranslateResult)) as TranslateResult;
                    }
                }
            }
            catch (Exception ex)
            {
                //throw new Exception("访问在线翻译服务时发生了异常", ex);
                XTrace.WriteException(ex);

                // 出错时返回空,不应该引起上层错误
                return null;
            }
            if (result.Status == 0)
            {
                if (result.TextTranslations != null && result.TextTranslations.Count > 0)
                {
                    string[] ret = result.TextTranslations.ConvertAll<string>(delegate(TextTrans t)
                    {
                        if (t.Translations != null && t.Translations.Count > 0)
                        {
                            return t.Translations[0].Text;
                        }
                        return t.Original;
                    }).ToArray();
                    if (ret != null && ret.Length == 0) ret = null;
                    return ret;
                }
                return null;
            }
            else
            {
                throw new Exception(string.Format("访问在线翻译服务时发生服务端异常 {0}:{1}",
                    result.Status, string.Join(", ", result.Messages.ToArray())));
            }
        }
Пример #2
0
        public string[] Translate(string[] words)
        {
            if (words == null || words.Length == 0) return null;
            bool multi = words.Length > 1;
            string text = multi ? string.Join("\u0000", words) : words[0];
            //// 暂时限制400个单词,否则URL超长
            //string text = multi ? string.Join("\u0000", words, 0, Math.Min(words.Length, 100)) : words[0];

            var url = String.Format("{0}/Translate.ashx", UrlPrefix);
            var urlData = new StringBuilder();
            urlData.AppendFormat("Text={0}&Kind={1}&OneTrans=&NoWords=", HttpUtility.UrlEncode(text), "1");
            if (multi)
            {
                urlData.Append("&MultiText=");
            }
            TranslateResult result = null;
            try
            {
                using (var web = new WebClient())
                {
                    //byte[] buffer = web.DownloadData(url.ToString());
                    //using (MemoryStream stream = new MemoryStream(buffer))
                    //{
                    web.Encoding = Encoding.UTF8;
                    web.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";

                    var buffer = web.UploadData(url, Encoding.UTF8.GetBytes(urlData.ToString()));
                    //var buffer = web.DownloadData(url + "?" + urlData.ToString());
                    var reader = new XmlReaderX();
                    reader.Stream = new MemoryStream(buffer);
                    result = reader.ReadObject(typeof(TranslateResult)) as TranslateResult;
                    //}
                }
            }
            catch (Exception ex)
            {
                //throw new Exception("访问在线翻译服务时发生了异常", ex);
                XTrace.WriteException(ex);

                // 出错时返回空,不应该引起上层错误
                return null;
            }
            if (result.Status == 0)
            {
                if (result.TextTranslations != null && result.TextTranslations.Count > 0)
                {
                    string[] ret = result.TextTranslations.ConvertAll<string>(delegate(TextTrans t)
                    {
                        if (t.Translations != null && t.Translations.Count > 0)
                        {
                            return t.Translations[0].Text;
                        }
                        return t.Original;
                    }).ToArray();
                    if (ret != null && ret.Length == 0) ret = null;
                    return ret;
                }
                return null;
            }
            else
            {
                throw new Exception(string.Format("访问在线翻译服务时发生服务端异常 {0}:{1}",
                    result.Status, string.Join(", ", result.Messages.ToArray())));
            }
        }
Пример #3
0
        /// <summary>向翻译服务添加新的翻译条目</summary>
        /// <param name="Kind"></param>
        /// <param name="Source"></param>
        /// <param name="trans"></param>
        /// <returns></returns>
        public int TranslateNewWithSource(string Kind, string Source, params string[] trans)
        {
            if (Kind != null) Kind = Kind.Trim();
            if (string.IsNullOrEmpty(Kind)) throw new ArgumentException("翻译类型不能为空", "Kind");
            if (trans == null || trans.Length == 0) return 0;
            if ((trans.Length & 1) == 1)
            {
                throw new Exception("翻译条目不是成对的,条目数量必须是2的倍数");
            }

#if DEBUG
            UrlPrefix = "http://localhost:9005/Web";
#endif

            string url = UrlPrefix + string.Format("/TranslateNew.ashx?Kind={0}&Source={1}", HttpUtility.UrlEncode(Kind), HttpUtility.UrlEncode(Source));

            var data = new StringBuilder();
            for (int i = 0; i < trans.Length; i += 2)
            {
                string o = trans[i], t = trans[i + 1];
                if (!string.IsNullOrEmpty(o) && !string.IsNullOrEmpty(t))
                {
                    data.AppendFormat("&O={0}&T={1}", HttpUtility.UrlEncode(o), HttpUtility.UrlEncode(t));
                }
            }
            if (data.Length > 1)
            {
                data.Remove(0, 1);
            }
            else if (data.Length == 0)
            {
                throw new Exception("没有可添加的翻译条目");
            }

            TranslateNewResult result = null;
            try
            {
                using (var web = new WebClient())
                {
                    web.Encoding = Encoding.UTF8;
                    web.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
                    byte[] buffer = web.UploadData(url, Encoding.UTF8.GetBytes(data.ToString()));
                    using (var stream = new MemoryStream(buffer))
                    {
                        var reader = new XmlReaderX();
                        reader.Stream = stream;
                        result = reader.ReadObject(typeof(TranslateNewResult)) as TranslateNewResult;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("访问在线翻译服务时发生了异常", ex);
            }
            if (result.Status == 0)
            {
                return result.Accepted;
            }
            else
            {
                throw new Exception(string.Format("访问在线翻译服务时发生服务端异常 {0}:{1}", result.Status, result.Message));
            }
        }
Пример #4
0
        public string[] Translate(string[] words)
        {
            if (words == null || words.Length == 0)
            {
                return(null);
            }
            bool   multi = words.Length > 1;
            string text  = multi ? string.Join("\u0000", words) : words[0];
            //// 暂时限制400个单词,否则URL超长
            //string text = multi ? string.Join("\u0000", words, 0, Math.Min(words.Length, 100)) : words[0];

            var url     = String.Format("{0}/Translate.ashx", UrlPrefix);
            var urlData = new StringBuilder();

            urlData.AppendFormat("Text={0}&Kind={1}&OneTrans=&NoWords=", HttpUtility.UrlEncode(text), "1");
            if (multi)
            {
                urlData.Append("&MultiText=");
            }
            TranslateResult result = null;

            try
            {
                using (var web = new WebClient())
                {
                    //byte[] buffer = web.DownloadData(url.ToString());
                    //using (MemoryStream stream = new MemoryStream(buffer))
                    //{
                    web.Encoding = Encoding.UTF8;
                    web.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";

                    var buffer = web.UploadData(url, Encoding.UTF8.GetBytes(urlData.ToString()));
                    //var buffer = web.DownloadData(url + "?" + urlData.ToString());
                    var reader = new XmlReaderX();
                    reader.Stream = new MemoryStream(buffer);
                    result        = reader.ReadObject(typeof(TranslateResult)) as TranslateResult;
                    //}
                }
            }
            catch (Exception ex)
            {
                //throw new Exception("访问在线翻译服务时发生了异常", ex);
                XTrace.WriteException(ex);

                // 出错时返回空,不应该引起上层错误
                return(null);
            }
            if (result.Status == 0)
            {
                if (result.TextTranslations != null && result.TextTranslations.Count > 0)
                {
                    string[] ret = result.TextTranslations.ConvertAll <string>(delegate(TextTrans t)
                    {
                        if (t.Translations != null && t.Translations.Count > 0)
                        {
                            return(t.Translations[0].Text);
                        }
                        return(t.Original);
                    }).ToArray();
                    if (ret != null && ret.Length == 0)
                    {
                        ret = null;
                    }
                    return(ret);
                }
                return(null);
            }
            else
            {
                throw new Exception(string.Format("访问在线翻译服务时发生服务端异常 {0}:{1}",
                                                  result.Status, string.Join(", ", result.Messages.ToArray())));
            }
        }
Пример #5
0
        /// <summary>向翻译服务添加新的翻译条目</summary>
        /// <param name="Kind"></param>
        /// <param name="Source"></param>
        /// <param name="trans"></param>
        /// <returns></returns>
        public int TranslateNewWithSource(string Kind, string Source, params string[] trans)
        {
            if (Kind != null)
            {
                Kind = Kind.Trim();
            }
            if (string.IsNullOrEmpty(Kind))
            {
                throw new ArgumentException("翻译类型不能为空", "Kind");
            }
            if (trans == null || trans.Length == 0)
            {
                return(0);
            }
            if ((trans.Length & 1) == 1)
            {
                throw new Exception("翻译条目不是成对的,条目数量必须是2的倍数");
            }

#if DEBUG
            UrlPrefix = "http://localhost:9005/Web";
#endif

            string url = UrlPrefix + string.Format("/TranslateNew.ashx?Kind={0}&Source={1}", HttpUtility.UrlEncode(Kind), HttpUtility.UrlEncode(Source));

            var data = new StringBuilder();
            for (int i = 0; i < trans.Length; i += 2)
            {
                string o = trans[i], t = trans[i + 1];
                if (!string.IsNullOrEmpty(o) && !string.IsNullOrEmpty(t))
                {
                    data.AppendFormat("&O={0}&T={1}", HttpUtility.UrlEncode(o), HttpUtility.UrlEncode(t));
                }
            }
            if (data.Length > 1)
            {
                data.Remove(0, 1);
            }
            else if (data.Length == 0)
            {
                throw new Exception("没有可添加的翻译条目");
            }

            TranslateNewResult result = null;
            try
            {
                using (var web = new WebClient())
                {
                    web.Encoding = Encoding.UTF8;
                    web.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
                    byte[] buffer = web.UploadData(url, Encoding.UTF8.GetBytes(data.ToString()));
                    using (var stream = new MemoryStream(buffer))
                    {
                        var reader = new XmlReaderX();
                        reader.Stream = stream;
                        result        = reader.ReadObject(typeof(TranslateNewResult)) as TranslateNewResult;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("访问在线翻译服务时发生了异常", ex);
            }
            if (result.Status == 0)
            {
                return(result.Accepted);
            }
            else
            {
                throw new Exception(string.Format("访问在线翻译服务时发生服务端异常 {0}:{1}", result.Status, result.Message));
            }
        }