Add multilingual support to WPF, WinForms, or CLI apps. JSON, YAML, and RESX resources with Dependency Injection.
Created in Poland by Leszek Pomianowski and open-source community.
- Table of Contents
- Why This Library?
- Get Started
- How to Use
- Packages
- API Reference
- Maintainers
- Support
- License
Traditional .NET localization with resource files requires ceremony and scattered configuration:
// Traditional approach - manual resource loading
ResourceManager rm = new("MyApp.Resources.Strings", typeof(Program).Assembly);
CultureInfo culture = new("pl-PL");
string translated = rm.GetString("HelloWorld", culture) ?? "Hello World";With Lepo.i18n, localizations are registered once and consumed anywhere - including directly in XAML:
// Lepo.i18n with Dependency Injection
services.AddStringLocalizer(b =>
{
b.SetCulture("pl-PL");
b.FromResource<Translations>(new CultureInfo("pl-PL"));
b.FromResource<Translations>(new CultureInfo("en-US"));
});<!-- Bind directly in XAML -->
<TextBlock Text="{i18n:StringLocalizer 'Hello World'}" />dotnet add package Lepo.i18nPick additional packages based on your needs:
# Microsoft.Extensions.DependencyInjection integration
dotnet add package Lepo.i18n.DependencyInjection
# Load translations from JSON files
dotnet add package Lepo.i18n.Json
# WPF markup extensions
dotnet add package Lepo.i18n.WpfNuGet: https://www.nuget.org/packages/Lepo.i18n
WPF Application (without DI)
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
this.UseStringLocalizer(b =>
{
b.SetCulture(new CultureInfo("pl-PL"));
b.FromResource<Translations>(new CultureInfo("pl-PL"));
b.FromResource<Translations>(new CultureInfo("en-US"));
});
}
}Generic Host with Dependency Injection
IHost host = Host.CreateDefaultBuilder()
.ConfigureServices((context, services) =>
{
services.AddStringLocalizer(b =>
{
b.SetCulture("pl-PL");
b.FromResource<Translations>(new CultureInfo("pl-PL"));
b.FromJson(assembly, "Resources.Translations-en.json", new CultureInfo("en-US"));
});
})
.Build();Loading from JSON files
services.AddStringLocalizer(b =>
{
b.SetCulture("en-US");
b.FromJson(assembly, "Resources.en-US.json", new CultureInfo("en-US"));
b.FromJson(assembly, "Resources.pl-PL.json", new CultureInfo("pl-PL"));
});Add the i18n XML namespace and use the StringLocalizer markup extension:
<Window
xmlns:i18n="http://schemas.lepo.co/i18n/2022/xaml">
<StackPanel>
<!-- Simple localization -->
<TextBlock Text="{i18n:StringLocalizer 'Hello World'}" />
<!-- With string format arguments -->
<TextBlock Text="{i18n:StringLocalizer 'Test {0}, of {1}!', Arg1='What?', Arg2='No'}" />
<!-- In any content property -->
<CheckBox Content="{i18n:StringLocalizer 'Enable notifications'}" />
</StackPanel>
</Window>WPF Application
Application.Current.SetLocalizationCulture(new CultureInfo("en-US"));With ILocalizationCultureManager (DI)
public class SettingsViewModel(ILocalizationCultureManager cultureManager)
{
public void SwitchToEnglish()
{
cultureManager.SetCulture("en-US");
}
}| Package | Description |
|---|---|
Lepo.i18n |
Core library - localization builder, provider, and YAML support |
Lepo.i18n.DependencyInjection |
IServiceCollection integration with IStringLocalizer |
Lepo.i18n.Json |
Load translations from embedded or external JSON files |
Lepo.i18n.Wpf |
WPF markup extensions (StringLocalizer, PluralStringLocalizer) |
| Method | Description |
|---|---|
SetCulture(CultureInfo) |
Set the default culture |
SetCulture(string) |
Set the default culture by name |
AddLocalization(LocalizationSet) |
Add a localization set manually |
FromResource<T>(CultureInfo) |
Load from embedded RESX resource |
FromJson(path, CultureInfo) |
Load from an embedded JSON file |
FromJson(assembly, path, CultureInfo) |
Load from a JSON file in a specific assembly |
FromYaml(assembly, path, CultureInfo) |
Load from an embedded YAML file |
| Extension | Description |
|---|---|
{i18n:StringLocalizer 'key'} |
Localize a string in XAML |
{i18n:PluralStringLocalizer ...} |
Localize with singular/plural forms |
| Method | Description |
|---|---|
app.UseStringLocalizer(Action) |
Configure localizations for a WPF app |
app.SetLocalizationCulture(CultureInfo) |
Change culture at runtime (WPF) |
services.AddStringLocalizer(Action) |
Register localizations via DI |
ILocalizationCultureManager.SetCulture() |
Change culture via DI service |
- Leszek Pomianowski (@pomianowski)
For support, please open a GitHub issue. We welcome bug reports, feature requests, and questions.
This project is licensed under the terms of the MIT open source license. Please refer to the LICENSE file for the full terms.
You can use it in private and commercial projects. Keep in mind that you must include a copy of the license in your project.
