本地化和资源文件

本地化和资源文件

本文介绍了C# 本地化和资源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的应用程序中进行本地化.我的解决方案中有名为ui.resx"和ui.de.resx"的 ui 资源文件.但是,我的实现中有些地方是不正确的,我很难过.

I'm trying to get localization working in my application. I have my ui resource files, named "ui.resx" and "ui.de.resx" in my solution. However, something in my implementation is incorrect, and I'm stumped.

ResourceManager res_man;
CultureInfo culture;
string exception;

private void myForm_Load(object sender, EventArgs e)
{
    culture = CultureInfo.CreateSpecificCulture("de");
    res_man = new ResourceManager("MyApp.ui.resx", typeof(myForm).Assembly);
    doTheThing();
}

private void doTheThing()
{
    try
    {
        BTN_save.text = ui.SAVE;
        //Do code here
    }
    catch(Exception e)
    {
        exception = e.toString();
    }
}

当我运行程序时,它会出错并显示异常:

When I run the program, it errors out and exception reads:

异常:System.Resources.MissingManifestResourceException:找不到适合指定区域性或中性区域性的任何资源.确保ui.resx.resources"在编译时正确嵌入或链接到程序集myProgram"中,或者所需的所有附属程序集都是可加载的且完全签名的."

"Exception: System.Resources.MissingManifestResourceException: Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "ui.resx.resources" was correctly embedded or linked into assembly "myProgram" at compile time, or that all the satellite assemblies required are loadable and fully signed."

推荐答案

你应该使用你的类的全名(带命名空间)作为第一个参数,比如:

You should use the full name (with namespace) of your class as first parameter lik:

var resman= new ResourceManager("Sample.Resources", typeof(Resources).Assembly);

要知道应该使用什么名称,请打开 ui.resx 节点下的 ui.cs 并查看类的命名空间和类名并使用它们如上图.

To know what name you should use, open ui.cs under the ui.resx node and look at the namespace of the class and the class name and use them as shown above.

注意不要传"MyApp.ui.resx",而是传"MyApp.ui"

然后您可以简单地使用管理器来获取特定文化的资源,如下所示:

Then you can simply use the manager to get a resource for specific culture like this:

var str = resman.GetString("YourResourceKey", culture);

注意:

您应该注意的另一件事是,阅读特定文化资源的方法更简单.您可以简单地设置:

Another thing you should notice, there is more simple way to read culture specific resources. You can simply set :

var culture= ...

System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
System.Threading.Thread.CurrentThread.CurrentCulture = culture;

设置文化后,无论您在何处使用,例如 MyApp.Ui.Key,都将使用特定于该文化的 Key 的值.

After setting culture, wherever you use for example MyApp.Ui.Key the value of the Key specific to that culture will be used.

这篇关于C# 本地化和资源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 11:17