动态加载资源并读取CurrentUICulture的正确值 [英] Dynamically loading a resource and reading the correct value for the CurrentUICulture
问题描述
我正在创建一种将枚举转换为友好字符串的方法.友好名称存储在资源文件中,并且要进行全球化.因此,我创建了两个资源文件:Enums.resx和Enums.pt-BR.resx,它们的键是枚举的名称,后跟它的值(即DeliveryStatus_WaitingForPayment).
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();
}
}
该方法几乎可以完美地工作,除了它忽略CurrentUICulture并始终从默认资源返回值之外,也就是说,即使当我将pt-BR用作CurrentUICulture时,它也会从Enum.resx中加载该值,而不是Enum.pt-BR.resx.
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.
我在做什么错了?
推荐答案
事实证明,我采用了错误的方法来读取资源文件.不仅我不需要遍历流,还阻止了我基于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:
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"))
{
//The substring is necessary cause the ResourceManager is already expecting the '.resurces'
rm = new ResourceManager(resourceNames[i].Substring(0, resourceNames[i].Length - 10), assembly);
return rm.GetString(key);
}
return obj.ToString();
}
}
我希望这可以帮助将来尝试类似方法的任何人!
I hope this helps anyone trying something similar in the future!
这篇关于动态加载资源并读取CurrentUICulture的正确值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!