【问题标题】:C# Localization and Resource FilesC# 本地化和资源文件
【发布时间】:2015-12-19 14:40:51
【问题描述】:

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

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();
    }
}

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

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

【问题讨论】:

    标签: c# localization resources


    【解决方案1】:

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

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

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

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

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

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

    注意:

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

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

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

    【讨论】:

    • 我更改了这一行,使其包含 new ResourceManager("myApp.ui.resx", typeof(myForm).Assembly);它仍然出错。
    • @dan 从第一个参数中删除 .resx 并传递 "MyApp.ui"
    • @Dan 让我知道它是否解决了问题。
    • 我能再问一个相关的问题...您如何生成文件?我已经尝试了我能想到的一切。创建新的 resx 文件等,但我无法正确引用本地化文件。是否有一些在线演练的地方?
    • @Brandon 使用“添加新项目”窗口添加资源文件,然后在设计器中编辑这些文件,例如添加 strings.resx,然后对于您想要支持的其他文化,添加 strings.es.resx、字符串。 fr.resx,strings.fa.resx,...你也可以看看How to: Create a Localized Version of a Resource File希望答案对你有帮助:)
    猜你喜欢
    • 2011-09-27
    • 1970-01-01
    • 2012-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-10
    • 2012-09-20
    • 1970-01-01
    相关资源
    最近更新 更多