ASP.NET MVC language change link

2019-01-31 15:32发布

问题:

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" />

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?

回答1:

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

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

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

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


回答2:

An alternative and I feel it is more flexible

 protected override void ExecuteCore()
    {
        if (RouteData.Values["lang"] != null && !string.IsNullOrWhiteSpace(RouteData.Values["lang"].ToString()))
        {
            SetCulture(RouteData.Values["lang"].ToString());
        }
        else
        {
            var cookie = HttpContext.Request.Cookies["myappculture"];
            if (cookie != null)
            { SetCulture(cookie.Value); }
            else
            { SetCulture(HttpContext.Request.UserLanguages[0]);}
        }
        base.ExecuteCore();
    }

public ActionResult ChangeCulture(string lang, string returnUrl)
    {

        SetCulture(lang);
        // Little house keeping
        Regex re = new Regex("^/\\w{2,3}(-\\w{2})?");
        returnUrl = re.Replace(returnUrl,"/" + lang.ToLower());
        return Redirect(returnUrl);
    }

private void SetCulture(string lang)
    {
        CultureInfo ci = new CultureInfo(lang);
        System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
        System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);

        // Force a valid culture in the URL
        RouteData.Values["lang"] = lang;

        // save the location into cookie
        HttpCookie _cookie = new HttpCookie("myappculture", Thread.CurrentThread.CurrentUICulture.Name);
        _cookie.Expires = DateTime.Now.AddYears(1);
        HttpContext.Response.SetCookie(_cookie);
    }

In the view

I kept the resource in a different project as follows



回答3:

If you use the App_GloabalResources to store your resx language files, all you have to do is add a drop down which changes the current thread's UI Culture and this will automatically select the right resx language file to display.



回答4:

App_GloabalResources is not the right place the resources when it comes to MVC programmering. See http://buildingwebapps.blogspot.no/2012/05/right-way-to-use-resource-files-for.html