动态初始化的ResourceManager

编程入门 行业动态 更新时间:2024-10-26 16:29:46
本文介绍了动态初始化的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

本文发布于:2023-11-22 23:42:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619360.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:初始化   动态   ResourceManager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!