ASP .NET Core 1.0 RTM Localization not working

前端 未结 3 519
小蘑菇
小蘑菇 2020-12-19 07:09

I\'ve been trying to implement localization for my asp .NET Core 1.0 RTM web app following microsofts documentation and I just can not get it to work. The problem I\'m havin

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-19 07:54

    You are probably hitting the following : https://github.com/aspnet/Mvc/issues/4692

    Mainly take a look at the comment: https://github.com/aspnet/Mvc/issues/4692#issuecomment-223671462

    Summary: You can create a Resource filter in MVC to workaround the issue:

    public class CultureSettingResourceFilter : IResourceFilter, IOrderedFilter
    {
        public int Order
        {
            get
            {
                return int.MinValue;
            }
        }
    
        public void OnResourceExecuted(ResourceExecutedContext context)
        {
            // By this time the response would already have been started, so do not try to modify the response
        }
    
        public void OnResourceExecuting(ResourceExecutingContext context)
        {
            var culture = httpContext.GetRouteValue("your-culture-key-name")?.ToString();
            // set your culture here
            CultureInfo.CurrentCulture = culture;
            CultureInfo.CurrentUICulture = culture;
        }
    }
    
    services.AddMvc(o =>
    {
        o.Filters.Add(new CultureSettingResourceFilter());
    });
    

提交回复
热议问题