动态加载资源并读取 CurrentUICulture 的正确值

发布于 2024-09-07 07:50:30 字数 1447 浏览 10 评论 0原文

我正在创建一种将枚举转换为友好字符串的方法。友好名称存储在资源文件中并受全球化影响。因此,我创建了两个资源文件: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗琴音 2024-09-14 07:50:30

事实证明我采用了错误的方法来读取资源文件。我不仅不需要通过流来工作,而且还阻止我获得基于 CurrentUICulture 的结果。

该解决方案比我第一次尝试的解决方案要容易得多:

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();
      }

}

我希望这可以帮助任何人将来尝试类似的方法!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文