Dynamically loading a resource and reading the correct value for the CurrentUICulture

时间秒杀一切 提交于 2019-12-11 06:03:09

问题


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?


回答1:


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!



来源:https://stackoverflow.com/questions/3125153/dynamically-loading-a-resource-and-reading-the-correct-value-for-the-currentuicu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!