gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:33:09 24 4
gpt4 key购买 nike

我正在寻找针对我遇到的问题的解决方案或建议。我有一堆需要本地化的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com