ASP .NET Core 1.0 RTM Localization not working

前端 未结 3 476
小蘑菇
小蘑菇 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());
    });
    
    0 讨论(0)
  • 2020-12-19 07:57

    I experimented with i18n and I got my translations to work when supported cultures matched with the cultures in resource file names

    var supportedCultures = new List<CultureInfo>{
         new CultureInfo("en-US"),
         new CultureInfo("sl-SI"),
         new CultureInfo("de-DE"),
         new CultureInfo("hr-HR")
      };
    

    Change your resource file names from

    Resources/Views/ControllerName/ViewName.sl.resx

    to specific language culture

    Resources/Views/ControllerName/ViewName.sl-SI.resx

    0 讨论(0)
  • 2020-12-19 08:12

    Edit on @Siim Haas.
    It's works good.
    In my case: I using the LanguageViewLocationExpanderFormat.SubFolder

    At ConfigureServices(IServiceCollection services)

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; });
    
        services.AddMvc()
            .AddViewLocalization(
                Microsoft.AspNetCore.Mvc.Razor.LanguageViewLocationExpanderFormat.SubFolder,
                opts => { opts.ResourcesPath = "Resources"; }
            )
            .AddDataAnnotationsLocalization();
    
        services.Configure<RequestLocalizationOptions>(opts =>
        {
            var supportedCultures = new[]
            {
                new CultureInfo("en-AU"),
                new CultureInfo("en-GB"),
                new CultureInfo("en-US"),
                new CultureInfo("en"),
                new CultureInfo("zh-cn"),
                new CultureInfo("zh"),
            };
            opts.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("en-US");
            opts.SupportedCultures = supportedCultures;
            opts.SupportedUICultures = supportedCultures;
        });
    }
    

    At Configure (IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        app.UseRequestLocalization();
    
        app.UseMvc(routes =>
        {
            routes.MapRoute(name: "default", template: "{controller=Home}/{action=Index}");
        });
    }
    

    My Resources stucts Like:

    {Your Project}/Resources/Views/{your controller}/ViewName.en-us.resx

    0 讨论(0)
提交回复
热议问题