初用asp.net mvc3 和 4,发现asp.net mvc提供非常好框架,今天在做例子的时候想到标题国际化问题,比如:Albums中有AlbumArtUrl属性,我希望在中文环境下显示“专辑艺术家地址”,而在英文环境下显示“Album Art Url”,为了解决这个问题需要如下步骤:
1.创建一个自定义的标记,将国际化KEY存入其中;
2.View上改造原有的@Html.LabelFor(model => model.AlbumArtUrl)
为此,我们先建立一个自定义标记
namespace MvcMusicStore {
public class ResDisplayNameAttribute:Attribute {
public string ResKey { get; set; }
public ResDisplayNameAttribute() {
this.ResKey = string.Empty;
}
public ResDisplayNameAttribute(string resKey) {
this.ResKey = resKey;
}
}
}
然后在Album的AlbumArtUrl属性上,添加这个标记
namespace MvcMusicStore.Models {
/// <summary>
/// 专辑
/// </summary>
//[Bind(Exclude="AlbumId")]
public class Album {
//[ScaffoldColumn(false)]
public int AlbumId { get; set; }
[DisplayName("Genre")]
public int GenreId { get; set; }
[DisplayName("Artist")]
public int ArtistId { get; set; }
[Required(ErrorMessage="An Album Title is required")]
[StringLength(160)]
public string Title { get; set; }
[Required(ErrorMessage = "Price is required")]
[Range(0.01,100.00,ErrorMessage="Price must be between 0.01 and 100.00.")]
public decimal Price { get; set; }
[DisplayName("Album Art Url")]
[ResDisplayName("AlbumArtUrl")]//国际化显示的资源key值
[StringLength(1024)]
public string AlbumArtUrl { get; set; }
public virtual Genre Genre { get; set; }
public virtual Artist Artist { get; set; }
}
}
接下来我们需要在项目中添加资源文件,并添加相应的Key、Value值
在项目中添加App_GlobalResources文件夹,存放全局国际化Lang.resx和Lang.zh-CN.resx,分别存放英文和中文字符串
然后我们要分析一下@Html.LabelFor方法,原来LabelFor是HtmlHelper的一个扩展方法
public static MvcHtmlString Editor(this HtmlHelper html, string expression);
那么我们也自己增加一个扩展方法
namespace MvcMusicStore {
public static class LabelExtensions2 {
public static MvcHtmlString ResourceLabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string resLabelText) {
string labelText = null;
MemberExpression body = expression.Body as MemberExpression;
if (body != null) {
//获取lambda对应的属性信息
PropertyInfo pi = body.Member as PropertyInfo;
//获取ResDisplayNameAttribute标记
object[] objs = pi.GetCustomAttributes(typeof(ResDisplayNameAttribute), true);
if (objs.Length > 0) {
ResDisplayNameAttribute attr = objs[0] as ResDisplayNameAttribute;
//获取标记中资源Key对应的Value值
labelText = HttpContext.GetGlobalResourceObject("Lang", attr.ResKey) as string; //<globalization uiculture="zh-CN" culture="zh-CN" />
}
return html.LabelFor<TModel, TValue>(expression, labelText);
} else {
throw new ArgumentException();
}
}
}
}
最后我们更新View视图
@model MvcMusicStore.Models.Album
@using MvcMusicStore;
……
<div class="editor-label">
@*@Html.LabelFor(model => model.AlbumArtUrl)*@
@Html.ResourceLabelFor(model => model.AlbumArtUrl, "AlbumArtUrl");
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AlbumArtUrl)
@Html.ValidationMessageFor(model => model.AlbumArtUrl)
</div>
……
</fieldset>
}
注意LabelExtensions2需要使用using导入,那么如何更改全局国际化呢?只需要在web.confg中增加如下内容:
<system.web>
<globalization uiCulture="en" culture="en" />
<!--<globalization uiCulture="zh-CN" culture="zh-CN" />-->
……
</system.web>
试试结果是不是en环境下显示
zh-CN环境下显示:
由于我也是初用MVC,有不妥之处请指教。