我知道有一些工具可以将文本文件获取到 Visual Studio 的资源文件。但我想将我的资源文件中的文本转换为文本文件,以便翻译它们。还是有更好的方法来做到这一点?
7 回答
您可以使用Resx Editor,一个面向翻译的小型文件编辑器。
- 目标受众:译者。
- 支持的文件格式:Microsoft RESX 2.0
这是 Joannès Vermoel(免费工具的作者)关于它的博客条目的链接。
最后,我只是使用了一个快速破解:
public class Export
{
public string Run()
{
var resources = new StringBuilder();
var assembly = Assembly.GetExecutingAssembly();
var types = from t in assembly.GetTypes()
where t != typeof(Export)
select t;
foreach (Type t in types)
{
resources.AppendLine(t.Name);
resources.AppendLine("Key, Value");
var props = from p in t.GetProperties()
where !p.CanWrite && p.Name != "ResourceManager"
select p;
foreach (PropertyInfo p in props)
{
resources.AppendFormat("\"{0}\",\"{1}\"\n", p.Name, p.GetValue(null));
}
resources.AppendLine();
}
return resources.ToString();
}
}
将此代码添加到包含 your.resx 文件的项目中(我的位于单独的“语言”项目中),然后使用以下代码将结果保存到 .csv 中,以便可以使用电子表格编辑器加载它。
var hack = new Languages.Export();
var resourcesSummary = hack.Run();
var cultureName = System.Threading.Thread.CurrentThread.CurrentCulture.Name;
using (TextWriter file = File.CreateText(@"C:\resources." + cultureName + ".csv"))
{
file.Write(resourcesSummary);
}
这不允许您将文件从 .csv 导入回您的 .resx 文件,以便对其进行编译。有一个可以做到这一点的实用程序会很好。
您可以使用Simple Resx Editor,它有一些有趣的功能可以帮助您进入翻译过程。
尽管它是反直觉的,但翻译 exe 而不是资源文件是一个更好的主意。在这里阅读原因:
您可能想看看Excel Resource Transfer。它是 Microsoft Excel 的插件,用于从资源文件中导入和导出文本。有试用版。完整版售价 25 欧元。
如果您正在为 Web 项目执行此操作,则进行国际化(包括翻译)的更好方法是使用i18n nuget 包。不仅可以更好地使用模板,而且还具有其他优点,例如本地化 URL。
这是来自 github 存储库的示例:
<div id="content">
<h2>[[[Welcome to my web app!]]]</h2>
<h3><span>[[[Amazing slogan here]]]</span></h3>
<p>[[[Ad copy that would make Hiten Shah fall off his chair!]]]</p>
<span class="button" title="[[[Click to see plans and pricing]]]">
<a href="@Url.Action("Plans", "Home", new { area = "" })">
<strong>[[[SEE PLANS & PRICING]]]</strong>
<span>[[[Free unicorn with all plans!]]]</span>
</a>
</span>
</div>
运行后期构建任务会生成一个 PO 数据库,该数据库可以提供给使用 PO 编辑工具(如POEdit)的翻译人员来提供特定于区域设置的文本。
您可以使用 Microsoft 的 winres.exe,它可以让您本地化 Windows 窗体,而无需使用 Visual Studio。它不会将资源保存到文本文件中,但其想法是每种文化的本地化专家都可以使用该工具生成应用程序的本地化版本。
这是一个更好的解释: http: //msdn.microsoft.com/en-us/library/8bxdx003 (VS.80).aspx