Skip to content

lepoco/i18n

Lepo.i18n logo

Lepo.i18n

Internationalization library for .NET applications.

Add multilingual support to WPF, WinForms, or CLI apps. JSON, YAML, and RESX resources with Dependency Injection.

NuGet NuGet Downloads GitHub stars License

Created in Poland by Leszek Pomianowski and open-source community.


Table of Contents


Why This Library?

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'}" />

Get Started

Install the Packages

dotnet add package Lepo.i18n

Pick 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.Wpf

NuGet: https://www.nuget.org/packages/Lepo.i18n


How to Use

1. Register Localizations

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"));
});

2. Localize in XAML

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>

3. Change Culture at Runtime

WPF Application
Application.Current.SetLocalizationCulture(new CultureInfo("en-US"));
With ILocalizationCultureManager (DI)
public class SettingsViewModel(ILocalizationCultureManager cultureManager)
{
    public void SwitchToEnglish()
    {
        cultureManager.SetCulture("en-US");
    }
}

Packages

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)

API Reference

LocalizationBuilder Methods

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

WPF XAML Extensions

Extension Description
{i18n:StringLocalizer 'key'} Localize a string in XAML
{i18n:PluralStringLocalizer ...} Localize with singular/plural forms

Culture Management

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

Maintainers


Support

For support, please open a GitHub issue. We welcome bug reports, feature requests, and questions.


License

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.

About

Convenient localization of Windows applications with YAML files.

Topics

Resources

License

MIT, MIT licenses found

Licenses found

MIT
LICENSE
MIT
LICENSE.md

Code of conduct

Contributing

Stars

Watchers

Forks

Sponsor this project

 

Contributors

Languages