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

HttpCookie加匿名类实现多语言

时间:2016-06-02 00:44:32      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:

突然想做一个多语言网站,确不知道怎么实现好,突然想到了HttpCookie,然后页面后台用匿名类实现语言的储存。

            string lan = Request["str_lan"];
            ViewBag.lan = lan;

            HttpCookie hc = new HttpCookie("language");
            hc.Value = lan;
            hc.Expires = DateTime.Now.AddDays(1);
            Response.Cookies.Add(hc);        

HttpCookie用来当前记录语言种类。

 

前台页面取值:

@{
    ViewBag.Title = "Home Page";
    var language = new Dictionary<string, object>();
    language.Add("zh_cn", new { name = "邓永高" });
    language.Add("en", new { name = "dyg" });

    string get_lan = string.Empty;
    HttpCookie cookie_language = Request.Cookies["language"];
    if (cookie_language != null)
    {
        get_lan = cookie_language.Value;
    }

}


@using MVCMutiLan.Controllers;

<h2>@Html.getPageLanVal(language, get_lan, "name").ToString()</h2>
<form action="/Home/Index" method="post">

    <select name="str_lan">
        <option value="zh_cn">简体中文</option>
        <option value="en">英文</option>
    </select>  <input type="submit" value="设置语言" />

</form>

@ViewBag.lan

 

控件取值的扩展方法:

 public static class HtmlHelpers
    {
        public static object getPageLanVal(this HtmlHelper helper, Dictionary<string, object> dic, string strlan, string Lanvalue)
        {
            return dic[strlan].GetType().GetProperties().Where(x => x.Name == Lanvalue).FirstOrDefault().GetValue(dic[strlan]);
        }
    }

 

HttpCookie加匿名类实现多语言

标签:

原文地址:http://www.cnblogs.com/dyg540/p/5551560.html

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