locked
How can I use globalization and localization in .net core 3.0 preview8? RRS feed

  • Question

  • User1052024640 posted

    I used globalization and localization in .net core 2.2 yet and now wanna migrate it to .net core 3.0 preview8.

    Here is my code in .net core 2.2:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
    if (env.IsDevelopment())
    {
    //app.UseBrowserLink();
    app.UseDeveloperExceptionPage();
    }
    else
    {
    app.UseExceptionHandler("/Error");
    app.UseHttpsRedirection();
    }

    var SupportedCultures = new List<CultureInfo> {
    new CultureInfo("en"),
    new CultureInfo("zh-Hans"),
    new CultureInfo("zh-Hant")
    };
    var options = new RequestLocalizationOptions
    {
    DefaultRequestCulture = new RequestCulture("en"),
    SupportedCultures = SupportedCultures,
    SupportedUICultures = SupportedCultures
    };
    app.UseRequestLocalization(options);
    app.UseStaticFiles();

    app.UseStatusCodePagesWithReExecute("/StatusCode/{0}");


    var requestProvider = new RouteDataRequestCultureProvider();
    options.RequestCultureProviders.Insert(0, requestProvider);

    app.UseRouter(routes =>
    {
    routes.MapMiddlewareRoute("{culture=en}/{*mvcRoute}", subApp =>
    {
    subApp.UseRequestLocalization(options);

    subApp.UseMvc(mvcRoutes =>
    {
    mvcRoutes.MapRoute(
    name: "default",
    template: "{culture=en}/{controller=Home}/{action=Index}");
    // map the .html suffix
    mvcRoutes.MapRoute(
    name: "home.html",
    template: "{culture=en}/{controller=Home}/{action}.html");
    });
    });
    });
    }

    And here is my code in .net core 3.0

    [Obsolete]
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
    if (env.IsDevelopment())
    {
    app.UseDeveloperExceptionPage();
    }
    else
    {
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHttpsRedirection();
    }

    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseCookiePolicy();

    var SupportedCultures = new List<CultureInfo> {
    new CultureInfo("en"),
    new CultureInfo("zh-Hans"),
    new CultureInfo("zh-Hant")
    };
    var options = new RequestLocalizationOptions
    {
    DefaultRequestCulture = new RequestCulture("zh-Hans"),
    SupportedCultures = SupportedCultures,
    SupportedUICultures = SupportedCultures
    };
    app.UseRequestLocalization(options);
    var requestProvider = new RouteDataRequestCultureProvider();
    options.RequestCultureProviders.Insert(0, requestProvider);
    app.UseStaticFiles();

    app.UseStatusCodePagesWithReExecute("/StatusCode/{0}");

    app.UseRouting();
    app.UseCors();
    app.UseAuthentication();
    app.UseAuthorization();
    app.UseEndpoints(endpoints =>
    {
    endpoints.MapControllerRoute(
    name: "default",
    pattern: "{culture=zh-hans}/{controller=Home}/{action=Index}");
    endpoints.MapControllerRoute(
    name: "html",
    pattern: "{culture=zh-hans}/{controller=Home}/{action}.html");

    });

    }

    The MVC Controller and any other things change a lot and I don't know how to migrate it.

    Now the problem is the default language of globalization and localization is 'zh-hans' but not in .net core 3.0 is English. What's more, it seems the globalization and localization do not work also.

    The code I wrote before is for changing the globalization and localization by URL, for example, www.microsoft.com/en/ for English, www.microsoft.com/zh-hans for Chinese.

    How can I solve it? Would you please help me? Thank you.

    Friday, August 23, 2019 3:48 AM

Answers

  • User1052024640 posted

    Finally, I found a solution by editing the code like this:

    app.UseRouter(routes =>
    {
    routes.MapMiddlewareRoute("{culture=zh-hans}/{*mvcRoute}", subApp =>
    {
    subApp.UseRequestLocalization(options);
    subApp.UseRouting();
    subApp.UseEndpoints(mvcRoutes =>
    {
    mvcRoutes.MapControllerRoute("default", "{culture=zh-hans}/{controller=Home}/{action=Index}");
    mvcRoutes.MapControllerRoute("home.html", "{culture=zh-hans}/{controller=Home}/{action}.html");
    });
    });
    });

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, August 24, 2019 5:38 AM

All replies