C#、本地化、资源和 MonoDevelop
我的问题是这样的:使用 MonoDevelop(这是我正在从事的许多项目的首选环境),我试图弄清楚如何将资源文件用于本地化消息以及如何正确使用将它们作为嵌入式资源包含在项目中。
我的目标是拥有一个资源文件,其中包含用于消息键及其值的简单名称-值对,并为其本地化字符串提供单独的文件,例如
Messages.resources
Hello.World = Hello World
Goodbye.Cruel.World = Goodbye, Cruel World
Messages.de.resources
Hello.World = Hallo Welt
Goodbye.Cruel.World = Auf Wiedersehen, grausame Welt
我有几个问题。
首先,MonoDevelop 的 .resources 文件和 Visual Studio 的资源概念之间有什么区别(如果有的话)。据我了解,MonoDevelop(和 SharpDevelop)允许您创建 .resources 文件,而 Visual Studio 利用 .resx 文件并通过 resgen 实用程序将它们编译为 .resources 文件(二进制文件类型)。在 MonoDevelop 中使用资源时,我需要使用 resgen 编译我的资源(例如 Messages.resources)吗?当我尝试仅使用 MonoDevelop 允许我通过其向导创建的直接 .resources 文件时,我收到以下错误:
“Stream 不是有效的资源文件。”
第二,一旦我有了适当生成的资源文件,我就可以将它们嵌入到我的项目中,如果我理解正确的话,将使资源成为程序集的一部分。如果我有两个文件,Messages.resources 和 Messages.de.resources,当我嵌入它们时,MonoDevelop(至少)会为它们分配相同的 ID 值。我是否需要将默认本地化包含在项目中,然后为每个受支持的区域设置一个单独的项目?接下来,C# 如何区分我的 Messages.resources 和 Messages.de.resources 文件(或它们是什么文件)?
我目前正在尝试使用以下代码解析我的消息资源:
...
public string Translate(string messageKey, CultureInfo cultureInfo) {
ResourceManager resourceManager = new ResourceManager("My.Project.Messages", Assembly.GetExecutingAssembly());
string message = resourceManager.GetString(messageKey, cultureInfo);
return message;
}
...
我觉得我在使用 C# 进行国际化/本地化/全球化等工作时缺少一些基本点。我以前曾用 Java 从事过国际化项目,但由于某种原因,我无法完全理解 C# 中的国际化项目。
另外,顺便说一句——国际化项目中资源的“首选”目录结构是什么?
My problem is this: Using MonoDevelop (which is the preferred environment for a number of projects I'm working on) I'm trying to figure out how to use resource files for localized messages and how to properly include them in the project as an embedded resource.
My goal is to have a resources file with simple name-value pairs for message keys and their values, and have separate files for their localized strings e.g.
Messages.resources
Hello.World = Hello World
Goodbye.Cruel.World = Goodbye, Cruel World
Messages.de.resources
Hello.World = Hallo Welt
Goodbye.Cruel.World = Auf Wiedersehen, grausame Welt
I'm having a couple of issues.
First, what is the difference (if any) between MonoDevelop's .resources file and Visual Studio's concept of resources. From what I understand, MonoDevelop (and SharpDevelop) allow you to create .resources files, whereas Visual Studio utilizes .resx files and compiles them into .resources files (a binary file type) through the resgen utility. When using resources in MonoDevelop do I need to compile my resources (e.g. Messages.resources) using resgen? When I try to use just the straight .resources files that MonoDevelop allows me to create through their wizard I get the following error:
"Stream is not a valid resource file."
Second, once I have an appropriately generated resource file, I can embed them to my project, which if I understand it correctly, makes the resources a part of the assembly. If I have two files though, Messages.resources and Messages.de.resources, MonoDevelop (at least) assigns them the same ID value when I embed them. Do I need to have my default localization included in the project and then a separate project for each supported locale? Following up on this, how does C# distinguish between my Messages.resources and Messages.de.resources files (or whatever files they are)?
I'm currently trying to resolve my message resources with the following code:
...
public string Translate(string messageKey, CultureInfo cultureInfo) {
ResourceManager resourceManager = new ResourceManager("My.Project.Messages", Assembly.GetExecutingAssembly());
string message = resourceManager.GetString(messageKey, cultureInfo);
return message;
}
...
I feel like I'm missing some fundamental points in the effort of internationalization/localization/globalization etc. with C#. I have worked on internationalized projects in Java before, but for some reason I can't quite wrap my head around it in C#.
Also, as an aside--what is the "preferred" directory structure for resources in an internationalized project?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不熟悉 .NET 本地化(我使用 gettext),但据我了解,.resources 文件是 二进制格式,实际上嵌入到您的 dll 中。您可以编译 resx (XML)或 文本使用 resgen 将资源转换为二进制格式。文本文件更具可读性,但只能用于字符串资源。 XML 更详细,但可以表示二进制资源可以表示的所有内容。
通常的做法是将资源以 .resx 形式存储在项目中,MonoDevelop 在构建项目时会自动将它们编译为 .resources 文件(您必须手动编译 .txt 文件)。不幸的是,MD 没有针对 resx 文件的特殊编辑工具,因此您必须直接编辑 XML。
MD 确实有很好的 gettext 本地化工具,但 Windows 目前不支持这些工具。
I'm not familiar with .NET localization (I use gettext), but as I understand it, .resources files are a binary format that's actually embedded into your dll. You can compile resx (XML) or text resources into the binary format using resgen. Text files are more readable but can only be used for string resources. XML is more verbose but can represent everything that binary resources can.
The usual thing is to store your resources in .resx form in the project, and MonoDevelop will automatically compile them into .resources files when building your project (you'd have to compile .txt files manually). Unfortunately MD doesn't have special editing tools for resx files, so you'd have to edit the XML directly.
MD does have nice localization tools for gettext, but these aren't currently supported on Windows.
有没有不使用 Gettext 的原因?
(例如,你想与 VS 兼容?如果不是,这对我有用:http://monodevelop。 com/Documentation/Localizing_Applications)
is there a reason of not using Gettext?
(E. g. you want to be compatible to VS? If not, this worked for me: http://monodevelop.com/Documentation/Localizing_Applications)