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

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

问题描述

我有一个使用资源的 ASP.NET MVC 站点,它使用两种语言.为了允许服务器以适当的语言呈现网站(取决于用户浏览器中配置的语言),我在 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 中,如果它不存在,则回退到浏览器配置...有可能吗?

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?

推荐答案

你可以看看 以下指南.它使用 Session 来存储当前的用户语言偏好,但可以很容易地调整代码以使用 cookie.这个想法是你将有一个控制器动作:

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 和会话来存储当前语言对 SEO 不友好.当我需要本地化应用程序时,我更喜欢使用包含语言的特殊路由:

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 
    }
);

然后在我所有的网址前加上该语言.这为不同语言提供了唯一的 url,以便机器人可以正确索引所有内容.现在剩下的就是修改 Application_AcquireRequestState 方法,使其使用 url 的 lang 标记,并根据其值设置正确的 Thread.CurrentThread.CurrentUICultureThread.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天全站免登陆