Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use UseRequestLocalization in ASP .Net Core 3?

I need to have a multi-language web application. I was using my code in .net core 2.2 and everything was good. When I migrated to .net core 3 I face to some issues that one of them was using UseRequestLocalization.

I'm using this code in Configure method of startups.cs and after the running project, I see an empty page.

var supportedCultures = new CultureInfo[] {
    new CultureInfo ("en-US"),
    new CultureInfo ("en"),
    GetPersianCulture ("fa-IR"),
    GetPersianCulture ("fa"),
};
app.UseRequestLocalization(new RequestLocalizationOptions
{
    DefaultRequestCulture = new RequestCulture("fa"),
    SupportedCultures = supportedCultures,
    SupportedUICultures = supportedCultures,        
});
like image 823
Bahman Shafiei Avatar asked Jul 13 '19 07:07

Bahman Shafiei


People also ask

How can I get browser language in ASP.NET Core?

Declaration of supported languages is defined in Configure() of your Startup class (see example). If you still need all accepted languages as a simple string[] like the older Request. UserLanguages property, then use the HeaderDictionaryTypeExtensions. GetTypedHeaders() extension defined in the Microsoft.

What is middle ware in .NET Core?

Middleware is software that's assembled into an app pipeline to handle requests and responses. Each component: Chooses whether to pass the request to the next component in the pipeline. Can perform work before and after the next component in the pipeline.


1 Answers

Issue with globalization asp.net core 3.1, that's how I solved it

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{    
    var supportedCultures = new string[] { "en-GB", "en-US" };
    app.UseRequestLocalization(options =>
                options
                .AddSupportedCultures(supportedCultures)
                .AddSupportedUICultures(supportedCultures)
                .SetDefaultCulture("en-GB")
                .RequestCultureProviders.Insert(0, new CustomRequestCultureProvider(context =>
                {
                    return Task.FromResult(new ProviderCultureResult("en-GB"));
                }))
        );
like image 66
dnxit Avatar answered Nov 09 '22 12:11

dnxit