Mtf.Windows.Forms.LanguageService
3.1.23
dotnet add package Mtf.Windows.Forms.LanguageService --version 3.1.23
NuGet\Install-Package Mtf.Windows.Forms.LanguageService -Version 3.1.23
<PackageReference Include="Mtf.Windows.Forms.LanguageService" Version="3.1.23" />
<PackageVersion Include="Mtf.Windows.Forms.LanguageService" Version="3.1.23" />
<PackageReference Include="Mtf.Windows.Forms.LanguageService" />
paket add Mtf.Windows.Forms.LanguageService --version 3.1.23
#r "nuget: Mtf.Windows.Forms.LanguageService, 3.1.23"
#:package Mtf.Windows.Forms.LanguageService@3.1.23
#addin nuget:?package=Mtf.Windows.Forms.LanguageService&version=3.1.23
#tool nuget:?package=Mtf.Windows.Forms.LanguageService&version=3.1.23
Translator Class Documentation
The Translator class provides functionality to translate Windows Forms controls, forms, and their children recursively. It integrates with the Lng class to provide multilingual support by updating the Text property and other string-based properties of controls and related UI elements.
Namespace
Mtf.LanguageService.Windows.Forms
Methods
Translate(Form form, ToolTip toolTip = null)
Translates a Form and all its child controls.
- Parameters:
form(Form): TheFormto be translated.toolTip(ToolTip, optional): AToolTipcomponent whose tooltips should also be translated.
- Returns:
Dictionary<object, string>: A dictionary containing each control and its original text value.
- Exceptions:
ArgumentNullException: Thrown ifformisnull.
- Description:
- Updates the form's
Textproperty and recursively translates all child controls. - Returns original text values to allow restoration if needed.
- Updates the form's
Translate(UserControl userControl, ToolTip toolTip = null)
Translates a UserControl and all its child controls.
- Parameters:
userControl(UserControl): TheUserControlto be translated.toolTip(ToolTip, optional): AToolTipcomponent whose tooltips should also be translated.
- Returns:
Dictionary<object, string>: A dictionary containing each control and its original text value.
- Exceptions:
ArgumentNullException: Thrown ifuserControlisnull.
- Description:
- Updates the user control's
Textproperty and recursively translates all child controls.
- Updates the user control's
Translate(Control.ControlCollection controls, ToolTip toolTip = null)
Recursively translates a collection of controls.
- Parameters:
controls(Control.ControlCollection): The collection of controls to be translated.toolTip(ToolTip, optional): AToolTipcomponent whose tooltips should also be translated.
- Returns:
Dictionary<object, string>: A dictionary containing each control and its original text value.
- Exceptions:
ArgumentNullException: Thrown ifcontrolsisnull.
Translate(DataGridView dataGridView)
Translates the column headers of a DataGridView.
- Parameters:
dataGridView(DataGridView): TheDataGridViewto be translated.
- Returns:
Dictionary<object, string>: A dictionary containing each column and its original header text.
- Description:
- Translates the
HeaderTextof all columns in the grid.
- Translates the
Translate(TreeNode node, ToolTip toolTip = null)
Recursively translates a TreeNode and its child nodes.
- Parameters:
node(TreeNode): TheTreeNodeto be translated.toolTip(ToolTip, optional): AToolTipcomponent whose tooltips should also be translated.
- Returns:
List<KeyValuePair<object, string>>: A list of tree nodes and their original text values.
Translate(ComboBox comboBox)
Translates the string items in a ComboBox.
- Parameters:
comboBox(ComboBox): TheComboBoxwhose items will be translated.
- Exceptions:
ArgumentNullException: Thrown ifcomboBoxisnullor if itsItemscollection isnull.
- Description:
- Translates only string items in the combobox's
Itemscollection usingLng.Elem. - Non-string items (objects) are left unchanged.
- Preserves the currently selected index after translation.
- Clears and repopulates the items collection to apply translations.
- Translates only string items in the combobox's
Translate(ToolStripItemCollection toolStripItems, ToolTip toolTip = null)
Translates a collection of ToolStripItem objects (e.g., menu items, toolbar buttons).
- Parameters:
toolStripItems(ToolStripItemCollection): The collection of tool strip items to be translated.toolTip(ToolTip, optional): AToolTipcomponent whose tooltips should also be translated.
- Returns:
List<KeyValuePair<object, string>>: A list of tool strip items and their original text values.
- Exceptions:
ArgumentNullException: Thrown iftoolStripItemsisnull.
Translate(Menu.MenuItemCollection items, ToolTip toolTip) (Only for .NET Framework 4.8.1 and above)
Translates a collection of MenuItem objects.
- Parameters:
items(Menu.MenuItemCollection): The collection of menu items to be translated.toolTip(ToolTip): AToolTipcomponent whose tooltips should also be translated.
- Returns:
List<KeyValuePair<object, string>>: A list of menu items and their original text values.
- Note: This method is only available when targeting
.NET Framework 4.8.1or above.
Features
Control Types Supported:
Label,Button,TextBox,ListView,TreeView,MenuStrip,StatusStrip,ComboBox,ContextMenuStrip,DataGridView,CheckBox,RadioButton, and more.
Recursive Translation:
- Child elements of controls like
TreeView.Nodes,MenuStrip.Items, and nested controls are translated recursively.
- Child elements of controls like
ComboBox String Items:
- Only translates items that are strings; object items are preserved unchanged.
- Maintains the selected index across translations.
Tooltip Support:
- Optional
ToolTipparameter allows translation of control tooltips alongside the controls.
- Optional
Integration with
LngClass:- Uses
Lng.Elemto translate strings based on the application's current language.
- Uses
Framework Compatibility:
- Conditional compilation directives ensure compatibility with different .NET versions.
Usage Examples
Basic Form Translation
using System.Windows.Forms;
using Mtf.LanguageService.Windows.Forms;
class Program
{
static void Main()
{
// Create a simple form with controls
var form = new Form
{
Text = "Main Application"
};
// Add button
var button = new Button
{
Text = "Click Me",
Location = new System.Drawing.Point(10, 10)
};
// Add label
var label = new Label
{
Text = "Hello World",
Location = new System.Drawing.Point(10, 50)
};
// Add textbox
var textbox = new TextBox
{
Text = "Enter text here",
Location = new System.Drawing.Point(10, 90)
};
form.Controls.Add(button);
form.Controls.Add(label);
form.Controls.Add(textbox);
// Translate the form and all its controls
var originals = Translator.Translate(form);
Application.Run(form);
}
}
ComboBox String Items Translation
using System.Windows.Forms;
using Mtf.LanguageService.Windows.Forms;
public partial class MyForm : Form
{
public MyForm()
{
InitializeComponent();
// Create a ComboBox with string items
var comboBox = new ComboBox
{
Location = new System.Drawing.Point(10, 10),
DropDownStyle = ComboBoxStyle.DropDownList
};
// Add string items to translate
comboBox.Items.AddRange(new object[]
{
"Option One",
"Option Two",
"Option Three"
});
comboBox.SelectedIndex = 0;
this.Controls.Add(comboBox);
// Translate the combobox items
Translator.Translate(comboBox);
}
}
ComboBox with Mixed Content (Strings and Objects)
using System.Windows.Forms;
using Mtf.LanguageService.Windows.Forms;
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
public override string ToString() => Name;
}
public partial class MyForm : Form
{
public MyForm()
{
InitializeComponent();
var comboBox = new ComboBox
{
Location = new System.Drawing.Point(10, 10),
DropDownStyle = ComboBoxStyle.DropDownList
};
// Add mixed content: only strings will be translated
comboBox.Items.AddRange(new object[]
{
"Select an option",
new Item { Id = 1, Name = "John" },
new Item { Id = 2, Name = "Jane" },
"Custom Entry"
});
comboBox.SelectedIndex = 0;
this.Controls.Add(comboBox);
// Translate only the string items
Translator.Translate(comboBox);
}
}
Translation with MenuStrip
using System.Windows.Forms;
using Mtf.LanguageService.Windows.Forms;
public partial class MyForm : Form
{
public MyForm()
{
InitializeComponent();
var menuStrip = new MenuStrip();
var fileMenu = new ToolStripMenuItem { Text = "File" };
fileMenu.DropDownItems.Add(new ToolStripMenuItem { Text = "Open" });
fileMenu.DropDownItems.Add(new ToolStripMenuItem { Text = "Save" });
fileMenu.DropDownItems.Add(new ToolStripSeparator());
fileMenu.DropDownItems.Add(new ToolStripMenuItem { Text = "Exit" });
var editMenu = new ToolStripMenuItem { Text = "Edit" };
editMenu.DropDownItems.Add(new ToolStripMenuItem { Text = "Copy" });
editMenu.DropDownItems.Add(new ToolStripMenuItem { Text = "Paste" });
menuStrip.Items.Add(fileMenu);
menuStrip.Items.Add(editMenu);
MainMenuStrip = menuStrip;
Controls.Add(menuStrip);
// Translate all menu items
var originals = Translator.Translate(menuStrip.Items);
}
}
Translation with ToolTips
using System.Windows.Forms;
using Mtf.LanguageService.Windows.Forms;
public partial class MyForm : Form
{
public MyForm()
{
InitializeComponent();
var toolTip = new ToolTip();
var button = new Button
{
Text = "Save",
Location = new System.Drawing.Point(10, 10)
};
toolTip.SetToolTip(button, "Save the current document");
this.Controls.Add(button);
// Translate the form with tooltip support
Translator.Translate(this, toolTip);
}
}
TreeView Translation
using System.Windows.Forms;
using Mtf.LanguageService.Windows.Forms;
public partial class MyForm : Form
{
public MyForm()
{
InitializeComponent();
var treeView = new TreeView();
// Add nodes
var rootNode = treeView.Nodes.Add("Projects");
rootNode.Nodes.Add("Project One");
rootNode.Nodes.Add("Project Two");
var filesNode = treeView.Nodes.Add("Files");
filesNode.Nodes.Add("Document.txt");
filesNode.Nodes.Add("Image.jpg");
this.Controls.Add(treeView);
// Translate all tree nodes
foreach (TreeNode node in treeView.Nodes)
{
var originals = Translator.Translate(node);
}
}
}
DataGridView Column Headers Translation
using System.Windows.Forms;
using Mtf.LanguageService.Windows.Forms;
public partial class MyForm : Form
{
public MyForm()
{
InitializeComponent();
var dataGridView = new DataGridView();
dataGridView.Columns.Add("Name", "Full Name");
dataGridView.Columns.Add("Email", "Email Address");
dataGridView.Columns.Add("Phone", "Phone Number");
this.Controls.Add(dataGridView);
// Translate column headers
var originals = Translator.Translate(dataGridView);
}
}
Language Switching
using System.Windows.Forms;
using Mtf.LanguageService;
using Mtf.LanguageService.Windows.Forms;
public partial class MyForm : Form
{
private Dictionary<object, string> _originalTexts;
public MyForm()
{
InitializeComponent();
// Translate and store originals
_originalTexts = Translator.Translate(this);
}
private void SwitchToHungarian()
{
Lng.DefaultLanguage = Language.Hungarian;
Translator.Translate(this);
}
private void SwitchToEnglish()
{
Lng.DefaultLanguage = Language.English;
Translator.Translate(this);
}
private void RevertToOriginal()
{
// No direct restore method; re-translate with original language
Lng.DefaultLanguage = Language.English;
Translator.Translate(this);
}
}
Best Practices
Translate After Control Creation:
- Call
Translateafter all controls have been added to the form or container.
- Call
Store Originals for Language Switching:
- Capture the returned dictionary if you plan to switch languages later.
ComboBox Considerations:
- Only string items are translated; ensure data-bound items or objects are handled separately if needed.
- Call
Translateafter populating the ComboBox.
ToolTip Integration:
- Pass the
ToolTipcomponent to ensure tooltips are translated along with controls.
- Pass the
Nested Controls:
- No need to manually translate parent and child controls separately; the recursive methods handle the entire hierarchy.
Non-Translatable Elements:
- Elements like
WebBrowserare explicitly skipped during translation to avoid issues.
- Elements like
Notes
Error Handling:
- All public methods include null checks to prevent runtime errors.
Non-Translatable Elements:
- Certain controls like
WebBrowserare skipped during automatic translation.
- Certain controls like
Customization:
- Extend translation behavior by creating wrapper methods or by modifying the
Lng.Elemmethod.
- Extend translation behavior by creating wrapper methods or by modifying the
Conditional Compilation:
MenuItemCollectiontranslation is only available for.NET Framework 4.8.1and above.
ComboBox Behavior:
- Non-string items are preserved unchanged to support object binding scenarios.
- Selected index is maintained after translation to preserve user selection.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0-windows7.0 is compatible. net9.0-windows was computed. net9.0-windows7.0 is compatible. net10.0-windows was computed. net10.0-windows7.0 is compatible. |
-
net10.0-windows7.0
- Mtf.Extensions (>= 1.0.34)
-
net8.0-windows7.0
- Mtf.Extensions (>= 1.0.34)
-
net9.0-windows7.0
- Mtf.Extensions (>= 1.0.34)
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 |
|---|---|---|
| 3.1.23 | 115 | 10/23/2026 |
| 3.1.19 | 195 | 3/26/2026 |
| 3.1.16 | 383 | 6/13/2025 |
| 3.0.96 | 254 | 6/6/2025 |
| 3.0.84 | 289 | 5/26/2025 |
| 3.0.64 | 298 | 5/22/2025 |
| 3.0.49 | 292 | 5/20/2025 |
| 3.0.46 | 289 | 5/20/2025 |
| 3.0.45 | 277 | 5/20/2025 |
| 3.0.44 | 242 | 5/20/2025 |
| 3.0.39 | 241 | 4/6/2025 |
| 3.0.37 | 228 | 3/27/2025 |
| 3.0.35 | 245 | 1/29/2025 |
| 3.0.34 | 179 | 1/29/2025 |
| 3.0.33 | 203 | 1/13/2025 |
| 3.0.32 | 204 | 1/13/2025 |
| 3.0.31 | 231 | 1/13/2025 |
| 3.0.30 | 231 | 1/10/2025 |
| 2.0.29 | 242 | 1/9/2025 |
| 2.0.26 | 247 | 12/21/2024 |