Ends in:
2 DAYS
2 HRS
21 MIN
20 SEC
Ends in:
2 D
2 H
21 M
20 S

Blazor UI: Localization

Blazor applications can reuse the same IStringLocalizer<T> service that is explained in the localization document.

All the localization resources and texts available in the server side are usable in the Blazor application.

IStringLocalizer

IStringLocalizer<T> (T is the localization resource class) can be injected in any service or component to use the localization service.

Razor Components

Use @inject IStringLocalizer<MyProjectNameResource> to use the localization in a razor component.

Example: Localization in a Razor Component

@page "/"
@using MyCompanyName.MyProjectName.Localization
@using Microsoft.Extensions.Localization
@inject IStringLocalizer<MyProjectNameResource> L

<h1>
    @L["LongWelcomeMessage"]
</h1>

L is a name that we love and use as the name of a IStringLocalizer instance, while you can give any name.

The AbpComponentBase

AbpComponentBase is a useful base class that you can derive the components from. It has some useful properties/methods you typically need in a component.

The AbpComponentBase already defines a base L property (of type IStringLocalizer). It only requires to set the resource type (in the constructor of the derived class). If you created your application from the ABP's application startup template, then you should have a YourProjectComponentBase class in the Blazor project. Inherit components from this class to have the localizer pre-injected.

Example: Derive from the base component class

@page "/"
@inherits MyProjectNameComponentBase

<h1>
    @L["LongWelcomeMessage"]
</h1>

Other Services

IStringLocalizer<T> can be injected into any service.

Example

public class MyService : ITransientDependency
{
    private readonly IStringLocalizer<TestResource> _localizer;

    public MyService(IStringLocalizer<TestResource> localizer)
    {
        _localizer = localizer;
    }

    public void Foo()
    {
        var str = _localizer["HelloWorld"];
    }
}

Format Arguments

Format arguments can be passed after the localization key. If your message is Hello {0}, welcome!, then you can pass the {0} argument to the localizer like _localizer["HelloMessage", "John"].

Refer to the Microsoft's localization documentation for details about using the localization.

See Also

Contributors


Last updated: July 31, 2024 Edit this page on GitHub

Was this page helpful?

Please make a selection.

To help us improve, please share your reason for the negative feedback in the field below.

Please enter a note.

Thank you for your valuable feedback!

Please note that although we cannot respond to feedback, our team will use your comments to improve the experience.

In this document
Community Talks

What’s New with .NET 9 & ABP 9?

21 Nov, 17:00
Online
Watch the Event
Mastering ABP Framework Book
Mastering ABP Framework

This book will help you gain a complete understanding of the framework and modern web application development techniques.

Learn More