Repository navigation
Wpf Intro
The GetText.NET.Wpf package provides localization support for WPF elements. It can translate text properties in a window or another WPF element tree using the same ICatalog and gettext catalogs as the rest of GetText.NET.
Introduced in the 11.x line, this package targets .NET Framework 4.6.1 and .NET 10 for Windows. The WPF sample is in samples\Wpf\Samples.Hello.Wpf, with shared translation catalogs in samples\Shared\Locales.
Install the package with:
dotnet add package GetText.NET.Wpf
GetText.NET.Extractor parses .xaml files and exports literal values from common translatable attributes, including Content, Header, Label, Text, Title and ToolTip. It also extracts direct text from TextBlock, Run and AccessText elements.
For example:
<Window x:Class="Example.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Title="Hello, world!">
<StackPanel>
<TextBlock Text="Welcome" />
<Button Content="Continue" ToolTip="Continue to the next step" />
</StackPanel>
</Window>The extractor includes these literal values in the generated PO template. It also recognizes Gettext markup extensions and extracts their literal text, context and plural text. Other markup extensions and data-bound properties are left for the application to resolve; runtime binding values are not extracted.
GetText.Wpf.Localizer walks the logical and visual trees below a DependencyObject and translates writable string properties. It handles Text, Title, Content, Header and ToolTip, as well as DataGridColumn and GridViewColumn headers.
using GetText;
using GetText.Wpf;
public partial class MainWindow : Window
{
private readonly ObjectPropertiesStore originalTextStore = new ObjectPropertiesStore();
private void SetTexts(ICatalog catalog)
{
GettextExtension.DefaultCatalog = catalog;
Localizer.Localize(this, catalog, originalTextStore);
}
private void RestoreOriginalTexts()
{
Localizer.Revert(this, originalTextStore);
}
}The localizer snapshots logical and visual children before changing text, so localization and reversion can safely update realized WPF text documents. AccessText manages its own accelerator inlines; the localizer translates its Text rather than the generated Runs. Authored TextBlock inlines are translated individually without replacing their formatting or bindings with aggregate text.
Properties supplied by bindings, expressions or templates (including TemplateBinding and template triggers) are left under WPF's control. A menu's Header or a button's Content is translated at the owning control, and its template-generated text follows that value. Literal text declared inside a template is also left template-managed; use the Gettext markup extension or a localized binding for those values rather than relying on the tree localizer to overwrite the template.
Use ObjectPropertiesStore when the UI needs to switch languages repeatedly: call Localizer.Revert before loading the new catalog and calling Localizer.Localize again. Call both methods on the element's WPF dispatcher thread.
Keep accelerator markers in menu source strings and translations (for example, _File). WPF's AccessText then manages access-key registration and inline rendering. Do not independently translate its generated Runs.
ComboBox.Text is selection/input state, not a static display label. Setting it can select a different item even when IsEditable is false and SelectedItem or SelectedValue is bound. The localizer therefore neither translates nor restores ComboBox.Text, and it ignores any original-text entry captured for that property by an older version.
For editable ComboBoxes, the same rule applies to the template's PART_EditableTextBox: WPF synchronizes that editor through callbacks even when its Text has no explicit binding. Free-form user input and the current selection are left intact. This rule is specific to ComboBoxes and their editor parts; it does not exclude ordinary authored TextBoxes from existing text localization.
Surrounding labels and tooltips still localize normally. For item display names, use application-localized item properties, a localized binding or an item template rather than translating the selector's Text. Refresh the authoritative display source when the language changes; tree localization does not translate strings supplied by ItemsSource or infer their message IDs.
SelectionChanged can still occur for other programmatic changes. Applications should distinguish user intent from their own selection updates when forwarding preferences, rather than assuming every added item represents a user action. The localizer does not suppress events or temporarily change selection to restore it afterward.
The WPF package also provides the Gettext markup extension for values that should be resolved directly by XAML:
<Window xmlns:gt="clr-namespace:GetText.Wpf;assembly=GetText.Wpf">
<TextBlock Text="{gt:Gettext Hello, world!}" />
</Window>Set GettextExtension.DefaultCatalog before the XAML value is evaluated, or use the extension with the catalog initialization pattern used by the application.
The extension supports Context, PluralText and Count, with an optional per-instance Catalog. A Context can also be encoded as context|text in the source text when no explicit context is supplied.
ProvideValue resolves a string when XAML evaluates the extension. Changing DefaultCatalog does not automatically refresh existing values. For live language switching, use a binding whose source is refreshed by the application, or recreate/re-evaluate the relevant XAML content. Do not rely on tree localization to recover the original message ID from an already translated markup-extension value.
CatalogConverter implements IValueConverter for string bindings, using its Catalog property or GettextExtension.DefaultCatalog. Non-string and empty values pass through unchanged. Use it with one-way bindings: conversion back does not reverse a translation.
After changing the catalog, notify the bound source property or explicitly refresh the binding target so the converter runs again. The tree localizer deliberately leaves these bound properties alone.