简体翻译成繁体(三)

2020-03-07  本文已影响0人  泱千澈
using System;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;

public class ChineseConverter {
    public static string GetWebString(string cc)
    {
        HttpWebRequest request = null;
        HttpWebResponse response = null;
        const string url = "https://brushes8.com/zhong-wen-jian-ti-fan-ti-zhuan-huan";
        if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
        {
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
            request = WebRequest.Create(url) as HttpWebRequest;
            request.ProtocolVersion = HttpVersion.Version11;
        }
        else
        {
            request = WebRequest.Create(url) as HttpWebRequest;
        }
        request.Method = "POST";
        request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36";
        request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3";
        request.ContentType = "application/x-www-form-urlencoded";
        request.AllowAutoRedirect = true;
        request.KeepAlive = true;//建立持久性连接
                                 //整数据

        string postData = string.Format("data={0}&variant=zh-tw&dochineseconversion=1&submit=开始转换 (Ctrl + Enter)", cc);
        byte[] bytepostData = Encoding.UTF8.GetBytes(postData);
        request.ContentLength = bytepostData.Length;

        //发送数据 using结束代码段释放
        using (Stream requestStm = request.GetRequestStream())
        {
            requestStm.Write(bytepostData, 0, bytepostData.Length);
        }

        //响应
        response = (HttpWebResponse)request.GetResponse();
        StringBuilder sb = new StringBuilder();
        string text = string.Empty;
        using (Stream responseStm = response.GetResponseStream())
        {
            if (response.ContentEncoding.ToLower().Contains("gzip"))
            {
                using (GZipStream stream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress))
                {
                    using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                    {

                        text = reader.ReadToEnd();
                    }
                }
            }
            else if (response.ContentEncoding.ToLower().Contains("deflate"))
            {
                using (DeflateStream stream = new DeflateStream(response.GetResponseStream(), CompressionMode.Decompress))
                {
                    using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                    {

                        text = reader.ReadToEnd();
                    }

                }
            }
            else
            {
                using (Stream stream = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                    {

                        text = reader.ReadToEnd();
                    }
                }
            }
        }

        text = text.Substring(14457);
        return text;
    }

    private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
    {
        return true;
    }
}
1. 获取FormData
示范分析过程
各个参数
参数连接
从图片中可以看出我们所要提交的postData的结构:data={0}&variant=zh-tw&dochineseconversion=1&submit=开始转换 (Ctrl + Enter){0}即是我们输入的字符串。
2.设置request的参数

把参数一一设置到代码

request.Method = "POST";
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3";
request.ContentType = "application/x-www-form-urlencoded";
3. & 问题

如果我有一个lang.xml,里面的内容是这样的


对应文本如下:

<root>
<n k="啧啧。看到这些排列的整整齐齐的机器人,就忍不住想冲进去大闹一场啊……" v="" />
<n k="至少说明我们来对了地方。准备动手吧!" v="" />
<n k="恶作剧发明家,今天我可是留手了哦。" v="" />
<n k="以你的战斗力,留不留手根本无所谓。" v="" />
<n k="你们一碰面就会吵架吗?难道你们从来没配合过……" v="" />
<n k="毕昇&amp;达尔文" v="" />
<n k="(异口同声)谁要和这样的家伙配合啊?" v="" />
<n k="两人对视片刻" v="" />
<n k="(扭头)哼!" v="" />
<n k="(这个同步率……)" v="" />
<n k="前面就是主控室了……" v="" />
</root>

我们要别k="xxx"里面文字翻译到v=""里面,我们把文字在网页上提交能得到正确的结果,但是如果这样调用

 string text = File.ReadAllText(langPath, Encoding.UTF8);
 text = ChineseConverter.GetWebString(text);

结果就是会在&amp;这个地方翻译就断掉了,后面的内容被舍弃掉了。
这个问题我还没想到非常好的方法,暂时的方式时先把&amp替换成其他字符,等翻译完成之后在替换回来。比如

private static void ConvertToTraditonal()
{
    string langPath = GameAssetPath.Lang + "/" + LangType.tc + "/" + AssetFixedName.Lang_Xml;
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(langPath);
    XmlElement root = xmlDoc.DocumentElement;
    XmlNodeList list = root.ChildNodes;
    string text = File.ReadAllText(langPath, Encoding.UTF8);
    text = text.Replace("&amp", "#####");
    text = ChineseConverter2.GetWebString(text);
    XmlDocument xmlDoc2 = new XmlDocument();
    string dst = text.Replace("\\\"", "\"").Replace("#####", "&amp").Replace("\\\\n", "\\n");
    xmlDoc2.LoadXml(dst);
    XmlElement root2 = xmlDoc2.DocumentElement;
    XmlNodeList list2 = root2.ChildNodes;
    for (int i = 0; i < list2.Count; i++)
    {
        string kAttr = list2[i].Attributes["k"].Value;
        list[i].Attributes["v"].Value = kAttr;
    }
    xmlDoc.Save(langPath);
}

需要提出的时这部分

string dst = text.Replace("\\\"", "\"").Replace("#####", "&amp").Replace("\\\\n", "\\n"); 

这里是因为结果


引号和换行符\n前面都会多加一个\号

langPath可以替换成你所在的xml的路径,运行完毕得出正确结果


perfect!over了
上一篇下一篇

猜你喜欢

热点阅读