简体   繁体   中英

Can we make enum multilingual in asp.net with c#

I am working on multilingual site and in which i used some enum and now it can be possible that we can make theses enum as per multilingual?

My enum structure is

public enum abc
{
  [Description{"multilingual text"}]
  StatucActive = 1
}

like this. i want to write multilingual text in description.

You should follow the steps:

(1) prepare the resource files eg resource.en-US.resx / resource.zh-CN.resx /etc. Each resource file has keys and values, their keys are the same between files, values are different in languages.

(2) define your own DescriptionAttribute , something like this:

public class LocalDescriptionAttribute : DescriptionAttribute
{
    public string ResourceKey { get; set; }
    public string CultureCode { get; set; }
    //you can set a default value of CultureCode
    //so that you needn't set it everywhere
    public override string Description
    {
        get
        {
            //core of this attribute
            //first find the corresponding resource file by CultureCode
            //and then get the description text by the ResourceKey
        }
    }
}

The usage:

public enum MyTexts
{
    [LocalDescription(CultureCode="zh-CN", ResourceKey="Title")]
    Title = 0,
    [LocalDescription(ResourceKey="Status")]   //default CultureCode
    Status = 1
}

No we can't use enum as a multilingual but i have an alternate option that is use resource file it working like an enum in some situations.

please try resource file and it will solve your problem....

An easy way to translate an enumerated would be to create an array of values for each language you want.

String language1[] = {"value","value2"};

String language2[] = {"other value","other value2"};

String multi = language2[enumvalue];

Your enum value become the index to your string array of translations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM