C#本地化和资源文件 [英] C# Localization and Resource Files

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

问题描述

我正在尝试使本地化在我的应用程序中正常工作.我的解决方案中有我的ui资源文件,分别名为"ui.resx"和"ui.de.resx".但是,我的实现中有错误之处,我感到很困惑.

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."

推荐答案

您应将类的全名(带有名称空间)用作第一个参数lik:

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"

Pay attention that you should not pass "MyApp.ui.resx", instead pass "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#本地化和资源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆