微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net-mvc-3 – ASP.NET MVC语言更改链接

我有一个ASP.NET MVC网站,它使用两种语言使用资源.要让服务器以适当的语言呈现站点(取决于用户浏览器中配置的站点),我将以下内容放在我的web.config中:
<globalization culture="es-es" uiCulture="auto" />

如何添加链接来更改uiCulture?我想将选择存储在cookie中,如果它不存在,则回到浏览器配置…是可能吗?

解决方法

您可以查看 following guide.它使用会话来存储当前的用户语言首选项,但代码可能很容易调整,以便使用cookie.这个想法是你会有一个控制器的动作:
public ActionResult ChangeCulture(string lang,string returnUrl)
{
    var langCookie = new HttpCookie("lang",lang)
    {
        HttpOnly = true
    };
    Response.AppendCookie(langCookie);
    return Redirect(returnUrl);
}

然后在Global.asax中,您可以订阅Application_AcquireRequestState事件,以便根据cookie的值设置当前的线程文化:

protected void Application_AcquireRequestState(object sender,EventArgs e)
{
    var langCookie = HttpContext.Current.Request.Cookies["lang"];
    if (langCookie != null)
    {
        var ci = new CultureInfo(langCookie.Value);
        //Checking first if there is no value in session 
        //and set default language 
        //this can happen for first user's request
        if (ci == null)
        {
            //Sets default culture to english invariant
            string langName = "en";

            //Try to get values from Accept lang HTTP header
            if (HttpContext.Current.Request.UserLanguages != null && HttpContext.Current.Request.UserLanguages.Length != 0)
            {
                //Gets accepted list 
                langName = HttpContext.Current.Request.UserLanguages[0].Substring(0,2);
            }

            langCookie = new HttpCookie("lang",langName)
            {
                HttpOnly = true
            };


            HttpContext.Current.Response.AppendCookie(langCookie);
        }

        //Finally setting culture for each request
        Thread.CurrentThread.CurrentUICulture = ci;
        Thread.CurrentThread.CurrentCulture = ci;

        //The line below creates issue when using default culture values for other
        //cultures for ex: NumericSepratore.
        //Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);
    }
}

在这是说使用cookies和会话来存储当前语言不是SEO友好.当我需要一个本地化的应用程序时,我喜欢做什么是使用一个特殊的路由,其中​​包含语言:

routes.MapRoute(
    "Default","{lang}/{controller}/{action}/{id}",new 
    { 
        lang = "en-US",controller = "Home",action = "Index",id = UrlParameter.Optional 
    }
);

然后使用该语言对我的所有URL进行前缀.这为不同的语言提供唯一的URL,以便机器人可以正确地索引所有内容.现在剩下的是修改Application_AcquireRequestState方法,以便它使用url的lang标记,并根据其值设置正确的Thread.CurrentThread.CurrentUICulture和Thread.CurrentThread.CurrentCulture.

现在,当你想改变语言的时候,你只需要生成正确的链接

@Html.ActionLink("Page index en français","index",new { lang = "fr-FR" })

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“WPF...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这...
Some samples are below for ASP.Net web form controls:(from http://www.visualize.uk.com/resources/asp
问题描述: 对于未定义为 System.String 的列,唯一有效的值是(引发异常)。 For columns not defined as System.String, the only vali
最近用到了CalendarExtender,结果不知道为什么发生了错位,如图在Google和百度上找了很久,中文的文章里面似乎只提到了如何本地化(就是显示中文的月份)以及怎么解决被下拉框挡住的问题,谈
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence changed a lot since 1.1. Here is the order: App
静态声明: &#39; Style=&quot;position: relative&quot; AppendDataBoundItems=&quot;True&quot;&gt; (无 或 空 或
以下内容是从网络上搜集资料,然后整理而来的。不当之处,请不吝指教。(The following were from network, and edited by myself. Thanks in a
Imports System Imports System.Reflection Namespace DotNetNuke &#39;*********************************
Ok so you have all seen them: “8 million tools for web development”, “5 gagillion tools that if you