动态加载资源并读取 CurrentUICulture 的正确值
我正在创建一种将枚举转换为友好字符串的方法。友好名称存储在资源文件中并受全球化影响。因此,我创建了两个资源文件:Enums.resx 和 Enums.pt-BR.resx,其键是枚举的名称及其值(即 DeliveryStatus_WaitingForPayment)。
这是我用来加载资源并获取枚举的相应友好名称的代码:
public static string EnumToString<T>(object obj)
{
string key = String.Empty;
Type type = typeof(T);
key += type.Name + "_" + obj.ToString();
Assembly assembly = Assembly.Load("EnumResources");
string[] resourceNames = assembly.GetManifestResourceNames();
ResourceManager = null;
for(int i = 0; i < resourceNames.Length; i++)
{
if(resourceNames[i].Contains("Enums.resources"))
{
rm = new ResourceManager(resourceNames[i], Assembly.GetExecutingAssembly());
Stream resStream = assembly.GetManifestResourceStream(resourceNames[i]);
ResourceReader reader = new ResourceReader(resStream);
IDictionaryEnumerator dict = reader.GetEnumerator();
while (dict.MoveNext())
{
string keyToCompare = dict.Key.ToString();
if (keyToCompare == key)
return dict.Value.ToString();
}
}
return obj.ToString();
}
}
此方法几乎完美地工作,只是它忽略了 CurrentUICulture 并始终从默认资源返回值,也就是说,即使当我'如果我使用 pt-BR 作为我的 CurrentUICulture,它将从 Enum.resx 而不是 Enum.pt-BR.resx 加载值。
我做错了什么?
I'm creating a method to convert an enum to a friendly string. The friendly names are stored in a resource file and are subject to globalization. So I created two resource file: Enums.resx and Enums.pt-BR.resx whose keys are the name of the enum followed by it's value (i.e DeliveryStatus_WaitingForPayment).
This is the code I'm using to load the resource and get the corresponding friendly name for the enum:
public static string EnumToString<T>(object obj)
{
string key = String.Empty;
Type type = typeof(T);
key += type.Name + "_" + obj.ToString();
Assembly assembly = Assembly.Load("EnumResources");
string[] resourceNames = assembly.GetManifestResourceNames();
ResourceManager = null;
for(int i = 0; i < resourceNames.Length; i++)
{
if(resourceNames[i].Contains("Enums.resources"))
{
rm = new ResourceManager(resourceNames[i], Assembly.GetExecutingAssembly());
Stream resStream = assembly.GetManifestResourceStream(resourceNames[i]);
ResourceReader reader = new ResourceReader(resStream);
IDictionaryEnumerator dict = reader.GetEnumerator();
while (dict.MoveNext())
{
string keyToCompare = dict.Key.ToString();
if (keyToCompare == key)
return dict.Value.ToString();
}
}
return obj.ToString();
}
}
This method works almost perfectly except that it ignores the CurrentUICulture and always returns the values from the default resource, that is, even when I'm using pt-BR as my CurrentUICulture it will load the value from Enum.resx and not Enum.pt-BR.resx.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明我采用了错误的方法来读取资源文件。我不仅不需要通过流来工作,而且还阻止我获得基于 CurrentUICulture 的结果。
该解决方案比我第一次尝试的解决方案要容易得多:
我希望这可以帮助任何人将来尝试类似的方法!
As it turns out I was taking the wrong approach to read the resource file. Not only I didn't need to work my way through a stream it was preventing me from getting the result based on the CurrentUICulture.
The solution is much easier than that of my first attempt:
I hope this helps anyone trying something similar in the future!