/Avalonia.Localizer

Easy-to-use localization provider for the AvaloniaUI

Primary LanguageC#MIT LicenseMIT

Avalonia localizer

The sample project comes with a ready-to-use localization provider, allowing for dynamic runtime localization using the AvaloniaUI framework. The idea is to create JSON files as assets. Each file contains a key and translation according to the locale:

 {
  "EnumSample": "Primer pretvornika enum",
  "car": "Avto",
  "plane": "Letalo",
  "bicycle": "Kolo",
  "SampleTranslate": "To je primer besedila, uporabljenega za lokalizacijo."
}

Than you can use the localizer in XAML, code or with enums:

\\ XAML
xmlns:localization="clr-namespace:Avalonia.Localizer.Core.Localization"
<TextBlock FontSize="18" Margin="0,10,0,0" Text="{localization:Localize SampleTranslate}"/>

\\ XAML for Enum
<Window.Resources>
   <converters:EnumDescriptionConverter x:Key="enumConverter" />
</Window.Resources>

<ComboBox.ItemTemplate>
  <DataTemplate>
     <TextBlock Text="{Binding Converter={StaticResource enumConverter}}"/>
  </DataTemplate>
</ComboBox.ItemTemplate>

\\ Code
var localized = ProgramCore.Localizer["plane"];

\\ In Enum, where in the Description attribute we use the localization key
 public enum SampleEnum
    {
        [Description("car")]
        Car,
    }
    
 EnumDescriptionConverter.GetEnumDescription(SampleEnum.Car);

Thanks to bindings, we can change the locale in the runtime with just one line of code:

ProgramCore.Localizer.SwitchLanguage("en-US");

Credits to sakya for the idea.