Skip to content

Using the SimpleLocalizer methods

Jon P Smith edited this page Jan 12, 2023 · 8 revisions

The ISimpleLocalizer service has methods to localize messages using a string or FormattableString. Thye are:

  • LocalizeString method, which handles string messages, e.g. "Welcome"
  • LocalizeFormatted method, which handles messages dynamic message, e.g. "The date is {DateTime.Now:d}"

NOTE: You can these being used in this view.

Getting an instance of the ISimpleLocalizer service

You can obtain an instance of the ISimpleLocalizer service via dependency injection (DI). There are two approaches.

Obtaining the registered ISimpleLocalizer service

The SimpleLocalizer feature is designed to make it quick and easy to use. Therefore it provides a service that uses one resource file group. which is set up during the registering of this service (see this section on how this works. The code below shows how to get an instance of the ISimpleLocalizer within a View, which is a typical use case.

@inject ISimpleLocalizer SimpleLocalizer

Because the resource class and the culture of the default messages are defined when the localizers are registered, then service contains these settings, which makes using the ISimpleLocalizer simple.

Obtaining ISimpleLocalizer service via the ISimpleLocalizerFactory service

In some rare cases you might want to create a ISimpleLocalizer with a resource different to the one you set at registration time. Therefore there is a ISimpleLocalizerFactory service that will do that for you. The code below shows how you would obtain the normal ISimpleLocalizer (shown as _normalSimpleLoc) and via the ISimpleLocalizerFactory service (shown as _createdSimpleLoc).

public class TestController : Controller
{
    private readonly ISimpleLocalizer _normalSimpleLoc;
    private readonly ISimpleLocalizer _createdSimpleLoc;

    public TestController(ISimpleLocalizer normalSimpleLoc,
          ISimpleLocalizerFactory simplefactory)
    {
        _normalSimpleLoc = normalSimpleLoc;
        _createdSimpleLoc = simplefactory.Create(typeof(TestController));
    }       
    // rest of code left out

Using the ISimpleLocalizer service in your application

The ISimpleLocalizer service has methods to show a string, where the content is static, or using FormattableString that let you provide dynamic parts, e.g. $"Date is {DateTime.Now:M}"

Localize a string message

The ISimpleLocalizer service two methods to localize / display a message in a string: one for normal use and one to use in a static method where this isn't available. The non-static version is shown below.

<label for="month">
    @(SimpleLocalizer.LocalizeString("Provide a string (can be null)", this))
</label>

NOTE: the static version would look like this SimpleLocalizer.StaticLocalizeString("Hello!", typeof(MyStaticClass))

Localize a FormattableString message

The ISimpleLocalizer service two methods to localize / display a message in a FormattableString: one for normal use and one to use in a static method where this isn't available. The non-static version is shown below.

<div>
    <a asp-action="Index">
       @(SimpleLocalizer.LocalizeFormatted($"Back to {Model.ControllerName} Home", this))
    </a>
</div>

NOTE: the static version would look like this SimpleLocalizer.StaticLocalizeFormatted($"Date is {DateTime.Now:M}", typeof(MyStaticClass))

How it finds messages in other languages

The method's take in a message in the language (known as default culture) that is set when the localizers are registered. But if the user's culture is different to the default culture, then the ISimpleLocalizer service will ask the .NET IStringLocalizer<TResource> to lookup the localized message held in the resource file. To do this it needs:

  1. A resource class to define the group of resource files to look at. This is defined when you registered the SimpleLocalizer.
  2. The user's culture, which is provided by the .NET localization code, to define the other part of the resource file name.
  3. The localize key, which is provided by the ISimpleLocalizer methods, which is used to lookup the Value in the selected resource file.

Parts 1 and 2 are already set, but the localize key is created by the method using the message and an Prefix, in the form {prefixName}(message). Here are some examples:

  • SimpleLocalizer.LocalizeString("Provide a string (can be null)", this) would create a localize key of "SimpleLocalizer(Provide a string (can be null))".
  • SimpleLocalizer.LocalizeFormatted($"Date is {DateTime.Now:M}", this) would create a localize key of SimpleLocalizer(Date is {0:M}.)".

NOTE: The default prefix is SimpleLocalizer, but you can change this when you register SimpleLocalizer.

Clone this wiki locally