问题描述
我正在寻找一个解决方案或建议我遇到的问题。我有一堆ASPX页面将被本地化,并且需要支持6种语言的一堆文本。
正在翻译的人将无法访问到Visual Studio,可能最简单的工具是Excel。如果我们使用Excel或甚至导出到CSV,我们需要导入以移动到.resx文件。那么,最好的方法是什么?
我知道这个问题,,并且使用Resx编辑器,但更容易的解决方案将是首选。
我不知道你在寻找多么全面的答案,但如果你真的只是使用[string ,字符串]对,您只需要一个快速的方式来加载资源(.resx)文件与您的翻译结果,那么以下将作为一个相当快速,低技术的解决方案。 p>
要记住的是,.resx文件只是XML文档,所以应该可以从外部代码手动将数据加载到资源中。以下示例在VS2005和VS2008中适用于我:
命名空间SampleResourceImport
{
class Program
{
static void Main(string [] args)
{
XmlDocument doc = new XmlDocument();
string filePath = @[文件路径到你的resx文件];
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;
//以下模拟实际检索本地化文本
//从CSV或?文件...
// CSV解析器是常见的,它不应该太困难
//找到一个如果这是你的方向。
字典< string,string> d = new Dictionary< string,string>();
d.Add(Label1,First Name);
d.Add(Label2,Last Name);
d.Add(Label3,出生日期);
foreach(KeyValuePair< string,string>在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检索您的本地化信息。文件大小的唯一注意事项往往会变得很blo肿。
最好的运气。
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.
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.
这篇关于将CSV文件或Excel电子表格转换为RESX文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!