문제

Binding Drop down with localized value using enum in C#.

I have value for all the languages. So I do not need to fetch it from anywhere else. I can write all the value in different languages resx file. But I am not sure how it works.

I am using C# windows form. framework 3.5

도움이 되었습니까?

해결책

I have used following code and it works fine for me

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"));

In above code LblName is a Label and StrMsg a simple string.

In resource file I will write like this:

Name Value


Lbl.Name labelname................

StrMsg anycustom msg............

다른 팁

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;
        }
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top