# 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]\(slug://develop-widgets-with-asp.net-core > layout-and-style > 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](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-8.0).

> info **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](https://www.progress.com/documentation/sitefinity-cms/sitefinity-installation).
> * Create a German version of version of the page where you want to paste the Localized widget.\
>   For more information, see [Translate a page](https://www.progress.com/documentation/sitefinity-cms/tutorial-translate-a-page).

> info **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

<SfDetails summary="Create the layout file">

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:

   ```Razor
   @model Progress.Sitefinity.AspNetCore.Models.PageModel
   @using Progress.Sitefinity.AspNetCore.Mvc.Rendering

   @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
   @addTagHelper *, Progress.Sitefinity.AspNetCore

   @inject Progress.Sitefinity.AspNetCore.Web.IRequestContext requestContext;

   <!DOCTYPE html>
   <html id="html" lang="@requestContext.Culture">
   <head>
       <meta charset="utf-8" />
       <meta name="viewport" content="width=device-width, initial-scale=1.0" />
       <link rel="icon" type="image/x-icon" href="favicon.ico">

       @Html.RenderSeoMeta(this.Model)

       <environment include="Development">
             <link rel="stylesheet" href="@Progress.Sitefinity.AspNetCore.Constants.PrefixRendererUrl("Styles/bootstrap.css")" />
       </environment>
       <environment exclude="Development">
          <link rel="stylesheet" href="@Progress.Sitefinity.AspNetCore.Constants.PrefixRendererUrl("Styles/bootstrap.min.css")" />
       </environment>

       @* Scripts for the OOB Sitefinity widgets *@
       <link rel="stylesheet" href="@Progress.Sitefinity.AspNetCore.Constants.PrefixRendererUrl("styles/main.css")" type="text/css" />

       @* Custom styles *@
       <link rel="stylesheet" href="~/styles/styles.css" />

       @* Custom scripts *@
       <script src="~/scripts/scripts.js"></script>
   </head>

   <body class="container-fluid">
       @RenderSection("Scripts")
   </body>
   </html>
   ```

</SfDetails>

> info **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.

<SfDetails>

<SfDetailsSummary>

1. Create the `ViewComponent`

</SfDetailsSummary>

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

```C#
using System;
using Microsoft.AspNetCore.Mvc;
using Progress.Sitefinity.AspNetCore.ViewComponents;
using Microsoft.Extensions.Localization;

namespace Renderer.ViewComponents
{
    /// <summary>
    /// Test widget with different kind of restrictions for its properties.
    /// </summary>
    [SitefinityWidget]
    public class LocalizedViewComponent : ViewComponent
    {
        private IStringLocalizer<LocalizedViewComponent> localizer;

        public LocalizedViewComponent(IStringLocalizer<LocalizedViewComponent> localizer)
        {
            this.localizer = localizer;
        }

        /// <summary>
        /// Invokes the view.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public IViewComponentResult Invoke()
        {
            // when a Sitefinity page is executed the CultureInfo.CurrentUICulture is automatically populated
            // with the culture the page is translated on, so the string here would be resolved automatically
            var localizedString = this.localizer.GetString("Hello World !");
            return this.View("Default", localizedString.Value);
        }
    }
}
```

</SfDetails>

<SfDetails>

<SfDetailsSummary>

1. Create the German `ViewComponent`

</SfDetailsSummary>

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

```XML
<?xml version="1.0" encoding="utf-8"?>
<root>
  
  <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
    <xsd:element name="root" msdata:IsDataSet="true">
      <xsd:complexType>
        <xsd:choice maxOccurs="unbounded">
          <xsd:element name="metadata">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0" />
              </xsd:sequence>
              <xsd:attribute name="name" use="required" type="xsd:string" />
              <xsd:attribute name="type" type="xsd:string" />
              <xsd:attribute name="mimetype" type="xsd:string" />
              <xsd:attribute ref="xml:space" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="assembly">
            <xsd:complexType>
              <xsd:attribute name="alias" type="xsd:string" />
              <xsd:attribute name="name" type="xsd:string" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="data">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
                <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
              </xsd:sequence>
              <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
              <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
              <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
              <xsd:attribute ref="xml:space" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="resheader">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
              </xsd:sequence>
              <xsd:attribute name="name" type="xsd:string" use="required" />
            </xsd:complexType>
          </xsd:element>
        </xsd:choice>
      </xsd:complexType>
    </xsd:element>
  </xsd:schema>
  <resheader name="resmimetype">
    <value>text/microsoft-resx</value>
  </resheader>
  <resheader name="version">
    <value>2.0</value>
  </resheader>
  <resheader name="reader">
    <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
  </resheader>
  <resheader name="writer">
    <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
  </resheader>
  <data name="Hello World !" xml:space="preserve">
    <value>Hallo Welt !</value>
  </data>
</root>
```

</SfDetails>

<SfDetails>

<SfDetailsSummary>

3. Create the `View`

</SfDetailsSummary>

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

```Razor
@model string
@using Microsoft.AspNetCore.Mvc.Localization

@inject IViewLocalizer Localizer;

<h1>Localized string passed from view component: @Model</h1>

<h1>Localized string in view: @Localizer["Hello World !"]</h1>
```

</SfDetails>

<SfDetails>

<SfDetailsSummary>

4. Create the German `View`

</SfDetailsSummary>

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

```XML
<?xml version="1.0" encoding="utf-8"?>
<root>
  
  <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
    <xsd:element name="root" msdata:IsDataSet="true">
      <xsd:complexType>
        <xsd:choice maxOccurs="unbounded">
          <xsd:element name="metadata">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0" />
              </xsd:sequence>
              <xsd:attribute name="name" use="required" type="xsd:string" />
              <xsd:attribute name="type" type="xsd:string" />
              <xsd:attribute name="mimetype" type="xsd:string" />
              <xsd:attribute ref="xml:space" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="assembly">
            <xsd:complexType>
              <xsd:attribute name="alias" type="xsd:string" />
              <xsd:attribute name="name" type="xsd:string" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="data">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
                <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
              </xsd:sequence>
              <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
              <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
              <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
              <xsd:attribute ref="xml:space" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="resheader">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
              </xsd:sequence>
              <xsd:attribute name="name" type="xsd:string" use="required" />
            </xsd:complexType>
          </xsd:element>
        </xsd:choice>
      </xsd:complexType>
    </xsd:element>
  </xsd:schema>
  <resheader name="resmimetype">
    <value>text/microsoft-resx</value>
  </resheader>
  <resheader name="version">
    <value>2.0</value>
  </resheader>
  <resheader name="reader">
    <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
  </resheader>
  <resheader name="writer">
    <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
  </resheader>
  <data name="Hello World !" xml:space="preserve">
    <value>Hallo Welt !</value>
  </data>
</root>
```

</SfDetails>

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](https://www.progress.com/documentation/sitefinity-cms/assets/e29055a3de4ae41b7fc898d3865458cf/localization.jpg "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](https://github.com/Sitefinity/sitefinity-aspnetcore-mvc-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.
