asp.net core Set Culture in ActionFilter for resx file

244 views Asked by At

I have an action filter that checks the cookie value and should create the correct culture

public class InitializeActionFilter: ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext context)
    {
        string lang = context.HttpContext.Request.Cookies["lang"];          

        context.HttpContext.Response.Cookies.Append(
            CookieRequestCultureProvider.DefaultCookieName,
            CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(lang)),
            new CookieOptions { Expires = DateTimeOffset.UtcNow.AddMinutes(20) } 
        );
        var cultureInfo = CultureInfo.GetCultureInfo(lang);
        Thread.CurrentThread.CurrentCulture = cultureInfo;
        Thread.CurrentThread.CurrentUICulture = cultureInfo;
  
        Thread.CurrentThread.CurrentCulture = new CultureInfo(lang);
        Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(lang);

        base.OnActionExecuting(context);
    }
}

and on the controller/ action I add the action filter

[InitializeActionFilter]   
    public IActionResult Index()
    {
        _log.Error(">>>>>>>>>current culture: " + Thread.CurrentThread.CurrentCulture);
        _log.Error(">>>>>>>>>current culture: " + Thread.CurrentThread.CurrentUICulture);
     }

when reviewing the culture it is the correct (fr-CA), but the resource files i.e. car.fr.resx does not get called, seems car.resx (english) is displayed but if I then refresh the page f5 car.fr.resx gets called and the correct content is displayed.

I think the resource files is loaded before culture gets set, how would I set the correct culture before resource file gets loaded.

also tried putting the code under OnActionExecuted still doesn't work

0

There are 0 answers