Sample: Localized widget

Overview

This project demonstrates how to create a localized widget. The Localized widget renders its content in the default language or in German, depending on which language version of the page is requested.
The project is based on the start template sample. For more information, see Sample: Starter template.

When the Renderer attempts to render a page, the culture for that page is passed along as information to the Renderer. The Renderer uses this culture to set the CultureInfo.CurrentUICulture and CultureInfo.CurrentCulture properties to match the value of the Sitefinity page.

The API for localized strings is based on the out-of-the-box API for developing ASP.NET Core applications. For more information, see Globalization and localization in ASP.NET Core.

PREREQUISITES: Before implementing the sample, you must:
  • Set up a Sitefinity renderer application and connect it to your Sitefinity CMS application.
    For more information, see Install Sitefinity in ASP.NET Core mode.
  • Create a German version of version of the page where you want to paste the Localized widget.
    For more information, see Translate a page.

NOTE: The instructions in this sample use Visual Studio 2022 and a Sitefinity renderer project named Renderer.

Create the folder structure

Under your Renderer project, you must create the following folders:

  • ViewComponents
  • Views/Shared/Components/Localized

Create a custom layout file

You must register the custom scripts or styles in the _Layout.cshtml file. You must then use this custom base layout for every page where the Localized widget is placed

Perform the following:

  1. In the context menu of folder Views/Shared, click Add » Class…
  2. Select Code File.
  3. In Name, enter _Layout.cshtml and click Add.
  4. In the file, paste the following code and save your changes:

NOTE: If you have any custom scripts or styles, place them in folders wwwroot/scripts/scripts.js and wwwroot/styles/styles.css.

Create the widget

In Visual Studio, open the Renderer application.

  • In the context menu of folder ViewComponents, click Add » Class…
  • In Name, enter LocalizedViewComponent.cs and click Add.
  • In the class, paste the following code and save your changes:

  • In the context menu of folder ViewComponents, click Add » Class…
  • In Name, enter LocalizedViewComponent.de.resx and click Add.
  • In the class, paste the following code and save your changes:

  • In the context menu of folder Views/Shared/Components/Localized, click Add » Class…
  • Select Code File.
  • In Name, enter Default.cshtml and click Add.
  • In the class, paste the following code and save your changes:

  • In the context menu of folder Views/Shared/Components/Localized, click Add » Class…
  • Select Code File.
  • In Name, enter Default.de.resx and click Add.
  • In the class, paste the following code and save your changes:

Build your solution.

Result

When you open your Renderer application and open the New editor, you will see the Localized widget in the widget selector. When you add the widget on your page and click the Custom empty text link, you can enter a text string that will be displayed on the frontend.

Localization

Run the sample

This sample is available in Sitefinity’s GitHub repository. You can run and play with it.
To do this, perform the following:

  1. Go to Sitefinity’s GitHub repository Sitefinity ASP.NET Core samples.
  2. Expand Code and click Download ZIP.
  3. Extract the files on your computer.
  4. In the extracted folder, navigate to sitefinity-aspnetcore-mvc-samples-master/src/localization folder.
  5. Open the localization.sln in Visual Studio.
  6. Open the appsettings.json file.
  7. In section “Sitefinity”, change the “Url” property to the URL of your Sitefinity CMS site.
    If you have deployed Sitefinity CMS on the IIS, point to “https://localhost:<https_port>".
  8. In Visual Studio, in the context menu of localization project, click View in Browser.
  9. Log in to your Sitefinity CMS instance and place the widget on a page.

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Get started with Integration Hub | Sitefinity Cloud | Sitefinity SaaS

This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.

Web Security for Sitefinity Administrators

This free lesson teaches administrators the basics about protecting yor Sitefinity instance and its sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?