惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
人人都是产品经理
人人都是产品经理
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
小众软件
小众软件
B
Blog
博客园 - 叶小钗
Microsoft Azure Blog
Microsoft Azure Blog
Apple Machine Learning Research
Apple Machine Learning Research
A
About on SuperTechFans
J
Java Code Geeks
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
博客园 - 【当耐特】
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Recent Announcements
Recent Announcements
宝玉的分享
宝玉的分享
Martin Fowler
Martin Fowler
Hugging Face - Blog
Hugging Face - Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
V
V2EX

博客园 - Sphix

根据银行卡获取银行卡开户银行和类型 HTTP could not register URL http://+:8000/testservice/. Your... 一些必不可少的Sublime Text 2插件 C# CultureInfo列表 解决"There is already an open DataReader associated with this Command which must be closed first." exception in EF 中 C#正则表达式整理备忘 Lucene.net 开发记录 在Asp.net 4.0 中动态注册HttpModule ASP.net 视频上传转换flv并且抓取第一帧生成图片源码 揭秘全球最大网站Facebook背后的那些软件 asp.net网站管理工具 的 地址(Web Site Administration Tool ) Sql Server 2008 Full-text search Error: Word breaking timed out for the full-text query string. Associations in EF Code First CTP5: Part 1 – Complex Types wordpress 文章缩略图功能 简单实际的方式分隔Admin 区域 EF4 中Self-track entity 错误用于单web开发中要注意的地方 C#验证文件类型 Asp.net文件下载 SQLite 资源汇总
C# Enum 类型的本地化
Sphix · 2011-03-29 · via 博客园 - Sphix
枚举类型本地化操作的简单方案,并应用到Asp.net MVC的DropdownList中。 

/// <summary>
    
/// Enum Localizable. default resouce key={Prefix}_{Enum}
    
/// If you ignore Prefix,will return {EnumTypeName}_{Enum} format
    
/// </summary>
    [AttributeUsage(AttributeTargets.Enum, AllowMultiple = true, Inherited = true)]
    
public class LocalizableAttribute : Attribute
    {
        
public LocalizableAttribute(Type language)
        {
            
this.LanguageType = language;
        }
        
public LocalizableAttribute(Type language, String prefix)
            :
this(language)
        {
            Prefix 
= prefix;
        }
public Type LanguageType
        {
            
get;
            
set;
        }
        
public string Prefix
        {
            
get;
            
set;
        }
    }
    
public static class EnumerableExtension
    {
        
public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> action)
        {
            
foreach (T item in enumerable)
            {
                action(item);
            }
        }
public static string ToLocalizable(this Enum value)
        {
            LocalizableAttribute customAttr
=(LocalizableAttribute) Attribute.GetCustomAttribute(value.GetType(), typeof(LocalizableAttribute));
            ResourceManager _resources 
= new ResourceManager(customAttr.LanguageType);
            var prefix
=customAttr.Prefix;
            
if (string.IsNullOrEmpty(prefix))
            {
                prefix
=value.GetType().Name;
            }
string rk = String.Format("{0}_{1}",prefix,value);
            
string localiza=_resources.GetString(rk);
            
if (localiza == null)
            {
                
return value.ToString();
            }
            
return localiza;
        }
public static IEnumerable<SelectListItem> ToSelectList(this Enum enumValue)
        {
            
return from Enum e in Enum.GetValues(enumValue.GetType())
                   select 
new SelectListItem { Selected = e.Equals(enumValue), Text = EnumerableExtension.ToLocalizable(e), Value = e.ToString() };
        }
    }

使用:

[Localizable(typeof(CoreLanguage),"UesrTitle")] 
    
public enum UserTitle
    {
        Mr,
        Miss
    }
//in view
@Html.DropDownListFor(m => m.UserTitle, Model.UserTitle.ToSelectList(), new { id = "UserTitle" })