I tried to add multi language feature to my asp.net-core project but there are some changes between .net 3.1 and 5.0 in RequestLocalization and i couldn't get what i want. I added Resource files for each language and I used Resource in my razor pages, its working but there is one unwanted default route bug and i want my routing to work friendly for default culture.
This is what i want,
For default culture (Turkish):
site.com/foo
site.com/foo/bar
site.com/foo/bar/5
For non-default culture (English):
site.com/en/foo
site.com/en/foo/bar
site.com/en/foo/bar/5
My other problem is; My project renders site.com/foo/foo/bar this url like site.com/tr/foo/bar it's not okay and i guess it should redirect to 404 page.
My Startup sample code below:
public void ConfigureServices(IServiceCollection services)
{
services.AddResponseCompression();
services.AddLocalization(opts => opts.ResourcesPath = "Resources");
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo("tr-TR"),
new CultureInfo("en")
};
options.DefaultRequestCulture = new RequestCulture("tr");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
options.RequestCultureProviders.Insert(0, new RouteDataRequestCultureProvider());
});
services.AddControllersWithViews();
services.AddRazorPages();
services.AddRouting(options => options.LowercaseUrls = true);
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseResponseCompression();
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.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
var supportedCultures = new string[] { "tr-TR", "en" };
app.UseRequestLocalization(options =>
options
.AddSupportedCultures(supportedCultures)
.AddSupportedUICultures(supportedCultures)
.SetDefaultCulture("tr-TR")
.RequestCultureProviders.Insert(0, new CustomRequestCultureProvider(context => Task.FromResult(new ProviderCultureResult("tr-TR"))))
);
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(name: "culture-route", pattern: "{culture}/{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(name: "default", "{culture=tr}/{controller=Home}/{action=Index}/{id?}");
});
}
Razor Resource usage and culture change navs
Resource files
How can I solve this or what am I doing wrong?
EDIT
I found this approach. It's using CookieRequestCultureProvider and there is no culture info in url but at least there are no corupted urls. I don't know if this is okay for SEO.