Avalonia UI Framework localization

So you want to localize your application developed using the Avalonia UI Framework? Let’s see how it can be done.

Some informations are taken from this bug report.

To localize an application we must be able to

  • Localize strings directly in the XAML
  • Get access to the localization strings in the code

To translate strings in the XAML we can use a custom MarkupExtension and bindings

Note that in addition to the string key there’s also a context, used to have different translations for the same string in different contexts.

This extension can be used in XAML:

We need to define the namespace first

then we can bind strings using our MarkupExtension

Now we need a way to translate the strings to the desired language. An easy way is to use JSON files containing the context/key with the corresponding translated text. We can embed the JSON files using assets

Missing strings are displayed as the language code followed by a semicolon and the string key.

Since we are using bindings and INotifyPropertyChanged the translated text is applied as soon as we load a new language.

To use translated string in the code use the static Localizer using the string key

You can download the sample project here (Avalonia 0.10)

AvaloniaLocalizationExample.zip (5 downloads)