自动翻译C#文档注释的小程序

本文分享了在VisualStudio中利用自定义程序实现代码文档注释的翻译方法,通过导出、在线翻译及重新导入,有效提升英文注释的阅读效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用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,它就是我们需要的中文注释。

接下来怎么做,你懂得!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值