Microsoft.Extensions.Localization Strongly Typed Class Generator

A Roslyn incremental source generator that creates strongly typed classes from your .resx files for use with IStringLocalizer from Microsoft.Extensions.Localization.
📖 Documentation: shinylib.net/localizegen
This is very much adapted (and a bunch copied) code from https://github.com/VocaDB/ResXFileCodeGenerator
Features
- Strongly typed properties for every resource key - no more magic strings
- Methods with typed parameters for format strings (ie.
Hello {0}), including format specifiers like {0:C2} or {1:yyyy-MM-dd}
- XML documentation comments generated from the
.resx comment field (falls back to the resource value)
- Namespaces follow your project's
RootNamespace and folder structure
- Keys containing spaces, dots, or dashes are converted to safe member names
- A single
AddStronglyTypedLocalizations() extension to register everything with dependency injection
- Optional
internal accessor for generated classes
- Generated classes are
partial so you can extend them
- Works with any .NET project (the generator targets .NET Standard 2.0) - MAUI, Blazor, ASP.NET Core, console, etc.
How it Works
This directory structure:
- MyViewModel.cs
- MyViewModel.resx
- MyViewModel.fr-ca.resx
- Folder1\FolderViewModel.cs
- Folder1\FolderViewModel.resx
- Folder1\FolderViewModel.fr-ca.resx
Will generate:
- MyViewModelLocalized.g.cs (RootNamespace.MyViewModelLocalized)
- Folder1.FolderViewModelLocalized.g.cs (RootNamespace.Folder1.FolderViewModelLocalized)
- ServiceCollectionExtensions.g.cs
Only the default (culture neutral) .resx files generate classes - culture specific files such as MyViewModel.fr-ca.resx are picked up at runtime by IStringLocalizer.
Getting Started
- Install the packages
dotnet add package Microsoft.Extensions.Localization
dotnet add package Shiny.Extensions.Localization.Generator
- Register the generated classes in your
MauiProgram.cs, Program.cs, or wherever you configure services
builder.Services.AddStronglyTypedLocalizations(); // also calls AddLocalization() for you
- Add a
.resx file to your project that matches the name of a class (ie. ViewModel, Controller, Service, etc).
- Inject the strongly typed class
public class MyViewModel
{
public MyViewModel(MyViewModelLocalized localizer) // same namespace and class name with "Localized" suffix
=> this.Localizer = localizer;
public MyViewModelLocalized Localizer { get; }
}
- Bind to it (XAML intellisense will pick it up)
<Label Text="{Binding Localizer.MyKey}" />
[!WARNING]
If the "class" beside the resource does not exist, a compile error will occur with the generated code
Format Strings
Any resource value containing indexed placeholders generates a method instead of a property. The method name gets a Format suffix (unless the key already ends with Format), and placeholders with format specifiers are fully supported.
<data name="Greeting" xml:space="preserve">
<value>Hello {0}, you have {1} new messages</value>
</data>
<data name="TotalCost" xml:space="preserve">
<value>Total cost: {0:C2}</value>
</data>
<data name="WelcomeMessage" xml:space="preserve">
<value>Welcome!</value>
</data>
Generates:
public string GreetingFormat(object parameter0, object parameter1)
=> string.Format(this.localizer["Greeting"], parameter0, parameter1);
public string TotalCostFormat(object parameter0)
=> string.Format(this.localizer["TotalCost"], parameter0);
public string WelcomeMessage => this.localizer["WelcomeMessage"];
Notes:
- A placeholder used more than once (ie.
Start: {0:HH:mm}, End: {0:HH:mm:ss}) produces a single parameter
- Braces that aren't indexed placeholders (
{name}, {}) are left alone and still produce a property
XML Documentation from Comments
Add a comment to a resource in the .resx file and it becomes the <summary> of the generated member, so you get intellisense describing what the string is for. If there is no comment, the resource value is used instead.
<data name="WelcomeMessage" xml:space="preserve">
<value>Welcome to our app!</value>
<comment>Displayed on the home screen when user first opens the app</comment>
</data>
/// <summary>
/// Displayed on the home screen when user first opens the app
/// </summary>
public string WelcomeMessage => this.localizer["WelcomeMessage"];
Key Naming