使用 SharpDevelop 进行本地化
我正在开发一个在 SharpDevelop 中开发的本地化应用程序。 基于 教程 我遇到了一个错误:
在磁盘上找不到任何适合指定区域性(或中性区域性)的资源。 基本名称:
我使用 Project | 创建了资源文件 添加 | 新项目相应的“空资源文件”(我找不到“程序集资源文件”)。 此外,我已将“构建操作”设置为“EmbeddedResource”。
在程序中,我有一个类执行以下
// load the resource file once
resourceManager = ResourceManager.CreateFileBasedResourceManager(file, filePath, null);
// depending on user selection, I set the CultureInfo in the code behind the form, i.e.
Thread.CurrentThread.CurrentUICulture = locales[0]; // which contains "en"
// and that's the method of the same class used to lookup the values
public string getValue ( string key )
{
return resourceManager.GetString(key);
}
SharpDevelop 3.0.0.3437 .NET Version 2.0.50727.3053
即使仔细查看评论并谷歌搜索很长一段时间后,我也找不到示例或解决方案。 因此,问题是:
- 如何将区域性添加到资源文件本身?
- 你能提供一个例子或链接吗?
非常感谢!
I'm working on a localized application which I develop in SharpDevelop. Based on a tutorial I ran into an error:
Could not find any resources appropriate for the specified culture (or the neutral culture) on disk. baseName:
I created the ressource file using Project | Add | New item respective "Empty ressource file" (I couldn't find "Assembly Resource file"). Furthermore I've set the "Build action" to "EmbeddedResource".
Within the program I have a class which does the following
// load the resource file once
resourceManager = ResourceManager.CreateFileBasedResourceManager(file, filePath, null);
// depending on user selection, I set the CultureInfo in the code behind the form, i.e.
Thread.CurrentThread.CurrentUICulture = locales[0]; // which contains "en"
// and that's the method of the same class used to lookup the values
public string getValue ( string key )
{
return resourceManager.GetString(key);
}
SharpDevelop 3.0.0.3437
.NET Version 2.0.50727.3053
Even after carefully going through the comments and googling for a long while, I couldn't find an example or a solution. Hence the questions:
- How can I add the culture to the ressource file itself?
- Can you provide an example or link?
Many thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CreateFileBasedResourceManager 将仅处理二进制 .resources 文件(而不是 .resx 文件)。
如果您坚持从外部文件加载资源而不是将资源嵌入到程序集中,
您将需要运行 resgen 命令行实用程序来生成 .resources 文件。
因此,如果您还没有这样做,请为每种受支持的文化生成一个。
否则看起来还不错。
但是,如果您希望将资源嵌入到程序集中,请在属性窗口中将“构建操作”设置为“嵌入资源”。
如果您还想要生成一个强类型类来处理所有脏工作,
将仅基本文件(不变文件)上的自定义工具设置为 ResXFileCodeGenerator,
或 PublicResXFileCodeGenerator 如果您需要的话(创建附属程序集时很好)。
那么您就不需要自己负责加载本地化资源。
生成的类会为您完成所有这些工作。
对于资源中的每个键,都会在生成的类中生成一个静态属性,您只需调用该属性,
它非常干净,也是迄今为止最简单的方法。
除非有特殊原因需要从 .resources 文件加载资源,
我总是建议将它们嵌入到程序集中。
CreateFileBasedResourceManager will only deal with binary .resources files (not .resx files).
If you insist on loading resources from external file rather than embed the resources into the assembly,
you will need to run the resgen command line utility to generate a .resources file.
So if you haven't done that, generate one for each supported culture.
Otherwise it looks OK.
But if you would rather embed resources into assembly, in the properties window, set the Build Action to Embedded Resource.
If you want also want a strongly typed class generated taking care of all the dirty work,
set the Custom Tool on the base file only (the invariant one) to ResXFileCodeGenerator,
or PublicResXFileCodeGenerator if you need that (good when creating satellite assemblies).
Then you don't need to take care of loading the localized resources yourself.
The generated class does all this for you.
For each key in the resource, a static property is generated in the generated class, and you'll just invoke this property,
it is very clean and by far the easiest way.
Unless there are special reasons for loading resources from .resources files,
i would always recommend embedding them into the (an) assembly.
我不确定您使用的 .NET 版本是什么,但这篇 MSDN 文章 运行时如何定位程序集可能对您有用。
I'm not sure what version of .NET you are using but this MSDN article on how the runtime locates assemblies may be of service to you.