使用资源管理器

Using ResourceManager(使用资源管理器)

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

问题描述

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

I'm trying to use the ResourceManager in a C# class, but don't know what to substitute for the basename when creating a new instance of the ResourceManager class.

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

I have a separate project that contains the resource files which is referenced by my project above named as the following:

  • Resources.resx
  • Resources.es-ES.resx

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

If I have a code snippet as follows, what should I substitute for the basename when I want to use the English or Spanish version of the resources?

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

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

I tried the approach suggested by Tom, but am getting the infamous error

找不到任何适合指定文化或中立文化的资源.

Could not find any resources appropriate for the specified culture or the neutral culture.

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

My solution has two projects where project YeagerTech is a web application and project YeagerTechResources contains all the resources. Project YeagerTech has a reference to YeagerTechResources.

我在创建资源管理器时使用以下语法:

I have the following syntax when creating my Resource Manager:

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

显然,这是不正确的.

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

The resource files in the YeagerTechResources project have their BuildAction set to Embedded Resource.

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

The name of my resource files are: Resources.resx and Resources.es-ES.resx.

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

If someone can simply tell me the exact syntax to use based on my project and resource file names when instantiating the resource manager, I would greatly appreciate it...

我已尽我所能解决此问题,但无法....

I've done everything I can think of to resolve this and can't....

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

This is my last attempt to resolve it here...

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

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

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

I am getting the following error after executing the code above:

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

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

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

I am able to use the resources in the HTML markup with absolutely no issues, but when coming to the C# code in the Controller, I keep on getting the above error.

任何关于我需要的基于我的项目的确切语法的帮助将不胜感激.

Any help for the exact syntax I need based on my projects would be greatly appreciated.

推荐答案

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

There's surprisingly simple way of reading resource by string:

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

在不能使用点符号"的情况下(例如,当资源键被持久保存在数据库中时),它是一种简洁而优雅的解决方案.

It's clean and elegant solution for reading resources by keys where "dot notation" cannot be used (for instance when resource key is persisted in the database).

这篇关于使用资源管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用资源管理器

基础教程推荐