2

使用 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(..)。

格式化程序在尝试反序列化消息时抛出异常:反序列化操作“翻译”的请求消息正文时出错。读取 XML 数据时已超出最大字符串内容长度配额 (30720)。可以通过更改创建 XML 阅读器时使用的 XmlDictionaryReaderQuotas 对象的 MaxStringContentLength 属性来增加此配额。第 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)异常。

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

4

1 回答 1

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()。

于 2016-07-03T16:15:31.320 回答