将 CSV 文件或 Excel 电子表格转换为 RESX 文件

发布于 2024-07-06 21:56:01 字数 375 浏览 11 评论 0原文

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

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

我知道这个问题,转换 Visual Studio 资源文件到文本文件? 已经并且使用 Resx Editor,但更容易的解决方案将是首选。

I am looking for a solution or recommendation to a problem I am having. I have a bunch of ASPX pages that will be localized and have a bunch of text that needs to be supported in 6 languages.

The people doing the translation will not have access to Visual Studio and the likely easiest tool is Excel. If we use Excel or even export to CSV, we need to be able to import to move to .resx files. So, what is the best method for this?

I am aware of this question, Convert a Visual Studio resource file to a text file? already and the use of Resx Editor but an easier solution would be preferred.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

︶ ̄淡然 2024-07-13 21:56:01

我不确定您正在寻找的答案有多全面,但如果您真的只是使用 [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 检索您的本地化信息。 唯一需要注意的是文件大小往往会变得相当臃肿。

祝你好运。

I'm not sure how comprehensive an answer you're looking for, but if you're really just using [string, string] pairs for your localization, and you're just looking for a quick way to load resource (.resx) files with the results of your translations, then the following will work as a fairly quick, low-tech solution.

The thing to remember is that .resx files are just XML documents, so it should be possible to manually load your data into the resource from an external piece of code. The following example worked for me in VS2005 and 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);
        }
    }
}

Obviously, the preceding method won't generate the code-behind for your resource, however opening the resource file in Visual Studio and toggling the accessibility modifier for the resource will (re)generate the static properties for you.

If you're looking for a completely XML-based solution (vs. CSV or Excel interop), you could also instruct your translators to store their translated content in Excel, saved as XML, then use XPath to retrieve your localization info. The only caveat being the file sizes tend to become pretty bloated.

Best of luck.

执妄 2024-07-13 21:56:01

我遇到了类似的问题,并意识到从 excel 文件创建 .resx 文件的最简单方法是使用 excel 的串联函数生成“<”data”>”..”<”/data”>” .resx 文件的节点,然后在任何文本编辑器中手动将生成的行复制到 .resx 文件中。 假设您在 Excel 文档的 A 列中有“名称”,在 Excel 文档的 B 列中有“值”。 使用 C 列中的以下公式,

=CONCATENATE("<data name=","""",A14,""" xml:space=""preserve"">","<value>", B14, "</value>", "</data>")

您将获得资源的数据节点。 然后,您可以将此公式复制到所有行,然后复制 .resx 文件中 C 列的内容。

I ran into similar problem and realized that the simplest way to create a .resx file from excel file is using a concatenate function of excel to generate "<"data">".."<"/data">" node for the .resx file and then manually copying the generated rows to the .resx file in any text editor. So lets say that you have "Name" in column A of an excel document and "value" in Column B of the excel document. Using following formula in Column C

=CONCATENATE("<data name=","""",A14,""" xml:space=""preserve"">","<value>", B14, "</value>", "</data>")

you will get the data node for resource. You can then copy this formula to all the rows and then copy the contents of Column C in your .resx file.

暖树树初阳… 2024-07-13 21:56:01

如果是 csv 格式,这里有一个快速的 Ruby 脚本来生成数据元素。

require 'csv'
require 'builder'

file = ARGV[0]

builder = Builder::XmlMarkup.new(:indent => 2)

CSV.foreach(file) do |row|
  builder.data(:name => row[0], "xml:space" => :preserve) {|d| d.value(row[1]) }
end

File.open(file + ".xml", 'w') { |f| f.write(builder.target!) }

If it's in csv here's a quick Ruby script to generate the data elements.

require 'csv'
require 'builder'

file = ARGV[0]

builder = Builder::XmlMarkup.new(:indent => 2)

CSV.foreach(file) do |row|
  builder.data(:name => row[0], "xml:space" => :preserve) {|d| d.value(row[1]) }
end

File.open(file + ".xml", 'w') { |f| f.write(builder.target!) }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文