In today's globalized software development environment, application internationalization is becoming increasingly important. It allows your software to adapt to users from different regions and languages, enhancing the user experience. This article is similar to 《Avalonia使用XML文件实现国际化》. The package introduced today is only for WPF programs. Below we will briefly explain its usage.
Install the Required NuGet Package
To implement internationalization using custom XML files in a WPF program, first install a key NuGet package. In the NuGet Package Manager Console in Visual Studio, run the following command:
Install-Package WPFXmlTranslator
This package provides the core functionality and tools needed for internationalization.
Dynamically Retrieve the Language List
In a multilingual application, it is important to let users choose their preferred language. With the following code, you can easily retrieve the language list:
List<LocalizationLanguage> languages = I18nManager.Instance.Resources.Select(kvp => kvp.Value).ToList();
Here, I18nManager is a management class provided by the WPFXmlTranslator package that handles internationalization resources. The Resources property stores all available language resources, and we convert them into a list of LocalizationLanguage objects using the Select method.
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;
//...
}
The LocalizationLanguage class contains basic language information, such as language name, description, and culture name. After retrieving the language list, you can bind it to the UI, for example, by showing the available language options in a dropdown menu or using it in other UI elements that need to display language information.
<ComboBox ItemsSource="{Binding Languages}"
SelectedItem="{Binding SelectLanguage}"
DisplayMemberPath="Language" />
In this XAML code, we create a ComboBox control, bind its ItemsSource property to the Languages list, bind the SelectedItem property to the SelectLanguage property, and set the DisplayMemberPath property to Language. This displays the language list in the dropdown menu for the user to choose from.
Dynamically Switch Languages
When the user selects a different language in the UI, we need to implement dynamic language switching in code. Here is an example of switching languages:
var culture = new CultureInfo(language);
I18nManager.Instance.Culture = culture;
Here, the language parameter is the CultureName property value of the LocalizationLanguage class. The CultureInfo class is a .NET framework class used to represent specific cultures. By setting the I18nManager.Instance.Culture property to the corresponding CultureInfo object, we can achieve instant UI language switching and provide a seamless internationalization experience for users.
Using Translated Strings in Code
In code, you can easily obtain translated strings for the current culture using strongly typed keys. For example:
var titleCurrentCulture = I18nManager.Instance.GetResource(Localization.Main.MainView.Title); // Get current language
var titleZhCN = I18nManager.Instance.GetResource(Localization.Main.MainView.Title, "zh-CN"); // Get Simplified Chinese
var titleEnUS = I18nManager.Instance.GetResource(Localization.Main.MainView.Title, "en-US"); // Get English
The I18nManager.Instance.GetResource method is used to retrieve the translated string for a specified language. The first parameter is a strongly typed language key that points to a specific translation item in the XML language file. The second parameter is optional and specifies the specific language culture name. This way, you can flexibly use translated text anywhere in your code, ensuring that the displayed content matches the user's selected language.
Usage in XAML UI
Using XML translation files in XAML is also very convenient. First, you need to import the appropriate namespaces:
xmlns:language="clr-namespace:Localization"
xmlns:markup="https://codewf.com"
Here, markup is the namespace for the helper library installed earlier. It provides the I18n markup extension helper class for binding translated text in the UI. language is the namespace for the C# strongly typed language key associated classes generated by the T4 file, which connects to the language keys in the XML language file.
Below is an example of using translated text in a control:
<Button Content="{markup:I18n {x:Static language:ChoiceLanguagesView.LanguageKey}}" />
In the example above, the Content property of the Button control is bound to the translated text corresponding to ChoiceLanguagesView.LanguageKey via the I18n markup extension. This way, when the UI language changes, the button's display text will automatically update to the translation in the corresponding language.
Specifying a language 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>
In this example, we create a StackPanel layout containing several child StackPanel elements, each displaying translated text in a different language. By specifying the CultureName property in the I18n markup extension, we can show the translation for a particular language.
Additionally, XAML supports binding dynamic keys, for example:
<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. The I18n markup extension enables the display of dynamic translated text.
Summary
The usage is consistent with the Avalonia version. Below are the WPF version source code, including the NuGet package source and usage examples:
By using custom XML files and the WPFXmlTranslator package, you can easily implement internationalization in WPF applications. This approach is not only simple and easy to use but also highly flexible, meeting the needs of various application scenarios. I hope this article helps you bring your WPF application to a broader international market.