Generates strongly typed classes for resources to be used by IStringLocalizer from Microsoft.Extensions.Localizer This is very much adapted (and a bunch copied) code from https://github.com/VocaDB/ResXFileCodeGenerator
This directory structure:
- MyViewModel.cs
- MyViewModel.resx
- MyViewModel.fr-ca.rex
- 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)
- ServiceCollections.g.cs
- Install Microsoft.Extensions.Localization
- Install Shiny.Extensions.Localization.Generator
- In your MauiProgram.cs, do the following
builder.Services.AddLocalization();
builder.Services.AddStronglyTypedLocalizations();
- Now inject the strongly typed classes
public class MyViewModel
{
public MyViewModel(MyViewModelLocalized localizer)
=> this.Localizer = localizer;
public MyViewModelLocalized Localizer { get; }
}
- Now bind (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