环境:vs2012 asp.net mvc4.
实现方式:resource 资源文件,根据路由规则中Lang参数来判断载入哪种语言方式
在网上找到了相关资料,顺便自己做了个练习,新建工程之类的步骤就免了,该注意的地方说明下,记着方便下次使用。
1:添加资源文件,记得设置资源文件的访问模式为public,不然默认是Internal,外面会访问不到:
2:添加路由规则,记得加在Default路由规则的前面,否则新规则没用,详细原因请参考这篇文章
1 routes.Add(new Route( 2 "{lang}/{controller}/{action}/{id}", 3 new RouteValueDictionary(new { 4 lang = "en-US",//默认为E文 5 controller = "Account", 6 action = "Logon", 7 id = UrlParameter.Optional 8 }), 9 new MultiLangRouteHandler()//这个类主要是通过GetHttpHandler来取得当前Lang的值 10 ));
1 public class MultiLangRouteHandler : MvcRouteHandler { 2 protected override IHttpHandler GetHttpHandler(RequestContext requestContext) { 3 string lang = requestContext.RouteData.Values["lang"].ToString(); 4 5 //Thread.CurrentThread.CurrentCulture = new CultureInfo(lang); 6 Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang); 7 8 //return new MvcHandler(requestContext); 9 return base.GetHttpHandler(requestContext); 10 } 11 }
有关代码中的CurrentUICulture可以参考我上一篇文章,里面有详细解释。
3:中英文切换入口。
1 <div> 2 @{ 3 string controller = ViewContext.Controller.ValueProvider.GetValue("controller").RawValue.ToString(); 4 string action = ViewContext.Controller.ValueProvider.GetValue("action").RawValue.ToString(); 5 //string lang = ViewContext.Controller.ValueProvider.GetValue("lang").RawValue.ToString(); 6 } 7 @Html.ActionLink("中文", action, new { Controller = controller, lang = "zh-CN" }, new {@class="btn-a" }) 8 @Html.ActionLink("English", action, new { Controller = controller, lang = "en-US" }, new {@class="btn-a" }) 9 </div>
4:界面上普通文字的多语言
其实我有按照上面的那篇文章写测试程序,但是写死的资源文件并没有根据中英文来切换,我都不知道是不是哪里有问题,闷~,后来mvc群里面的朋友(无情水)介绍另外一篇文章,我参照着改写了下,在此说声ths...
public static class LangHelper { //界面普通文字的多语言 public static string GetLangbyKey(this HtmlHelper htmlhelper, string key) { Type resourceType = (Thread.CurrentThread.CurrentUICulture.Name == "en-US") ? typeof(Resources.en_US) : typeof(Resources.zh_CN); PropertyInfo p = resourceType.GetProperty(key); if (p != null) return p.GetValue(null, null).ToString(); else return "undefined"; } //js定义多语言弹出框 public static string LangOutJsVar(this HtmlHelper htmlhelper, string key) { Type resourceType = (Thread.CurrentThread.CurrentUICulture.Name == "en-US") ? typeof(Resources.en_US) : typeof(Resources.zh_CN); PropertyInfo p = resourceType.GetProperty(key); if (p != null) return string.Format("var {0} = ‘{1}‘", key, p.GetValue(null, null).ToString()); else return string.Format("var {0} = ‘{1}‘", key, "undefined"); } }
View页面调用直接用函数的方式。如:<h2>@Html.GetLangbyKey("LogonDisplay")</h2>
5:DisplayName 的多语言化
重新定义一LocalDisplayName属性,他继承自DisplayNameAttribute
public class LocalDisplayName : DisplayNameAttribute { private string _defaultName = ""; public Type ResourceType { get { return (Thread.CurrentThread.CurrentUICulture.Name == "en-US") ? typeof(Resources.en_US) : typeof(Resources.zh_CN); } } public string ResourceName { get; set; } public LocalDisplayName(string defaultName) { _defaultName = defaultName; } public override string DisplayName { get { PropertyInfo p = ResourceType.GetProperty(ResourceName); if (p != null) return p.GetValue(null, null).ToString(); else return _defaultName; } } }
在Model类上就把Display属性换为LocalDisplayName
public class Account { [Required] [LocalDisplayName("user name", ResourceName = "UserDisplay")] public string UserName { get; set; } [Required] [LocalDisplayName("password", ResourceName = "PwdDisplay")] public string Pwd { get; set; } [Required] [Compare("Pwd",ErrorMessageResourceName="ConfirmPwdErrorDisplay")] [LocalDisplayName("confirm password", ResourceName = "ConfirmPwdDisplay")] public string ConfirmPwd { get; set; } [Required] [LocalDisplayName("remember", ResourceName = "RemDisplay")] public bool Rememberme { get; set; } }
调用方式:<div>@Html.LabelFor(m => m.UserName)</div>
实现效果:
大概这样基本的就实现了, 有个Errormessage的还没整好,有空再来写。有不对的地方大家给予指正,谢谢!
参考文档:
http://www.cnblogs.com/codehunter008/archive/2008/09/01/1281565.html
http://www.cnblogs.com/lionking/articles/1894277.html
如果是要写数据库的文字,应该怎么实现多语言呢?有什么好的方式,请教下。
下载链接: 点击下载
來源:http://www.cnblogs.com/Joans/archive/2012/08/16/2640473.html