Localizing an ASP.NET Core Blazor website using VS Code
The standard way of localizing .NET applications is using .resx files, which is very easy to do in Visual Studio. It’s also possible to do this using Visual Studio Code, however this tooling doesn’t come by default with the C# extension for Visual Studio Code, which means you need an extension and a bit more work.
In this tutorial I will assume that you have run the dotnet new blazorserver command, which will bootstrap a Blazor project for you.
IDE setup
All you need to do is to install the ResX Editor extension by Dominic Vonk, which will allow you to edit .resx files in VS Code.
Create resource files
Now, create a Resources folder on your project and create a file called SharedResources.resx. You could also create one resource file per page, e.g. Home.resx, Register.resx, etc. You can now add as many keys to this file as you wish.

For each .resx file you make, you must also create a .cs file with the same name (e.g. Home.resx -> Home.cs) in the same folder. Add the following code to the new file, replacing the namespace and the class name to match the file name:
namespace MyApp.Resources;
public class SharedResources
{
}
This empty class is needed because VS Code doesn’t generate it automatically for us like Visual Studio does. It can be left empty because all we care about is the name, which the localization extension will use to find the .resx file.
Builder configuration
Next, you must register the required services in your app’s builder:
var builder = WebApplication.CreateBuilder(args);
// ...
builder.Services.AddLocalization();
Localizing Blazor components
In order to use the string localizer in Blazor components all you must first add the following line to the _Imports.razor file:
@using Microsoft.Extensions.Localization
@using MyApp.Resources
This will expose the localization extension and your resource classes on all Razor files, which means that you won’t have to add these @usings every time you want to localize a string.
Next, you must add the following line to the top of your Blazor component file, replacing SharedResources with whatever name you chose earlier:
@inject IStringLocalizer<SharedResources> Loc
Finally, in order to get a localized string all you need to do is Loc["Foo"], for example:
<h1>The following text is translated: @Loc["Foo"]</h1>
Adding more languages
Whenever the localizer requests a string on a specific culture it will look for a resource file whose name ends with the culture name, e.g. SharedResources.en-US.resx. If this file can’t be found, it will fall back to the file that has no culture name in it. This means that all you have to do to translate a string is to create another .resx file next to it that includes the culture name just before the .resx extension, e.g. .en-US.resx (there’s no need to create a C# file for these .resx files).
Check the Microsoft documentation for Blazor globalization for more info, for example to change how the localizer determines what culture to use.
Comments
Post a Comment