【问题标题】:Use Microsoft Translator API to translate a whole web page with C#使用 Microsoft Translator API 用 C# 翻译整个网页
【发布时间】:2015-11-05 10:10:36
【问题描述】:

使用 C# 使用 Microsoft Translator API (SOAP) 翻译网页。我想让我的网站翻译,但使用翻译器小部件对我不利,因为我还需要谷歌来抓取我的翻译页面。所以我需要先翻译它,然后再发送到浏览器。

到目前为止,还没有 API(我试过找到它,但我找不到,如果你碰巧知道,请提及)在那里你可以传递一个 url,它会向你发送翻译后的响应,如下所示:http://www.microsofttranslator.com/bv.aspx?from=&to=nl&a=http%3A%2F%2Fwww.imdb.com%2F

这些是我到目前为止所做的尝试: 1.从Url下载字符串,传递给Client.Translate(..)。

格式化程序在尝试反序列化 消息:反序列化操作请求消息正文时出错 '翻译'。最大字符串内容长度配额 (30720) 已 读取 XML 数据时超出。此配额可能会增加 更改 MaxStringContentLength 属性 创建 XML 阅读器时使用的 XmlDictionaryReaderQuotas 对象。 第 516 行,位置 48。

2.

private static void processDocument(HtmlAgilityPack.HtmlDocument html, LanguageServiceClient Client)
        {
            HtmlNodeCollection coll = html.DocumentNode.SelectNodes("//text()[normalize-space(.) != '']");
            foreach (HtmlNode node in coll)
            {
                if (node.InnerText == node.InnerHtml)
                {
                    //node.InnerHtml = translateText(node.InnerText);
                    node.InnerHtml = Client.Translate("", node.InnerText, "en", "fr", "text/html", "general");
                }
            }

        }

这个太花时间了。最后我得到了一个错误的请求 (400) 异常。

解决这个问题的最佳方法是什么?我还打算保存文件,这样我就不用每次都翻译了。

【问题讨论】:

    标签: c# asp.net microsoft-translator


    【解决方案1】:

    这个 C# 示例从本地文件转换 HTML:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    using HtmlAgilityPack;
    
    namespace TranslationAssistant.Business
    {
    class HTMLTranslationManager
    {
        public static int DoTranslation(string htmlfilename, string fromlanguage, string tolanguage)
        {
            string htmldocument = File.ReadAllText(htmlfilename);
            string htmlout = string.Empty;
    
            HtmlDocument htmlDoc = new HtmlDocument();
            htmlDoc.LoadHtml(htmldocument);
            htmlDoc.DocumentNode.SetAttributeValue("lang", TranslationServices.Core.TranslationServiceFacade.LanguageNameToLanguageCode(tolanguage));
            var title = htmlDoc.DocumentNode.SelectSingleNode("//head//title");
            if (title != null) title.InnerHtml = TranslationServices.Core.TranslationServiceFacade.TranslateString(title.InnerHtml, fromlanguage, tolanguage, "text/html");
            var body = htmlDoc.DocumentNode.SelectSingleNode("//body");
            if (body != null)
            {
                if (body.InnerHtml.Length < 10000)
                {
                    body.InnerHtml = TranslationServices.Core.TranslationServiceFacade.TranslateString(body.InnerHtml, fromlanguage, tolanguage, "text/html");
                }
                else
                {
                    List<HtmlNode> nodes = new List<HtmlNode>();
                    AddNodes(body.FirstChild, ref nodes);
    
                    Parallel.ForEach(nodes, (node) =>
                        {
                            if (node.InnerHtml.Length > 10000)
                            {
                                throw new Exception("Child node with a length of more than 10000 characters encountered.");
                            }
                            node.InnerHtml = TranslationServices.Core.TranslationServiceFacade.TranslateString(node.InnerHtml, fromlanguage, tolanguage, "text/html");
                        });
                }
            }
            htmlDoc.Save(htmlfilename, Encoding.UTF8);
            return 1;
        }
    
        /// <summary>
        /// Add nodes of size smaller than 10000 characters to the list, and recurse into the bigger ones.
        /// </summary>
        /// <param name="rootnode">The node to start from</param>
        /// <param name="nodes">Reference to the node list</param>
        private static void AddNodes(HtmlNode rootnode, ref List<HtmlNode> nodes)
        {
            string[] DNTList = { "script", "#text", "code", "col", "colgroup", "embed", "em", "#comment", "image", "map", "media", "meta", "source", "xml"};  //DNT - Do Not Translate - these nodes are skipped.
            HtmlNode child = rootnode;
            while (child != rootnode.LastChild)
            {
                if (!DNTList.Contains(child.Name.ToLowerInvariant())) {
                    if (child.InnerHtml.Length > 10000)
                    {
                        AddNodes(child.FirstChild, ref nodes);
                    }
                    else
                    {
                        if (child.InnerHtml.Trim().Length != 0) nodes.Add(child);
                    }
                }
                child = child.NextSibling;
            }
        }
    
    }
    }
    

    这是http://github.com/microsofttranslator/documenttranslator 中的 HTMLTranslationManager.cs,它使用了 TranslationServiceFacade.cs 中的帮助函数 TranslateString()。您可以简化并在此处插入翻译服务调用来代替 TranslateString()。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-19
      • 2014-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多