c# - 我们可以用 c# 在 asp.net 中制作多语言枚举吗

标签 c# asp.net enums multilingual

我在多语言网站上工作,我在其中使用了一些枚举,现在我们可以根据多语言制作这些枚举了吗?

我的枚举结构是

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

像这样。我想在描述中写多语言文本。

最佳答案

您应该按照以下步骤操作:

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

(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
        }
    }
}

用法:

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

关于c# - 我们可以用 c# 在 asp.net 中制作多语言枚举吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6936023/

相关文章:

c# - nHibernate QueryOver 在参数为空时获取所有记录

c# - RichTextBox最新行

asp.net - 将 ValidationGroup 和客户端验证与 ASP.NET 自定义服务器控件结合使用

c# - 如何将任何验证控件传递给方法?

javascript - typescript 中的字符串枚举

c# - 团结 : is it possible to configure the scene for trigger colliders to only work with one layer?

c# - 正则表达式问题 C#

javascript - 使用 ClientScript 类将 VB.net 数组传递给 javascript

java - 如何在另一个 Java 类中使用枚举?

java - JAXB:将 XML 枚举验证为字符串,同时在 XSD 中保留类型