Walkthrough: Configuring localization and content translation
The Sitecore Rendering Engine SDK offers support for globalization and localization, the same as in ASP.NET Core.
For further information about globalization and localization, see the official Microsoft ASP.NET Core documentation.
Minimal configuration is required to add globalization and localization in a .NET Core rendering host application built using the Rendering Engine SDK. Most of this configuration is done in the Startup
class of the rendering host application.
For further information about the Startup
class, see the official Microsoft App startup in ASP.NET Core documentation.
This walkthrough describes how to:
Configure localization
You must first configure the Rendering Engine to use localization:
-
Add the following code to the
Startup.cs
class:RequestResponsepublic void ConfigureServices(IServiceCollection services) { services.AddLocalization(options => options.ResourcesPath = "Resources"); services.AddSitecoreRenderingEngine(options => { options.AddPartialView("Sample-Layout", "_SampleLayout") .AddDefaultPartialView("_ComponentNotFound"); }); }
Configure the localization middleware
The current culture on a request is set in the localization middleware.
To configure the localization middleware:
-
In the
Startup.cs
class, in theConfigure
method, addcallapp.UseRequestLocalization(options =>{})
.RequestResponsepublic void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseRouting(); app.UseStaticFiles(); app.UseRequestLocalization(options => { var supportedCultures = new List<CultureInfo> { new CultureInfo("en"), new CultureInfo("da"), new CultureInfo("da-DK") }; options.DefaultRequestCulture = new RequestCulture(culture: "da", uiCulture: "da"); options.SupportedCultures = supportedCultures; options.SupportedUICultures = supportedCultures; options.UseSitecoreRequestLocalization(); }); app.UseSitecoreRenderingEngine(); }
NoteThe
UseSitecoreRequestLocalization()
method allows culture to resolve from both thesc_lang
query string and the culture token from route data (for example,en-us
).If you do not invoke the
UseRequestLocalization
method, the language specified inWithDefaultRequestOptions
method is used instead. -
In
Startup.cs
, in theapp.UseEndpoints(endpoints =>{})
call in theConfigure
method, add the following to configure endpoints and add localized routes:RequestResponseapp.UseEndpoints(endpoints => { endpoints.AddLocalizedRoute("Localized", "Index", "Sitecore"); endpoints.MapFallbackToController("Index", "Sitecore"); });
The
AddLocalizedRoute
method accepts three parameters: the route name, default action, and controller name. It maps the controller route using the following pattern:RequestResponse/{culture:culture}/{**sitecoreRoute}
If you want custom routing, you can find the
culture
andsitecoreRoute
route values in theRouteValues
collection ofHttpRequest
objects. See Routing in ASP.NET Core.
Configure method overview
The following is an example of the full Configure
method in Startup.cs
once you have configured the localization middleware:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseStaticFiles();
app.UseRequestLocalization(options =>
{
var supportedCultures = new List<CultureInfo> { new CultureInfo("en"), new CultureInfo("da"), new CultureInfo("da-DK") };
options.DefaultRequestCulture = new RequestCulture(culture: "da", uiCulture: "da");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
options.UseSitecoreRequestLocalization();
});
app.UseSitecoreRenderingEngine();
app.UseEndpoints(endpoints =>
{
endpoints.AddLocalizedRoute("Localized", "Index", "Sitecore");
endpoints.MapFallbackToController("Index", "Sitecore");
});
}
See the getting started template for a working example of a rendering host with localization enabled.