Asp.net核心本土化文化

时间:2017-08-02 15:22:16

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

我想将默认文化“en”更改为“vi”。

但它不起作用。

  • 查看

_Layout.cshtml

    <div>
        <a href="/?culture=vi" style="margin-right: 16px;">Tiếng Việt</a>
        <a href="/?culture=en">English</a>
    </div>
    <div style="padding:12px;">
        <h2>@SharedLocalizer["Welcome"]</h2>
    </div>

index.cshtml

    @inject IViewLocalizer Localizer;

    <div style="padding:32px;">
       <h1>Value: @Localizer["Value"]</h1>
    </div>
  • Startup.cs

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddLocalization(options => options.ResourcesPath = "Resources");
            services.AddMvc().AddViewLocalization().AddDataAnnotationsLocalization();
        }
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            List<CultureInfo> supportedCultures = new List<CultureInfo>
            {
                new CultureInfo("vi"),
                new CultureInfo("en"),
            };
    
            app.UseRequestLocalization(new RequestLocalizationOptions
            {
                DefaultRequestCulture = new RequestCulture("vi"),
                SupportedCultures = supportedCultures,
                SupportedUICultures = supportedCultures
            });
    
            app.UseStaticFiles();
    
            app.UseMvc(routes =>
            {
                routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
            });
        }
    
    • 来源

SharedResources.en.resx

SharedResources.vi.resx

Views.Home.Index.en.resx

Views.Home.Index.vi.resx

0 个答案:

没有答案