代码之家  ›  专栏  ›  技术社区  ›  Banketeshvar Narayan

使用C#〔closed〕中的枚举使用本地化值绑定下拉列表

  •  2
  • Banketeshvar Narayan  · 技术社区  · 11 年前

    使用C#中的枚举绑定带有本地化值的下拉列表。

    我对所有语言都有价值。所以我不需要从其他地方取它。 我可以将所有值写入不同语言的resx文件。但我不确定它是如何工作的。

    我使用的是C#windows form.framework 3.5

    2 回复  |  直到 11 年前
        1
  •  2
  •   user3208036    10 年前

    我使用了以下代码,它对我来说很好

    using System.Globalization;
    using System.ComponentModel;
    using System.Threading;
    

    .......

    .......

    .......

    Thread.CurrentThread.CurrentUICulture = new CultureInfo(DDLLang.SelectedValue);
    ComponentResourceManager resource = new ComponentResourceManager(typeof(Default));
    resource.ApplyResources(LblName, "LblName");
    LblName.ID = "LblName";
    Response.Write(resource.GetString("strMsg",CultureInfo.CurrentUICulture)??resource.GetString("strMsg"));
    

    在上面的代码中,LblName是一个Label,StrMsg是一个简单的字符串。

    在资源文件中,我将这样写:

    名称值


    Lbl.名称 标签名称。。。。。。。。。。。。。。。。

    StrMsg(字符串) 任意自定义消息。。。。。。。。。。。。

        2
  •  1
  •   Uri Agassi    10 年前
    private static ResourceManager _resources = new ResourceManager("MyClass.myResources",
        System.Reflection.Assembly.GetExecutingAssembly());
    
    public static IEnumerable<string> GetLocalizedNames(this IEnumerable enumValues)
    {
        foreach(var e in enumValues)
        {
            string localizedDescription = _resources.GetString(String.Format("{0}.{1}", e.GetType(), e));
            if(String.IsNullOrEmpty(localizedDescription))
            {
                yield return e.ToString();
            }
            else
            {
                yield return localizedDescription;
            }
        }
    }