rskibbe.I18n.Wpf 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package rskibbe.I18n.Wpf --version 1.0.0                
NuGet\Install-Package rskibbe.I18n.Wpf -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="rskibbe.I18n.Wpf" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add rskibbe.I18n.Wpf --version 1.0.0                
#r "nuget: rskibbe.I18n.Wpf, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install rskibbe.I18n.Wpf as a Cake Addin
#addin nuget:?package=rskibbe.I18n.Wpf&version=1.0.0

// Install rskibbe.I18n.Wpf as a Cake Tool
#tool nuget:?package=rskibbe.I18n.Wpf&version=1.0.0                

Description

A package with helpers for translating Windows Presentation Foundation (WPF) applications - based on rskibbe.I18n.

Keep in mind that rskibbe.I18n is a 1 person project. I'm always trying to fix bugs, improve and extend as soon as I notice them/can.

Getting started

If you followed the steps from the base package, you are ready to provide your custom translations.

A quick translator setup recap

To setup your custom translator just go ahead and use this quick method (if you've installed rskibbe.I18n & like rskibbe.I18n.Json & finally this package rskibbe.I18n.Winforms):

// don't forget the imports
using rskibbe.I18n.Models;
using System.Windows;

// before like the first Form InitializeComponent
Translator.Builder
    .WithAutoFormTranslation()
    .Build()
    // if you want to be able to listen to language changes and update translations
    // .WithUpdates()
    .StoreInstance();

This will take the Json (or like Ini if installed instead) package by autodetection and will automatically translate and update corresponding controls like Labels, Buttons and Forms. You don't need to configure much more - isn't this nice 😉.

Remember to do this "bestly" before any UI going on, the Bootstrapping point Program.cs/Program.vb or like the first Window Constructor (before the InitializeComponent) could be the best place (but make sure it doesn't get called multiple times).

Marking controls as translatable

In the next step you need to tell the translator what controls should actually be translated. This is easily done by just using the "Translate" markup extension.

Full Example

Step 1 - Install packages from NuGet

The base package is always necessary rskibbe.I18n. Depending on the translation style you want to use (InMemory, file-based like Json or Ini, etc.), install the next package like rskibbe.I18n.Json. Now you can install the last helper package for WPF (this one you are in right now..) rskibbe.I18n.WPF.

Step 2 - Create a "Window/UserControl"

Go on like your first View - usually MainWindow / UserControl1 - and put a button on top of it. Name it as you wish but specify the Content property with the "Translate" markup extension. The translator will know, that it has to translate the corresponding translation key (the thing after the 😃.

Import the correct namespace in you xaml file
xmlns:i18n="clr-namespace:rskibbe.I18n.Wpf;assembly=rskibbe.I18n.Wpf"
Specify the content/text using the markup extension

In this case for example, it will try to find a translation for "btnEnglish" in the root namespace of the translation file. Keep in mind, that Ini files currently don't support namespacing like "myForm.myButton".

<StackPanel HorizontalAlignment="Center" VerticalAlignment="Bottom" Orientation="Horizontal">
    <Button Content="{i18n:Translate btnGerman}" Click="Button_ClickDE" Margin="5" />
    <Button Content="{i18n:Translate btnEnglish}" Click="Button_ClickEN" Margin="5" />
</StackPanel>

Step 3 - Create the necessary files

Please take a look at the corresponding package like rskibbe.I18n.Json or rskibbe.I18n.Ini for file and content structure. More packages for loading from web APIs and like SQL to come.

Step 4 - The code

using rskibbe.I18n.Models;
using System.Windows;

namespace WpfTestApp;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        Translator.Builder
            .WithUpdates()
            .Build()
            .StoreInstance();
        InitializeComponent();
        Loaded += MainWindow_Loaded;
    }

    private async void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        await Translator.Instance.LoadLanguagesAsync();
    }

    private async void Button_ClickDE(object sender, RoutedEventArgs e)
    {
        await Translator.Instance.ChangeLanguageAsync("de-DE");
    }

    private async void Button_ClickEN(object sender, RoutedEventArgs e)
    {
        await Translator.Instance.ChangeLanguageAsync("en-US");
    }
}

Take a look at the base package for more information like getting the available languages, changing the language, etc.

Product Compatible and additional computed target framework versions.
.NET net6.0-windows7.0 is compatible.  net7.0-windows was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2 74 11/1/2024
1.0.1 274 3/14/2023
1.0.0 252 10/30/2022