31

我正在尝试在 C# 类中使用 ResourceManager,但在创建 ResourceManager 类的新实例时不知道用什么替换基名。

我有一个单独的项目,其中包含我上面的项目引用的资源文件,名称如下:

  • 资源.resx
  • 资源.es-ES.resx

如果我有如下代码片段,当我想使用英语或西班牙语版本的资源时,我应该用什么代替基本名称?

ResourceManager RM = new ResourceManager(basename,Assembly.GetExecutingAssembly());

我尝试了 Tom 建议的方法,但遇到了臭名昭著的错误

找不到适合指定区域性或中性区域性的任何资源。

我的解决方案有两个项目,其中项目 YeagerTech 是一个 Web 应用程序,项目 YeagerTechResources 包含所有资源。Project YeagerTech 参考了 YeagerTechResources。

创建资源管理器时,我有以下语法:

ResourceManager RM = new ResourceManager("YeagerTechResources.Resources",
    Assembly.GetExecutingAssembly());

显然,这是不正确的。

YeagerTechResources 项目中的资源文件的 BuildAction 设置为 Embedded Resource。

我的资源文件的名称是:Resources.resx 和 Resources.es-ES.resx。

如果有人可以在实例化资源管理器时根据我的项目和资源文件名简单地告诉我要使用的确切语法,我将不胜感激......

我已经做了我能想到的一切来解决这个问题,但不能......

这是我最后一次尝试在这里解决它......

ResourceManager RM = new ResourceManager("YeagerTechResources.Resources", Assembly.GetExecutingAssembly());

sb.Append(RM.GetString("RegisterThanks"));

执行上述代码后出现以下错误:

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

我可以毫无问题地使用 HTML 标记中的资源,但是在控制器中使用 C# 代码时,我不断收到上述错误。

非常感谢根据我的项目对我需要的确切语法的任何帮助。

4

6 回答 6

54

通过字符串读取资源的方法非常简单:

ResourceNamespace.ResxFileName.ResourceManager.GetString("ResourceKey")

在不能使用“点表示法”的情况下(例如,当资源键保存在数据库中时),它是一种简洁而优雅的解决方案,用于通过键读取资源。

于 2015-04-22T08:57:15.713 回答
21

检查生成的 .resources 文件所需的字符串的快速而肮脏的方法。

您的 .resources 在资源项目obj/Debug目录中生成。(如果不是在解决方案资源管理器中右键单击 .resx 文件并点击“运行自定义工具”以生成 .resources 文件)

导航到此目录并查看文件名。您应该会看到一个以 XYZ.resources 结尾的文件。复制该文件名并删除尾随的 .resources ,这就是您应该加载的文件。

例如,在我的 obj/Bin 目录中,我有以下文件:

MyLocalisation.Properties.Resources.resources

如果资源文件在同一个类库/应用程序中,我将使用以下 C#

ResourceManager RM = new ResourceManager("MyLocalisation.Properties.Resources", Assembly.GetExecutingAssembly());

但是,听起来您正在使用来自单独的类库/应用程序的资源文件,您可能想要

Assembly localisationAssembly = Assembly.Load("MyLocalisation");
ResourceManager RM =  new ResourceManager("MyLocalisation.Properties.Resources", localisationAssembly);
于 2012-02-09T13:43:15.980 回答
11

在这种情况下,这个 SO 答案可能会有所帮助。
如果项目已经引用了资源项目,那么您可以在代码中显式使用生成的资源类,并ResourceManager从中访问它。因此,大致如下:

ResourceManager resMan = YeagerTechResources.Resources.ResourceManager;

// then, you could go on working with that
ResourceSet resourceSet = resMan.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
// ...
于 2013-08-22T09:00:15.210 回答
4

根据此处的 MSDN 文档,basename 参数指定“资源文件的根名称,不包括其扩展名,但包括任何完全限定的命名空间名称。例如,名为“MyApplication.MyResource.en-US”的资源文件的根名称。资源”是“MyApplication.MyResource”。

ResourceManager 将自动尝试检索当前 UI 文化的值。如果您想使用特定语言,则需要将当前 UI 文化设置为您希望使用的语言。

于 2012-01-11T19:55:11.337 回答
1

原则上它与@Landeeyos 的想法相同。无论如何,扩展该响应:聚会有点晚了,但这是我的两分钱:

设想:

我有一个独特的案例,即使用我的 WPF 应用程序添加一些(大约 28 个文本文件)预定义的模板文件。所以,这个想法是每次安装这个应用程序时,这些模板、文本文件都可以随时使用。无论如何,我所做的是通过添加resource.resx 来创建一个单独的库来保存文件。然后我将所有这些文件添加到这个资源文件中(如果你双击一个 .resx 文件,它的设计器会在 Visual Studio 中打开)。我已将访问修饰符设置为对所有人公开。此外,每个文件都通过每个文本文件的构建操作标记为嵌入式资源(您可以通过查看其属性来获得)。让我们称之为 bibliothek1.dll 我在另一个库(称之为 bibliothek2.dll)中引用了上面的库(bibliothek1.dll),然后在 mf wpf 应用程序中使用了第二个库。

真正的乐趣:

        // embedded resource file name <i>with out extension</i>(this is vital!) 

        string fileWithoutExt = Path.GetFileNameWithoutExtension(fileName);

        // is required in the next step

        // without specifying the culture
        string wildFile = IamAResourceFile.ResourceManager.GetString(fileWithoutExt);
        Console.Write(wildFile);

        // with culture
        string culturedFile = IamAResourceFile.ResourceManager.GetString(fileWithoutExt, CultureInfo.InvariantCulture);
        Console.Write(culturedFile);

示例: 结帐'testingresourcefilesusage'@ https://github.com/Natsikap/samples.git

我希望它可以帮助某人,某天,某处!

于 2018-10-24T09:11:48.480 回答
0

I went through a similar issue. If you consider your "YeagerTechResources.Resources", it means that your Resources.resx is at the root folder of your project.

Be careful to include the full path eg : "project\subfolder(s)\file[.resx]" to the ResourceManager constructor.

于 2016-04-18T09:33:35.120 回答