Mtf.WPF.LanguageService 2.0.29

dotnet add package Mtf.WPF.LanguageService --version 2.0.29                
NuGet\Install-Package Mtf.WPF.LanguageService -Version 2.0.29                
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="Mtf.WPF.LanguageService" Version="2.0.29" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Mtf.WPF.LanguageService --version 2.0.29                
#r "nuget: Mtf.WPF.LanguageService, 2.0.29"                
#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 Mtf.WPF.LanguageService as a Cake Addin
#addin nuget:?package=Mtf.WPF.LanguageService&version=2.0.29

// Install Mtf.WPF.LanguageService as a Cake Tool
#tool nuget:?package=Mtf.WPF.LanguageService&version=2.0.29                

Translator Class Documentation

The Translator class provides functionality to translate WPF Window objects and their visual elements recursively. It integrates with the Lng class to support multilingual applications by updating relevant properties of WPF controls and elements.


Namespace

Mtf.LanguageService.WPF

Methods

Translate(Window window)

Translates a Window and its visual elements.

  • Parameters:
    • window (Window): The Window to be translated.
  • Exceptions:
    • ArgumentNullException: Thrown if window is null.
  • Description:
    Updates the Title of the Window and recursively translates all child visual elements.

TranslateVisual(Visual myVisual)

Recursively translates a visual tree starting from the provided visual element.

  • Parameters:
    • myVisual (Visual): The starting visual element for translation.
  • Description:
    • Iterates through both logical and visual trees.
    • Checks for specific properties like Header, Text, and Content to translate their values.

TranslateObjectWithProperty(object childVisual, string propertyName)

Translates a specific property of a given object if it exists and is a string.

  • Parameters:
    • childVisual (object): The object to check for the property.
    • propertyName (string): The name of the property to translate.
  • Description:
    Uses reflection to identify and translate properties such as Header, Text, or Content.

Features

  1. Translation Integration:

    • Utilizes the Lng.Elem method to fetch translations for strings.
  2. Recursive Translation:

    • Handles nested visual elements by traversing both the logical and visual trees.
  3. Targeted Properties:

    • Specifically translates properties like Header, Text, and Content, making it versatile for WPF applications.
  4. Null Handling:

    • Includes checks to prevent runtime exceptions when encountering null objects or properties.

Usage Example

using System.Windows;
using Mtf.LanguageService.WPF;

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        // Set Window Title and control contents
        Title = "Main Window";

        var label = new Label
        {
            Content = "Hello World"
        };

        var button = new Button
        {
            Content = "Click Me"
        };

        var stackPanel = new StackPanel();
        stackPanel.Children.Add(label);
        stackPanel.Children.Add(button);

        Content = stackPanel;

        // Translate the Window
        Translator.Translate(this);
    }
}
using Mtf.LanguageService;
using Mtf.LanguageService.WPF;

public partial class MainWindow : Window
{
	public MainWindow()
	{
		InitializeComponent();

		lv.ItemsSource = new[] { Lng.Elem("Users"), Lng.Elem("Groups") };
		rtb.Document.Blocks.Add(new Paragraph(new Run(Lng.Elem("File name"))));

		Translator.Translate(this);
	}
}

If you need to translate to Hungarian, just use this code snippet.

Lng.DefaultLanguage = Mtf.LanguageService.Language.Hungarian;
Translator.Translate(this);

You can translate from any language to any other language like this.

Lng.Translate(Language.Hungarian, "Ismétlődés", Language.English);

Notes

  1. Logical vs. Visual Tree:

    • The method traverses both logical and visual trees to ensure all relevant elements are translated.
  2. Extendability:

    • Can be extended to handle custom properties by modifying the TranslateObjectWithProperty method.
  3. Compatibility:

    • Designed for WPF applications using System.Windows and System.Windows.Media.
  4. Performance:

    • Efficient traversal of visual and logical trees ensures minimal overhead.

Links

GitHub URL Developer's URL Facebook Page

Product Compatible and additional computed target framework versions.
.NET net8.0-windows7.0 is compatible.  net9.0-windows7.0 is compatible. 
.NET Framework net481 is compatible. 
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
2.0.29 73 12/21/2024
2.0.28 74 12/21/2024
2.0.27 76 12/21/2024
2.0.26 88 11/25/2024
1.0.10 91 11/14/2024
1.0.6 362 12/3/2022
1.0.5 513 11/14/2020
1.0.4 457 8/7/2020
1.0.3 399 8/7/2020
1.0.2 511 7/19/2020
1.0.1 598 5/13/2020 1.0.1 is deprecated because it has critical bugs.

Rename to Mtf.LanguageService.