Avalonia string localization support.
- Switch language at runtime.
- Customizable language file format and location.
- Simple interface, simple implementation.
- Json language file
Languages\en.json
:
{
"Welcome": "Welcome to Avalonia!"
}
- Load language files:
Localizer.SetLocalizer(new JsonLocalizer());
- Use localized string in .axaml:
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:l="using:Jeek.Avalonia.Localization"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Jeek.Avalonia.Localization.Example.MainWindow"
Title="Jeek.Avalonia.Localization.Example">
<StackPanel>
<!-- Get the string with key Welcome -->
<Label Content="{l:Localize Welcome}" />
</StackPanel>
</Window>
- Switch language:
Localizer.Language = "en";
- Get localized string in code:
Localizer.Get("Welcome");
- I found Localizing using ResX | Avalonia Docs (avaloniaui.net) in Avalonia website. It's static and can't switch language at runtime.
- Then I found this article: Avalonia UI Framework localization | Sakya's Homepage, and learn much from it. Finally I created my own version, more simple and flexible.