ASP.NET MVC的语言更改链接 [英] ASP.NET MVC language change link

查看:176
本文介绍了ASP.NET MVC的语言更改链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET MVC的网站,它的使用资源两种语言。为了让服务器在apropiate语言网站(取决于在用户的浏览器配置的)present我把我的web.config以下内容:

I have an ASP.NET MVC site that it's in two languages using Resources. To allow the server to present the site in the apropiate language (depending on the one that's configured in the user's browser) I put the following in my web.config:

<globalization culture="es-es" uiCulture="auto" />

我如何添加一个链接来改变的UICulture?我想存储在cookie中选择,如果它不是present,然后回落到浏览器配置......这可能吗?

How can I add a link to change the uiCulture? I want to store the selection in a cookie and if it's not present, then fall back to the browser configuration... Is it possible?

推荐答案

您可以看看的的以下指南。它使用会话来存储当前用户语言preference但code可能会为了使用浏览器来很容易调整。这个想法是,你将有一个控制器动作:

You may take a look at the following guide. It uses Session to store the current user language preference but the code could be very easily tweaked in order to use a cookie. The idea is that you will have a controller action:

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的值:

and then in Global.asax you could subscribe for the Application_AcquireRequestState event in order to set the current thread culture based on the value of the 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);
    }
}

现在这个存在使用cookie和session保存当前的语言是不是搜索引擎友好表示。我preFER做,当我需要一个本地化的应用程序是使用其中将包含语言的一种特殊的路由:

Now this being said using cookies and session to store current language is not SEO friendly. What I prefer doing when I need a localized application is to use a special route which will contain the language:

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

然后preFIX我的所有URL的语言。这提供了独特的网址,不同的语言,使机器人可以恰当的索引的所有内容。现在,所有剩下的就是修改 Application_AcquireRequestState 方法,以便它使用郎的url 标记,并根据其值设置正确的 Thread.CurrentThread.CurrentUICulture Thread.CurrentThread.CurrentCulture

and then prefix all my urls with the language. This provides unique urls for different languages so that robots can properly index all content. Now all that's left is to modify the Application_AcquireRequestState method so that it uses the lang token of the url and based on its value set the proper Thread.CurrentThread.CurrentUICulture and Thread.CurrentThread.CurrentCulture.

和现在,当你想改变你只会生成适当的链接的语言:

And now when you wanted to change the language you would simply generate the proper link:

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

这篇关于ASP.NET MVC的语言更改链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆