I have implemented localization in .Net Core 3.1 as per https://learn.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-3.1. It's working fine on local using QueryString & Accept-Language HTTP header on local but after deployment not working with Accept-Language HTTP header for other cultures except english. Code Changes
// in ConfigureServices
services.AddRazorPages()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization()
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = GetSupportedCultures();
options.DefaultRequestCulture = new RequestCulture("en");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
// in configure method
app.UseRequestLocalization(app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>().Value);
// helper method
private IList<CultureInfo> GetSupportedCultures()
{
var cultures = Configuration.GetValue<string>("SupportedCultures")?.Split(",");
var supportedCultures = new List<CultureInfo>();
foreach (var culture in cultures)
{
supportedCultures.Add(new CultureInfo(culture.Trim()));
}
return supportedCultures;
}
// in appsettings.json
"SupportedCultures": "en,fr,de,el,es"
Request Header in browser network tab
Accept-Language: fr-FR,fr-CA;q=0.9,fr;q=0.8,de;q=0.7,en;q=0.6,es;q=0.5,el;q=0.4,en-GB;q=0.3,en-US;q=0.2
The code looks good to me. Did you check whether you are getting Accept-Language
HTTP headers from your request after deployment? If you are not getting it then you need to check whether you are using any proxy server or CDN.
If that is the case, you are required to whitelist the Accept-Language
in headers as per below: