asp.net - 将 CSV 文件或 Excel 电子表格转换为 RESX 文件

标签 asp.net excel csv globalization

我正在寻找针对我遇到的问题的解决方案或建议。我有一堆需要本地化的 ASPX 页面,还有一堆需要支持 6 种语言的文本。

进行翻译的人员将无法访问 Visual Studio,最简单的工具可能是 Excel。如果我们使用 Excel 甚至导出到 CSV,我们需要能够导入以移动到 .resx 文件。那么,最好的方法是什么?

我知道这个问题,Convert a Visual Studio resource file to a text file?已经使用 Resx Editor,但更简单的解决方案将是首选。

最佳答案

我不确定您正在寻找的答案有多全面,但如果您真的只是使用 [string, string] 对进行本地化,并且您只是在寻找一种快速加载资源的方法( .resx)文件与您的翻译结果,那么以下将作为一个相当快速、低技术的解决方案。

要记住的是,.resx 文件只是 XML 文档,因此应该可以从外部代码手动将数据加载到资源中。以下示例在 VS2005 和 VS2008 中对我有用:

namespace SampleResourceImport
{
    class Program
    {
        static void Main(string[] args)
        {

            XmlDocument doc = new XmlDocument();
            string filePath = @"[file path to your resx file]";
            doc.Load(filePath);
            XmlElement root = doc.DocumentElement;

            XmlElement datum = null;
            XmlElement value = null;
            XmlAttribute datumName = null;
            XmlAttribute datumSpace = doc.CreateAttribute("xml:space");
            datumSpace.Value = "preserve";

            // The following mocks the actual retrieval of your localized text
            // from a CSV or ?? document...
            // CSV parsers are common enough that it shouldn't be too difficult
            // to find one if that's the direction you go.
            Dictionary<string, string> d = new Dictionary<string, string>();
            d.Add("Label1", "First Name");
            d.Add("Label2", "Last Name");
            d.Add("Label3", "Date of Birth");

            foreach (KeyValuePair<string, string> pair in d)
            {
                datum = doc.CreateElement("data");
                datumName = doc.CreateAttribute("name");
                datumName.Value = pair.Key;
                value = doc.CreateElement("value");
                value.InnerText = pair.Value;

                datum.Attributes.Append(datumName);
                datum.Attributes.Append(datumSpace);
                datum.AppendChild(value);
                root.AppendChild(datum);
            }

            doc.Save(filePath);
        }
    }
}

显然,前面的方法不会为您的资源生成代码隐藏,但是在 Visual Studio 中打开资源文件并切换资源的辅助功能修饰符将为您(重新)生成静态属性。

如果您正在寻找完全基于 XML 的解决方案(相对于 CSV 或 Excel 互操作),您还可以指示翻译人员将翻译内容存储在 Excel 中,另存为 XML,然后使用 XPath 检索您的本地化信息。唯一需要注意的是文件大小往往会变得相当臃肿。

祝你好运。

关于asp.net - 将 CSV 文件或 Excel 电子表格转换为 RESX 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/223533/

相关文章:

php - 使用 SQL 查询在 PHP 中生成二维数组

c# - 如何使用 Entity Framework 构建具有 "Core"数据库和各个派生数据库的 ASP.NET MVC 应用程序?

c# - 在 ASP.NET MVC (C#) 上提供静态文件

excel - ColdFusion XLS "Export"和字符编码

Excel 2010 在 IF 函数中搜索文本 - 单独的单元格数据

csv - Laravel 5.2 League CSV 输出在末尾添加带有数字的额外行

c# - 如何检查用户是 "logged in"?

javascript - 将 jQuery 函数应用于同一类 <ul>

excel - VBA - 选择图表A的系列1时使图表B出现

python - 将 csv 数据绘制为 map - Python