zHaytam / EasyLocalization

A simple library that makes WPF Localization easier
MIT License
22 stars 6 forks source link
csharp localization wpf

# EasyLocalization

A simple library that makes WPF Localization easier.

Features

Demo

Demo GIF

How to use

Things that you should know

Adding cultures (languages)

On startup (for example in App.xaml.cs) you'll have to register the cultures you want your applications to have:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    LocalizationManager.Instance.AddCulture(
        CultureInfo.GetCultureInfo("en-US"),
        new CharSeperatedFileReader("Resources/en-US.txt"),
        true);
    LocalizationManager.Instance.AddCulture(
        CultureInfo.GetCultureInfo("es-ES"),
        new XmlFileReader("Resources/es-ES.xml"));
    LocalizationManager.Instance.AddCulture(
        CultureInfo.GetCultureInfo("fr"),
        new JsonFileReader("Resources/fr.json"));
}

The true in the first AddCulture call tells the LocalizationManager to choose this language for now.

Simple localization

<TextBlock Margin="4" Text="{_:Localize Key1}" />
<TextBlock Margin="4" Text="{_:Localize Key=Key1_1}" />

Bindable Key

<TextBlock Margin="4" Text="{localization:Localize KeySource={Binding Key} />

Alterternative Key

<TextBlock Margin="4" Name="LblTitle" Text="{localization:Localize}" />

Since the Key is not provided, the LocalizationManager will use the alternative key, in this case it's LblTitle_Text.

Handling singular, zero and plural

<TextBlock Margin="4"
           Text="{localization:Localize KeySource={Binding Key}, 
                                        CountSource={Binding Value}}" />

The LocalizationManager will adapt the Text property whenever the Key or Count change:

Changing the culture (language)

LocalizationManager.Instance.CurrentCulture = CultureInfo.GetCultureInfo("fr");

Whenever the CurrentCulture changes, the whole application is automatically updated without the need to restart it.

For a list of the available/added cultures:

LocalizationManager.Instance.AvailableCultures