动态初始化的ResourceManager [英] Initialize ResourceManager dynamically

查看:197
本文介绍了动态初始化的ResourceManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的功能,它工作正常,从这个所谓的 OkayMessages 的特定的资源文件得到翻译值。

I have this function and it works fine to get a translated value from this specific resource file called OkayMessages.

public static string GetResourceString(string resourceKey){
  ResourceManager resourceManager = Resources.OkayMessages.ResourceManager;
  return resourceManager.GetString(resourceKey);
}

但我有1个多资源文件,我想这个功能,从这些文件中获取值以及..只是,我在使用动态/程控选择正确的资源(经理)的麻烦。 我曾尝试使用下面的code,还有一些变体,但我总是得到一个错误。

But i have more than 1 resource file and i want this function to get values from those files as well.. Only, i'm having trouble with dynamically/programmatically selecting the right resource(manager). I have tried to use the code below, and some variants to that, but i always get an error.

public static string GetResourceString(string resourceFile, string resourceKey){
  ResourceManager resourceManager = new System.Resources.ResourceManager("Resources." + resourceFile, Assembly.GetExecutingAssembly());
  return resourceManager.GetString(resourceKey);
}

我得到了大部分的时间错误是:找不到任何资源适合指定区域性或中性文化。确保Resources.OkayMessages.resources正确​​嵌入或链接到程序集。

The error i got most of the times was: Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "Resources.OkayMessages.resources" was correctly embedded or linked into assembly..

更新:我使用的是\ App_GlobalResources文件\文件夹我的资源文件,似乎这就是问题所在。当我把我的项目的根目录下的资源文件,我可以初始化一个ResourceManager没有问题。

Update: I'm using the \App_GlobalResources\ folder for my resource files, and it seems that this is the problem. When i place a resource file in the root of my project, i can initialize a ResourceManager without problems.

推荐答案

搜索在错误的方向一段时间后,我发现最简单的回答了这个问题。事实证明,有一个叫方法 GetGlobalResourceObject

After searching in the wrong direction for a while, I just found the most simple answer to this problem. It turns out that there is a method called GetGlobalResourceObject.

所以,在我的情况,我现在用的这行code这确实所有:

So in my case I'm now using this line of code which does all:

GetGlobalResourceObject("OkayMessages", "PasswordChanged").ToString();

这篇关于动态初始化的ResourceManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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