开发者

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 mu开发者_如何转开发ltilingual?

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 e.g. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜