我们可以用c#在asp.net中创建枚举多语言吗

5 c# asp.net multilingual enums

我正在使用多语言网站,并且我使用了一些枚举,现在我们可以根据多语言制作这些枚举?

我的枚举结构是

public enum abc
{
  [Description{"multilingual text"}]
  StatucActive = 1
}
Run Code Online (Sandbox Code Playgroud)

像这样.我想在描述中写多语言文字.

Che*_*hen 6

你应该按照以下步骤:

(1)准备资源文件,例如resource.en-US.resx/ resource.zh-CN.resx/等.每个资源文件都有键和值,它们的键在文件之间是相同的,值在语言上是不同的.

(2)定义你自己的DescriptionAttribute东西,如下:

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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

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


Gau*_*wal 1

不,我们不能使用枚举作为多语言,但我有一个替代选项,即使用资源文件,它在某些情况下像枚举一样工作。

请尝试资源文件,它将解决您的问题......