.NET CORE不加载不同语言的资源文件

时间:2017-07-19 06:05:41

标签: asp.net .net asp.net-core

我无法在ASP.NET CORE MVC中加载翻译。

我的stratup看起来像:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddLocalization(options => options.ResourcesPath = "Resources");

        services.AddMvc()
            .AddViewLocalization(
                Microsoft.AspNetCore.Mvc.Razor.LanguageViewLocationExpanderFormat.Suffix,
                opts => { opts.ResourcesPath = "Resources"; });

        var supportedCultures = new[]
        {
            new CultureInfo("en-US"),
            new CultureInfo("sk-SK"),
            new CultureInfo("hu-HU"),
        };

        services.Configure<RequestLocalizationOptions>(options =>
        {
            options.DefaultRequestCulture = new RequestCulture(culture: "sk-SK", uiCulture: "sk-SK");
            options.SupportedCultures = supportedCultures;
            options.SupportedUICultures = supportedCultures;
        });

        // Add framework services.
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        services.AddIdentity<UserData, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        // Add application services.
        services.AddTransient<IEmailSender, AuthMessageSender>();
        services.AddTransient<ISmsSender, AuthMessageSender>();
        services.AddTransient<IProductService, ProductService>();

        // Repository
        services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
        services.AddScoped<IProductRepository, ProductRepository>();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app,
        IHostingEnvironment env,
        ILoggerFactory loggerFactory,
        ApplicationDbContext context)
    {
        var supportedCultures = new[]
        {
            new CultureInfo("en-US"),
            new CultureInfo("sk-SK"),
            new CultureInfo("hu-HU"),
        };

        app.UseRequestLocalization(new RequestLocalizationOptions
        {
            DefaultRequestCulture = new RequestCulture("sk-SK"),
            // Formatting numbers, dates, etc.
            SupportedCultures = supportedCultures,
            // UI strings that we have localized.
            SupportedUICultures = supportedCultures
        });

        //app.UseRequestCulture();

        Mapper.Initialize(config =>
        {
            config.CreateMap<ProductData, ProductDetailViewModel>();
        });

        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
            app.UseBrowserLink();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseIdentity();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });

        DbInitializer.Initialize(context);
    }

我在

中有资源文件
/Resources/Views/Product/Details.resx
/Resources/Views/Product/Details.sk.resx
/Resources/Views/Product/Details.sk-SK.resx

和我的Details.cshtml

@using Microsoft.AspNetCore.Mvc.Localization

@inject IViewLocalizer Localizer

@{
    ViewData["Title"] = Localizer["Manufacturer"];
}

问题是我总是从Details.resx获得结果,而不是来自Details.sk.resx或Details.sk-SK.resx

我做错了什么?感谢

1 个答案:

答案 0 :(得分:0)

您需要在路线配置中映射新路线。

onBackPressed()

所以路线路径如下所示。   - /

  • / EN

  • /家

  • / ES-ES /家

  • /家庭/索引

  • / ES /家/索引

  • /家庭/索引/ 123

  • / EN-US /家庭/索引/ 123