码迷,mamicode.com
首页 > 编程语言 > 详细

在mvc4中多语言建站的实例

时间:2016-02-26 17:08:03      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:

环境: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

在mvc4中多语言建站的实例

标签:

原文地址:http://www.cnblogs.com/xuhongfei/p/5220709.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!