自動翻譯C#文檔註釋的小程序

在使用visual studio進行代碼編寫時,它會自動顯示代碼的文檔註釋

很多庫的文檔註釋是英文的,這對像我這樣的英文菜雞來說,降低了編程效率。今晚寫了個小程序,配合手工操作,實現了文檔註釋的翻譯,做個分享吧。

static void Export(string file)
{
    StreamWriter writer = new StreamWriter(file + ".txt");
    XmlDocument doc = new XmlDocument();
    doc.Load(file + ".xml");
    var members = doc.SelectSingleNode("doc/members");
    char[] splitor = { ' ' };
    Process = (node) => {
        bool isFirstLine = true;
        foreach (var line in node.Value.Split(splitor, StringSplitOptions.RemoveEmptyEntries))
        {
            if (string.IsNullOrWhiteSpace(line)) continue;
            if (isFirstLine)
                isFirstLine = false;
            else
                writer.Write(' ');
            writer.Write(line.Trim());
        }
        writer.WriteLine();
    };
    EachElement(members);
    writer.Close();
}
static  Action<XmlNode> Process;
static void EachElement(XmlNode node)
{
    if (node.NodeType == XmlNodeType.Text)
    {
        Process(node);
    }
    else
    {
        foreach (XmlNode item in node.ChildNodes)
        {
            EachElement(item);
        }
    }
}

static void Import(string file)
{
    StreamReader txt = new StreamReader(file + ".txt");
    XmlDocument doc = new XmlDocument();
    doc.Load(file + ".xml");
    var members = doc.SelectSingleNode("doc/members");
    Process = (node) => {
        node.Value = txt.ReadLine();
    };
    EachElement(members);
    doc.Save(file + "2.xml");
}

比如我有一個MathNet.Numerics.xml的註釋文件,那我調用Export("MathNet.Spatial")將會在該目錄下生成MathNet.Spatial.txt文件。

將MathNet.Spatial.txt用Chrome瀏覽器打開

右鍵,翻譯成中文。把中文註釋複製出來,覆蓋掉原來MathNet.Spatial.txt中的內容。然後調用Import("MathNet.Spatial"),該操作會在同級目錄下創建MathNet.Spatial2.xml,它就是我們需要的中文註釋。

接下來怎麼做,你懂得!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章