ASP.NET MVC5 Change the language / culture by clicking on Html.ActionLink

I have a problem with my ASP.NET MVC5 application. My application can install lang / culture installed in the browser (now only English and Polish (by default)). I want users to change their language / culture by clicking on Html.ActionLink.

I created a class:

namespace Guestbook
{
    public static class Click
    {
        public static void SetCulture(string name)
        {
            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(name);
            Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
        }

    }
}

And in my view:

@Html.ActionLink("PL", "", "Guests", routeValues: null, htmlAttributes: new { onclick = "SetCulture(\"pl\");" })
@Html.ActionLink("EN", "", "Guests", routeValues: null, htmlAttributes: new { onclick = "SetCulture(\"en\");" })

Of course, this does not work. What do I need more? JavaScript function?

+4
source share
2 answers

The simplest answer is that you need to create the controller that you are referring to.

public class LanguageController : Controller
{
    public ActionResult SetLanguage(string name)
    {
        Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(name);
        Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

        HttpContext.Current.Session["culture"] = name;

        return RedirectToAction("Index", "Home");
    }
}

Then, in your opinion:

<a href="@Url.Action("SetLanguage", "Language", new { @name = "pl" })">Polski</a>
<a href="@Url.Action("SetLanguage", "Language", new { @name = "en" })">English</a>

You might want to save user data in a session or similar.

EDIT:

, Application_BeginRequest global.asax.

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    var name = HttpContext.Current.Session["culture"] as string;

    if (string.IsNullOrEmpty(name))
    {
        return;
    }

    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(name);
    System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture;
}

EDIT:

cookie SetLanguage:

var cookie = new HttpCookie("_culture", name);
cookie.Expires = DateTime.Today.AddYears(1);
Response.SetCookie(cookie);

cookie Application_BeginRequest:

var cookie = HttpContext.Current.Request.Cookies["_culture"];
var name = cookie != null ? cookie.Value : null;
+7

. @Olivier ( mate!) , , , cookie, .

:

public class LanguageController : BaseController
    {
        // GET: Language
        public ActionResult SetLanguage(string name)
        {
            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(name);
            Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

            HttpCookie cultureCookie = new HttpCookie("_culture");
            cultureCookie.Value = name;
            cultureCookie.Expires = DateTime.UtcNow.AddYears(1);

            Response.Cookies.Remove("_culture");
            Response.SetCookie(cultureCookie);


            return RedirectToAction("Index", "Guests");
        }
    }

LanguageController BaseController ( Controller), : ASP.NET MVC 5 Nadeem Afana

:

<a href="@Url.Action("SetLanguage", "Language", new { @name = "pl" })">Polski</a>
<a href="@Url.Action("SetLanguage", "Language", new { @name = "en" })">English</a>
+1

All Articles