I am currently developing a web application in ASP.NET MVC Core where users should register. This is a localized web application that should be able to run for multiple languages. To be SEO friendly, I've chosen for routed localization, so my url's look like: https://localhost:5001/en/Catalogue or https://localhost:5001/fr/catalogue.
To allow this, I added this piece of code in my ConfigureServices method in Startup.cs
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddViewLocalization()
.AddDataAnnotationsLocalization();
In my Configure method I added this:
IList<CultureInfo> supportedCultures = new List<CultureInfo>
{
new CultureInfo("en"),
new CultureInfo("fr"),
};
var localizationOptions = new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
};
var requestProvider = new RouteDataRequestCultureProvider();
localizationOptions.RequestCultureProviders.Insert(0, requestProvider);
app.UseRouter(routes =>
{
routes.MapMiddlewareRoute("{culture=en}/{*mvcRoute}", subApp =>
{
subApp.UseRequestLocalization(localizationOptions);
subApp.UseMvc(mvcRoutes =>
{
mvcRoutes.MapRoute(
name: "areaRoute",
template: "{culture=en}/{area:exists}/{controller=Home}/{action=Index}/{id?}");
mvcRoutes.MapRoute(
name: "default",
template: "{culture=en}/{controller=Home}/{action=Index}/{id?}");
});
});
});
This works like a charm. I can translate my MVC pages in any flavour I want. My problem is with the identiy pages. I added those pages as scaffolded items. Their URL's are pointing to https://localhost:5001/Identity/Account/Register. Trying to access them with https://localhost:44339/en/Identity/Account/Register does not work. How can I implement routed localization with identity pages?
To offer a website in multiple languages using ASP.Net we simply need to add some resource. resx files to our project and voilà. Based on the language of the browser, IIS will match the localization resource.
Globalization is the process of designing the application in such a way that it can be used by users from across the globe (multiple cultures). Localization, on the other hand, is the process of customization to make our application behave depending on the current culture and locale. These two things go together.
To opt-in to localization, we need to modify our Startup file. We'll be registering a few services, configuring options, and registering middleware. All steps that are common-place for additions in an ASP.NET application. Starting in our ConfigureServices method, we need to make a call to AddLocalization .
AddAreaFolderRouteModelConvention will do the magic:
services.AddMvc()
.AddRazorPagesOptions(options =>
{
options.Conventions.AddAreaFolderRouteModelConvention("Identity", "/Account/", model =>
{
model.Selectors.ForEach(x =>
{
if (x.AttributeRouteModel.Template.StartsWith("Identity"))
{
x.AttributeRouteModel = new AttributeRouteModel()
{
Order = -1,
Template = AttributeRouteModel.CombineTemplates(("{culture=en-US}"),
x.AttributeRouteModel.Template)
};
}
});
});
});
MSDN page says:
pageName String The page name e.g. /Users/List
The page name is the path of the file without extension, relative to the pages root directory for the specified area. e.g. the page name for the file Areas/Identity/Pages/Manage/Accounts.cshtml, is /Manage/Accounts.
So all pages are actually inside "/Account/", here are some generated links:
/en-us/identity/account/login
/en-us/identity/account/manage/index
/en-us/identity/account/manage/orders
if you don't like "/identity/" inside path, you can do this:
AttributeRouteModel.CombineTemplates(("{culture=en-US}"),
x.AttributeRouteModel.Template.Substring("Identity/".Length)) //<==Substring
Then all links will be:
/en-us/account/login
/en-us/account/manage/index
/en-us/account/manage/orders
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With