Walkthrough: Creating a language switcher component
If you integrated your ASP.NET Core app before September 2024, it's using the legacy ASP.NET Core Rendering SDK, version 22 or earlier. This SDK is no longer receiving updates, so we recommend that you upgrade to the latest version of the new ASP.NET Core SDK.
This topic describes how to implement a language switching component in an ASP.NET Core rendering host, using built-in ASP.NET Core localization and routing functionality.
This walkthrough assumes you are configuring routing endpoints using the MapSitecoreLocalizedRoute extension or otherwise using the sitecoreRoute route value to indicate the item path to request from the Layout Service and the culture route value to indicate the desired language. For further information, see Sitecore Rendering Engine.
This walkthrough describes how to:
Create the View component
Create the View component
This example uses an ASP.NET Core view component to encapsulate the logic and presentation of the language switcher. You can then add the view component to the relevant location in your application.
To create the view component:
-
Create and start an ASP.NET Core based solution as described in the Getting Started guide.
-
In Visual Studio, in the
renderingproject, open theStartup.csclass.NoteSome changes are required in this class to make the ASP.NET Core
RequestLocalizationOptionsavailable to our language switcher through dependency injection. -
In the
ConfigureServicesmethod, add the following localization configuration registration, which enables three languages, in this example, German, English, and Japanese, as well as the Sitecore URL-based culture resolution: -
In the
Configuremethod, remove the existing call toapp.UseRequestLocalizationand replace it with a simpler one: -
In the
Modelsfolder of therenderingproject, add a new model class for the language switcher. This class outputs current and available languages. Add the following code in the class: -
Create a new folder in the
renderingproject calledViewComponents, and create a new View Component class inside the folder. This class populates the model based on the configured languages, current language, and the current route. Add the following code in the class: -
In the
renderingproject, create a new folder path for the component's view,Views/Shared/Components/LanguageSwitcher. Create a Razor View within the folder calledDefault.cshtml. This view renders our language switcher using the model. Add the following code in the view: -
In the
Views/_ViewImports.cshtmlView, add the following line to enable the use of custom View Components from this project as Tag Helpers: -
Add the language switcher to
Views/Default/Index.cshtml(the default View) under the Sitecore logo: -
Refresh the rendered site (
www.myproject.localhost) to see your changes. You can switch languages, but there is no content available in, for example, German or Japanese yet.
If you encounter an error, check the rendering host logs for compilation errors. In PowerShell, type the following: docker-compose logs -f rendering.
Add Sitecore languages and content
Add Sitecore languages and content
To add Sitecore languages and content:
-
Log in to the CM environment (
cm.myproject.localhost) and open the Content Editor from the Launchpad. -
Under
/sitecore/system/languages, add the languages you want, for example, German (Germany) and Japanese (Japan). This process creates language items forde-DEandja-JP. -
Navigate to the
/sitecore/content/MyProject/Home/Contentcontent item and create language versions for these new languages, and populate the Title field with relevant content, for example:-
German: Hallo!
-
Japanese: こんにちは
-
-
Publish the language additions and content changes.
-
Go to your rendered site. You can now see the language-specific content when you switch languages.
Configure serialization for language items (optional)
Configure serialization for language items (optional)
In a production solution, you must include the language items you just created in a serialization module. Because the en item comes out-of-the-box with Sitecore, you can use rules to ignore it.
To configure serialization for language items:
-
In
src\Items.module.json, add an include for/sitecore/system/Languages: -
To serialize the language items and your changes to the site content, run
dotnet sitecore ser pullfrom the Sitecore CLI.