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
<PackageReference Include="Mtf.WPF.LanguageService" Version="2.0.29" />
paket add Mtf.WPF.LanguageService --version 2.0.29
#r "nuget: Mtf.WPF.LanguageService, 2.0.29"
// 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
): TheWindow
to be translated.
- Exceptions:
ArgumentNullException
: Thrown ifwindow
isnull
.
- Description:
Updates theTitle
of theWindow
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
, andContent
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 asHeader
,Text
, orContent
.
Features
Translation Integration:
- Utilizes the
Lng.Elem
method to fetch translations for strings.
- Utilizes the
Recursive Translation:
- Handles nested visual elements by traversing both the logical and visual trees.
Targeted Properties:
- Specifically translates properties like
Header
,Text
, andContent
, making it versatile for WPF applications.
- Specifically translates properties like
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
Logical vs. Visual Tree:
- The method traverses both logical and visual trees to ensure all relevant elements are translated.
Extendability:
- Can be extended to handle custom properties by modifying the
TranslateObjectWithProperty
method.
- Can be extended to handle custom properties by modifying the
Compatibility:
- Designed for WPF applications using
System.Windows
andSystem.Windows.Media
.
- Designed for WPF applications using
Performance:
- Efficient traversal of visual and logical trees ensures minimal overhead.
Links
Product | Versions 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. |
-
.NETFramework 4.8.1
- System.IO.Compression (>= 4.3.0)
-
net8.0-windows7.0
- System.IO.Compression (>= 4.3.0)
-
net9.0-windows7.0
- System.IO.Compression (>= 4.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Rename to Mtf.LanguageService.