Skip to content

Adding this library to a ASP.NET Core app

Jon P Smith edited this page Jan 9, 2023 · 9 revisions

Adding the Net.LocalizeMessagesAndErrors's DefaultLocalizer and SimpleLocalizer is done by two extension methods provided by the library. Here are the details of registering these localizers on startup.

Both localizers use the .NET localization IStringLocalizer<TResource> service to access languages different from the default culture provided when registering the IDefaultLocalizer<TResource> service. Note that if you don't set up the .NET localization, the the localizers still work, but will always provides the default messages. This is useful if you are building a NuGet package that uses LocalizeMessagesAndErrors localizers, as the package will work without having to set up the .NET localization and resource files.

Adding the IDefaultLocalizer service

Within the section of the Program class where services are registered to the .NET dependency injection stage you need to use the RegisterDefaultLocalizer extension method to register the IDefaultLocalizer<> and IDefaultLocalizerFactory services, as seen below.

builder.Services.RegisterDefaultLocalizer("en", supportedCultures);

You have to add information for the two parameters:

1. defaultCulture parameter

This sets the culture of the messages you provide. If the user's culture matches the defaultCulture, then the messages used with the DefaultLocalizer or SimpleLocalizer already contain the correct message.

2. supportedCultures parameter

This parameter is only used to control the warning is logged if a resource file/lookup fails on supported cultures. Here are the three options:

  1. You supply an array of culture names your app supports, e.g new[] { "en", "fr" }. This will only log a warning on these cultures. NOTE: You can find this list from the .NET localization setup (see Configure middleware code).
  2. You supply an empty array, e.g. Array.Empty<string>(). This means no warnings will be logged for fail lookups.
  3. You supply a null. This will log a ANY culture that either failed to lookup the key, or that culture isn't supported. This might get you some useful data on your users, but you might get a LOT of logs!

Adding the ISimpleLocalizer as a service

NOTE: The SimpleLocalizer relies on the IDefaultLocalizer<> being registered and the defaultCulture and supportedCultures settings are taken from the IDefaultLocalizer<> service.

Within the section of the Program class where services are registered to the .NET dependency injection stage you need to use the RegisterSimpleLocalizer extension method to register the ISimpleLocalizer and ISimpleLocalizerFactory services, as seen below.

builder.Services.RegisterSimpleLocalizer<HomeController>();

You need to define the TypeParam in the method which is used to define the Resource class. For instance, the registering code show above the SimpleLocalizer would use a IDefaultLocalizer<HomeController>, which defines part of the resource file's name.

By default the localizer key with be of the form "SimpleLocalizer({your message})", but you can alter the prefix of the localize key using the options. The code below would return a localizer key of "NewPrefix({your message})".

builder.Services.RegisterSimpleLocalizer<HomeController>(
    options => options.PrefixKeyString = "NewPrefix" );

If you set the PrefixKeyString to null, then the localize key will be the message, without a prefix and no brackets around the message.

Clone this wiki locally