WPF internationalizes with custom XML files

WPF internationalizes with custom XML files

This article describes in detail the methods of using custom XML files to achieve internationalization in WPF programs, including installing the necessary NuGet package, dynamically obtaining language lists, dynamically switching languages, using translation strings in code and xaml interfaces, etc. It also provides source code links to help developers easily internationalize WPF applications.

最后更新 1/26/2025 6:02 AM
沙漠尽头的狼
预计阅读 7 分钟
分类
WPF
标签
.NET C# WPF NuGet internationalization

在当今全球化的软件开发环境中,应用程序的国际化变得越来越重要。它可以让你的软件适应不同地区和语言的用户,提升用户体验。本文和 《Avalonia使用XML文件实现国际化》 类似,今天介绍的包只用于WPF程序,下面我们将简单介绍其用法。

Install the necessary NuGet package

To internationalize using custom XML files in a WPF program, you first need to install a key NuGet package. In Visual Studio's NuGet Package Manager console, execute the following command:

Install-Package WPFXmlTranslator

This package provides us with the core functions and tools we need to internationalize.

Dynamic access to language list

In an application that supports multiple languages, it is important to allow users to choose their preferred language. We can easily get a list of languages with the following code:

List<LocalizationLanguage> languages = I18nManager.Instance.Resources.Select(kvp => kvp.Value).ToList();

这里的 I18nManagerWPFXmlTranslator 包提供的一个管理类,它负责处理国际化相关的资源。Resources 属性存储了所有可用的语言资源,我们通过 Select 方法将其转换为 LocalizationLanguage 对象的列表。

Among them, the "LocalizationLanguage" class is defined as follows:

public class LocalizationLanguage
{
    public string Language { get; set; } = (string) null;

    public string Description { get; set; } = (string) null;

    public string CultureName { get; set; } = (string) null;

    //...
}

LocalizationLanguage 类包含了语言的基本信息,如语言名称、描述和文化名称。获取到语言列表后,我们可以将其用于界面绑定,例如在下拉菜单中显示可供用户选择的语言选项,或者在其他需要展示语言信息的界面元素中进行数据绑定。

<ComboBox ItemsSource="{Binding Languages}"
      SelectedItem="{Binding SelectLanguage}"
      DisplayMemberPath="Language" />

在这个XAML代码中,我们创建了一个 ComboBox 控件,将其 ItemsSource 属性绑定到 Languages 列表,SelectedItem 属性绑定到 SelectLanguage 属性,DisplayMemberPath 属性设置为 Language,这样就可以在下拉菜单中显示语言列表供用户选择。

Dynamically switch languages

When users select different languages in the interface, we need to dynamically switch languages in the code. The following is a code example to implement language switching:

var culture = new CultureInfo(language);
I18nManager.Instance.Culture = culture;

这里的 language 参数为 LocalizationLanguage 类的 CultureName 属性值。CultureInfo 类是.NET框架中用于表示特定文化的类,通过设置 I18nManager.Instance.Culture 属性为相应的 CultureInfo 对象,我们可以实现界面语言的即时切换,为用户提供无缝的国际化体验。

Use translation strings in code

In the code, we can easily obtain the translation string of the current language and culture based on the strongly typed Key. For example:

var titleCurrentCulture = I18nManager.Instance.GetResource(Localization.Main.MainView.Title); // 获取当前语言
var titleZhCN = I18nManager.Instance.GetResource(Localization.Main.MainView.Title, "zh-CN");	// 获取中文简体
var titleEnUS = I18nManager.Instance.GetResource(Localization.Main.MainView.Title, "en-US");	// 获取英文

I18nManager.Instance.GetResource 方法用于获取指定语言的翻译字符串。第一个参数是强类型的语言Key,它指向XML语言文件中的某个具体的翻译项。第二个参数是可选的,用于指定具体的语言文化名称。通过这种方式,我们可以在代码的任何地方灵活地使用翻译文本,确保界面显示的内容与用户选择的语言相匹配。

Applications in the xaml interface

Using XML translation files in the xaml interface is also very convenient. First, you need to introduce the corresponding namespace:

xmlns:language="clr-namespace:Localization"
xmlns:markup="https://codewf.com"

Among them,"markup" is the previously installed auxiliary library namespace, which provides the "I18n" tag extension help class, which is used to bind translated text in the interface;"language" is the C#strongly typed language Key association class namespace generated by T4 files, through which you can associate it with the language key of an XML language file.

The following is an example of using translated text in a control:

<Button Content="{markup:I18n {x:Static language:ChoiceLanguagesView.LanguageKey}}" />

In the above example, the "Content" property of the "Button" control is bound to the translated text corresponding to "ChoiceLanguages View. Language Key" through the "I18n" tag extension. In this way, when the interface language changes, the displayed text of the button will automatically update to the translation content in the corresponding language.

Of course, specifying languages is also supported:

<StackPanel>     <StackPanel Orientation="Horizontal">
         <TextBlock Text="Current Thread" />
         <TextBlock Text="{markup:I18n {x:Static language:DevelopModule.Title2SlugView.Title}}"></TextBlock>
     </StackPanel>
     <StackPanel Orientation="Horizontal">
         <TextBlock Text="en-US" />
         <TextBlock Text="{markup:I18n {x:Static language:DevelopModule.Title2SlugView.Title}, CultureName=en-US}" />
     </StackPanel>
     <StackPanel Orientation="Horizontal">
         <TextBlock Text="ja-JP" />
         <TextBlock Text="{markup:I18n {x:Static language:DevelopModule.Title2SlugView.Title}, CultureName=ja-JP}" />
     </StackPanel>
     <StackPanel Orientation="Horizontal">
         <TextBlock Text="zh-CN" />
         <TextBlock Text="{markup:I18n {x:Static language:DevelopModule.Title2SlugView.Title}, CultureName=zh-CN}" />
     </StackPanel>
     <StackPanel Orientation="Horizontal">
         <TextBlock Text="zh-Hant" />
         <TextBlock Text="{markup:I18n {x:Static language:DevelopModule.Title2SlugView.Title}, CultureName=zh-Hant}" />
     </StackPanel>
 </StackPanel>

在这个示例中,我们创建了一个 StackPanel 布局,其中包含多个子 StackPanel,每个子 StackPanel 显示不同语言的翻译文本。通过在 I18n 标记扩展中指定 CultureName 属性,我们可以显示特定语言的翻译内容。

In addition, the xaml interface also supports dynamic key binding, such as:

<u:Banner
    Content="{markup:I18n {Binding SelectedMenuItem.Description}}"
    Header="{markup:I18n {Binding SelectedMenuItem.Name}}"
    Type="{Binding SelectedType}" />

In this example, the "Content" and "Header" properties of the "Banner" control are bound to the dynamic "SelectedMenuItem.Description" and "SelectedMenuItem.Name" properties respectively, and dynamically translated text is displayed through the "I18n" tag extension.

summary

The usage is consistent with the Avalonia version. The following is the source code for the WPF version, including the NuGet package source code and usage examples:

通过使用自定义XML文件和 WPFXmlTranslator 包,我们可以方便地实现WPF应用程序的国际化。这种方法不仅简单易用,而且具有很强的灵活性,可以满足不同应用场景的需求。希望本文对你有所帮助,让你的WPF应用能够走向更广阔的国际市场。

Keep Exploring

延伸阅读

更多文章