首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我们能用c#在asp.net中实现枚举多语言吗?

我们能用c#在asp.net中实现枚举多语言吗?
EN

Stack Overflow用户
提问于 2011-08-04 11:58:27
回答 3查看 2.7K关注 0票数 5

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

我的枚举结构是

代码语言:javascript
运行
复制
public enum abc
{
  [Description{"multilingual text"}]
  StatucActive = 1
}

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-08-11 00:02:18

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

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

票数 1
EN

Stack Overflow用户

发布于 2011-08-04 12:55:03

您应该遵循以下步骤:

(1)准备resource.en-US.resx/resource.zh-CN.resx/etc等资源文件,每个资源文件都有键和值,文件之间键相同,语言不同值不同。

(2)定义您自己的DescriptionAttribute,如下所示:

代码语言:javascript
运行
复制
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
        }
    }
}

用法:

代码语言:javascript
运行
复制
public enum MyTexts
{
    [LocalDescription(CultureCode="zh-CN", ResourceKey="Title")]
    Title = 0,
    [LocalDescription(ResourceKey="Status")]   //default CultureCode
    Status = 1
}
票数 6
EN

Stack Overflow用户

发布于 2011-08-04 12:18:07

翻译枚举的一种简单方法是为所需的每种语言创建一个值数组。

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

String language2[] ={“其他值”,“其他value2"};

String multi = language2enumvalue;

枚举值成为翻译字符串数组的索引。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6936023

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档