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

时间:2021-12-07 07:12:42

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.

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

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?

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

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.

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

3 个解决方案

#1


2  

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.

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

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:

要记住的是.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);
        }
    }
}

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.

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

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.

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

Best of luck.

祝你好运。

#2


1  

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

我遇到了类似的问题,并意识到从excel文件创建.resx文件的最简单方法是使用excel的连接函数生成“<”数据“>”..“<”/ data“>”节点。 resx文件,然后在任何文本编辑器中手动将生成的行复制到.resx文件。因此,假设您在Excel文档的A列中有“名称”,在Excel文档的B列中有“值”。在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.

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

#3


0  

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

如果它在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!) }

#1


2  

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.

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

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:

要记住的是.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);
        }
    }
}

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.

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

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.

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

Best of luck.

祝你好运。

#2


1  

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

我遇到了类似的问题,并意识到从excel文件创建.resx文件的最简单方法是使用excel的连接函数生成“<”数据“>”..“<”/ data“>”节点。 resx文件,然后在任何文本编辑器中手动将生成的行复制到.resx文件。因此,假设您在Excel文档的A列中有“名称”,在Excel文档的B列中有“值”。在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.

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

#3


0  

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

如果它在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!) }