C# 使用资源管理器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8825433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:39:23  来源:igfitidea点击:

Using ResourceManager

c#localizationresourcemanager

提问by sagesky36

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.

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

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
  • 资源.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?

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

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

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.

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

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.

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

I have the following syntax when creating my Resource Manager:

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

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

Obviously, it's incorrect.

显然,这是不正确的。

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

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

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

我的资源文件的名称是:Resources.resx 和 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:

执行上面的代码后,我收到以下错误:

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.

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

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.

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

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

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

回答by Tom Lint

According to the MSDN documentation here, The basename argument specifies "The root name of the resource file without its extension but including any fully qualified namespace name. For example, the root name for the resource file named "MyApplication.MyResource.en-US.resources" is "MyApplication.MyResource"."

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

The ResourceManager will automatically try to retrieve the values for the current UI culture. If you want to use a specific language, you'll need to set the current UI culture to the language you wish to use.

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

回答by SinOB

The quick and dirty way to check what string you need it to look at the generated .resources files.

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

Your .resources are generated in the resources projects obj/Debugdirectory. (if not right click on .resx file in solution explorer and hit 'Run Custom Tool' to generate the .resources files)

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

Navigate to this directory and have a look at the filenames. You should see a file ending in XYZ.resources. Copy that filename and remove the trailing .resources and that is the file you should be loading.

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

For example in my obj/Bin directory I have the file:

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

MyLocalisation.Properties.Resources.resources

MyLocalisation.Properties.Resources.resources

If the resource files are in the same Class library/Application I would use the following C#

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

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

However, as it sounds like you are using the resources file from a separate Class library/Application you probably want

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

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

回答by superjos

This SO answermight help in this case.
If the mainproject already references the resourceproject, then you could just explicitly work with your generated-resource class in your code, and access its ResourceManagerfrom that. Hence, something along the lines of:

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

ResourceManager resMan = YeagerTechResources.Resources.ResourceManager;

// then, you could go on working with that
ResourceSet resourceSet = resMan.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
// ...

回答by Landeeyo

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

对于无法使用“点符号”的键读取资源(例如,当资源键保留在数据库中时),这是一种干净而优雅的解决方案。

回答by leguminator

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.

我遇到了类似的问题。如果您考虑您的“YeagerTechResources.Resources”,则意味着您的 Resources.resx 位于项目的根文件夹中。

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

小心包含完整路径,例如:“project\subfolder(s)\file[.resx]”到 ResourceManager 构造函数。

回答by Ada_Spark

in priciple it's the same idea as @Landeeyos. anyhow, expanding on that response: a bit late to the party but here are my two cents:

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

scenario:

设想:

I have a unique case of adding some (roughly 28 text files) predefined, template files with my WPF application. So, the idea is that everytime this app is to be installed, these template, text files will be readily available for usage. anyhow, what I did was that made a seperate library to hold the files by adding a resource.resx. Then I added all those files to this resource file (if you double click a .resx file, its designer gets opened in visual studio). I had set the Access Modifier to public for all. Also, each file was marked as an embedded resource via the Build Action of each text file (you can get that by looking at its properties). let's call this bibliothek1.dll i referenced this above library (bibliothek1.dll) in another library (call it bibliothek2.dll) and then consumed this second library in mf wpf app.

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

actual fun:

实际乐趣:

        // 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);

sample: checkout 'testingresourcefilesusage' @ https://github.com/Natsikap/samples.git

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

I hope it helps someone, some day, somewhere!

我希望它可以帮助某人,有一天,在某个地方!