小编Fat*_*rol的帖子

为什么不能使用ASP.NET Core Localization

我创建了一个空项目.

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddLocalization(s => s.ResourcesPath = "Resources");
    var supportedCultures = new CultureInfo[]
    {
        new CultureInfo("de-CH"),
        new CultureInfo("en-GB"),
    };

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

    services.AddMvc()
        .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
        .AddDataAnnotationsLocalization();
}


public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{

    app.UseStaticFiles();

    // Using localization 
    var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
    app.UseRequestLocalization(locOptions.Value);

    app.UseMvc();
}
Run Code Online (Sandbox Code Playgroud)

文件夹结构

Resources
|
|--Controllers 
|       HomeController.de.resx
|       HomeController.en.resx
|       HomeController.resx
Run Code Online (Sandbox Code Playgroud)

调节器

public class HomeController : Controller …
Run Code Online (Sandbox Code Playgroud)

c# localization asp.net-core

6
推荐指数
1
解决办法
4651
查看次数

为什么 ASP.NET Core、区域中的属性路由不起作用

我创建了一个空项目。

启动.cs

public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddLocalization(options => options.ResourcesPath = "Resources");
            services.AddMvc().AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix).AddDataAnnotationsLocalization();

            services.Configure<RequestLocalizationOptions>(options =>
            {
                var supportedCultures = new[]
                {
                new CultureInfo("en-US"),
                new CultureInfo("de-DE"),
                new CultureInfo("tr-TR"),
                };

                options.DefaultRequestCulture = new RequestCulture(culture: "en-US", uiCulture: "en-US");
                options.SupportedCultures = supportedCultures;
                options.SupportedUICultures = supportedCultures;
                // options.RequestCultureProviders = new List<IRequestCultureProvider> { new CookieRequestCultureProvider() };
            });

            services.AddMvc();
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {

            var localizationOption = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();

            app.UseRequestLocalization(localizationOption.Value);

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

            app.UseStaticFiles(); …
Run Code Online (Sandbox Code Playgroud)

c# routes attributerouting asp.net-core

2
推荐指数
1
解决办法
2224
查看次数