Note that there are some explanatory texts on larger screens.

plurals
  1. POWhere to put general language adjustment?
    primarykey
    data
    text
    <p>Well, the title says it all.</p> <p>I do the following now in my LanguageFilterAttribute class:</p> <pre><code> public override void OnActionExecuting(ActionExecutingContext filterContext) { var request = filterContext.HttpContext.Request; string currentUrl = request.RawUrl; var urlHelper = new UrlHelper(request.RequestContext); string baseurl = urlHelper.Content("~"); string currentLanguageFromUrl = currentUrl.Split('/')[1]; string currentLanguageFromCulture = CultureHelper.CheckCulture(); var currentLanguageFromCookie = request.Cookies["_culture"]; var possibleCultures = UnitOfWork.CulturesRepository.GetListOfCultureNames(); if (possibleCultures.All(culture =&gt; currentLanguageFromUrl != culture)) { string cultureName; string newUrl; if (currentLanguageFromCookie != null) { cultureName = currentLanguageFromCookie.Value; CultureHelper.SetCulture(cultureName); newUrl = baseurl + cultureName; filterContext.Result = new RedirectResult(newUrl); return; } if (currentLanguageFromCulture != null) { cultureName = currentLanguageFromCulture; CultureHelper.SetCulture(cultureName); newUrl = baseurl + cultureName; filterContext.Result = new RedirectResult(newUrl); return; } cultureName = possibleCultures[0]; CultureHelper.SetCulture(cultureName); newUrl = baseurl + cultureName; filterContext.Result = new RedirectResult(newUrl); return; } CultureHelper.SetCulture(currentLanguageFromUrl); base.OnActionExecuting(filterContext); }; </code></pre> <p>Which sets the language when you select a new one from the dopdown on the shared Layout page (this works btw, selecting a different language triggers respectively, the above and below class correctly).</p> <pre><code> public static void SetCulture(string culture) { var cultureCookie = HttpContext.Current.Request.Cookies["_culture"] ?? new HttpCookie("_culture"); cultureCookie.Value = culture; var request = HttpContext.Current.Request; cultureCookie.Domain = request.Url.Host; cultureCookie.Expires = DateTime.Now.AddYears(1); cultureCookie.Path = "/"; HttpContext.Current.Response.Cookies.Add(cultureCookie); CultureInfo info = CultureInfo.CreateSpecificCulture(culture.ToString()); Thread.CurrentThread.CurrentCulture = info; Thread.CurrentThread.CurrentUICulture = info; } </code></pre> <p>The problem with this is, as you can guess, I will have to apply the [LanguageFilter] attribute on all my controllers. Isn't there a file where I can place this that will change my language every time I go to another page?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload