Yet another localization library with its own abstractions gives the possibility to create localization infrastructure with examples. It contains basic Json Localization Provider implementation (other types will be added in the future).
Before using the library, install following Nuget packages:
Name | Description | Version |
---|---|---|
TekDeq.Localization.Core | Core library | |
TekDeq.Localization.DependencyInjection | Microsoft DI extensions | |
TekDeq.Localization.Avalonia | Avalonia Localization and Extensions |
To create additional Localization Providers, please look at ILocalizationProvider and LocalizationProviderBase abstractions. As an example of usage please look at JsonLocalizationProvider and AvaloniaJsonLocalizationProvider implementation. If you are using dependency injection in your project, an example of the usage could be found in App.axaml.cs of the demo project.
public override void OnFrameworkInitializationCompleted()
{
var host = Host.CreateDefaultBuilder()
.ConfigureServices((context, services) =>
{
services.UseMicrosoftDependencyResolver();
// Initialize Splat
var resolver = Locator.CurrentMutable;
resolver.InitializeSplat();
resolver.InitializeReactiveUI();
// Register Views and ViewModels
services.AddTransient<MainWindow>();
services.AddTransient<MainWindowViewModel>();
// Register Localization
services.AddLocalization<AvaloniaJsonLocalizationProvider>(() =>
{
var options = new AvaloniaLocalizationOptions(
// cultures support localization
new List<CultureInfo>
{
new("en-US"),
new("uk-UA")
},
// defaultCulture, it uses for setting if currentCulture is not in cultures list
// and as fallback culture mor missing localization entries.
new CultureInfo("en-US"),
// currentCulture sets when infrastructure loads,
// could be received from app settings or so.
Thread.CurrentThread.CurrentCulture,
// path to assets with json files of localization.
$"{typeof(App).Namespace}/Assets/i18n");
return options;
});
}).Build();
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = new MainWindow
{
DataContext = host.Services.GetRequiredService<MainWindowViewModel>()
};
}
base.OnFrameworkInitializationCompleted();
}
For start using the markup localization in Avalonia by Localize markup extension, it needs the namespace to be added to the markup.
<Window xmlns="https://github.com/avaloniaui"
...
xmlns:i18n="clr-namespace:TekDeq.Localization.Avalonia.Extensions;assembly=TekDeq.Localization.Avalonia"
...
>
After this it could be used for localization of UI
<StackPanel
...
<TextBlock Text="{i18n:Localize Greeting}" />
...
</StackPanel>
The DEMO project included to the repository, just open and run it in Visual Studio.
This project is licensed under the terms of the MIT license.
Contributions are welcome. Just open an Issue or submit a new PR.
You can reach me via my email.