A universal localization method in WPF
By designing the localizeProvider localize class with additional properties, automatically searching and binding the text properties of the WPF control, the complex problem of localization binding methods in WPF is solved, and the universality and efficient development of control localization is realized.
Patent Information
- Application Number
- CN202510429722.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-08
- Publication Date
- 2025-08-15
- Estimated Expiration
- 2045-04-08
AI Technical Summary
In WPF, when the existing technology realizes localization through DynamicResource and Resx files, the binding method is complex and not universal, and requires manual binding for the properties of different controls.
Design the localizeProvider for additional properties, automatically find the control's dependency text properties through the ContentId property, and bind resource values through object reflection, providing a unified localized binding method.
It simplifies the development process, improves development efficiency, and realizes the universality and convenience of control localization.
Smart Images

Figure CN119987742B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of software development technology, and more specifically, to a universal localization method in WPF. Background Art
[0002] Localization is a commonly used development method in software development. In order to make application software compatible with international standards, the software will integrate support for multiple languages. Users in different countries or regions can choose the corresponding language for localized display. The software only needs to replace the resource files of the corresponding language, without the need to redevelop or update the software.
[0003] Currently in WPF, it is mainly implemented through DynamicResource and Resx files, or by replacing Resx files with csv files.
[0004] The method of implementing localization through DynamicResource requires developers to perform corresponding binding based on the properties of different controls. For example, the text property of the TextBlock control is Text, the text property of the Label control is Content, the text property of the TabControl is Header, and the text property of the Window control is Title. The binding method is complex and not universal. Summary of the Invention
[0005] In order to solve the above problems, the present invention proposes a universal localization method, which uses additional attributes to bind control text properties and provides a unified binding method. All controls can use ContextId to complete localization binding.
[0006] This application provides a general localization method in WPF that adopts the following technical solutions:
[0007] A general localization method in WPF includes the following steps:
[0008] Step 1: Prepare different localized resource Resx files, such as the Chinese resource file Resources.zh-CN.resx, the English resource file Resources.en.resx, the French resource file Resources.fr.resx, etc. The corresponding resources in these resource files have the same resource identifier Id;
[0009] Step 2. Design a localization class LocalizeProvider with additional properties. This class provides a ContentId additional property. The additional property automatically searches for the control's dependent text properties and resources based on the ID name and localization resource file. After obtaining the control text dependent property name, it binds and sets the value of the dependent property through object reflection.
[0010] Furthermore, the acquisition of the localized resource file in step 2 includes: determining the name of the resource file according to the user's localization selection (for example, if Chinese is selected, use the Resources.zh-CN.resx file name; if English is selected, use Resources.en.resx, and use ResourceManager to initialize a new instance), and then obtaining the displayed resource content according to the resource ID through the resource instance.
[0011] Furthermore, the automatic search for the dependent text properties of the control in step 2 includes: since most controls have unique text dependent properties, these different dependent properties are put into a list (the list includes: HeaderProperty, TitleProperty, ContentProperty, DataProperty, TextProperty), and in the implementation, the above list is looped to search for the matching text dependent property in the object properties, and the loop is exited after finding it, and the text dependent property name is recorded.
[0012] In summary, this application includes at least one of the following beneficial technical effects:
[0013] The present invention provides a universal localization method for developers, simplifies the development process, and further improves development efficiency. BRIEF DESCRIPTION OF THE DRAWINGS
[0014] Figure 1 This is a flowchart of the application;
[0015] Figure 2 This is the English display effect diagram of this application;
[0016] Figure 3 This is the Chinese display effect diagram of this application. DETAILED DESCRIPTION
[0017] The technical solutions in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application; it is obvious that the described embodiments are only part of the embodiments of the present application, rather than all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of this application.
[0018] In the description of this application, it should be noted that the terms "upper," "lower," "inner," "outer," "top / bottom," and the like, indicating orientations or positional relationships, are based on the orientations or positional relationships shown in the accompanying drawings and are intended solely to facilitate the description of this application and simplify the description. They do not indicate or imply that the devices or components referred to must have a specific orientation, be constructed, or operate in a specific orientation. Therefore, they should not be construed as limitations on this application. Furthermore, the terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance.
[0019] In the description of this application, it should be noted that, unless otherwise expressly specified or limited, the terms "installed," "provided with," "mounted / connected," and "connected" should be understood in a broad sense. For example, "connected" can mean a fixed connection, a detachable connection, or an integral connection; it can be a mechanical connection or an electrical connection; it can be a direct connection or an indirect connection through an intermediate medium, or it can be internal communication between two components. Those skilled in the art will understand the specific meanings of the above terms in this application based on the specific circumstances.
[0020] The present application is further described in detail below with reference to the accompanying drawings.
[0021] This embodiment discloses a general localization method in WPF, including the following steps:
[0022] Step 1: Prepare different localized resource Resx files, such as the Chinese resource file Resources.zh-CN.resx, the English resource file Resources.en.resx, the French resource file Resources.fr.resx, etc. The corresponding resources in these resource files have the same resource identifier Id;
[0023] Step 2. Design a localization class LocalizeProvider with additional properties. This class provides a ContentId additional property. The additional property automatically searches for the control's dependent text properties and resources based on the ID name and localization resource file. After obtaining the control text dependent property name, it binds and sets the value of the dependent property through object reflection.
[0024] Step 1 is an operation well known to those skilled in the art and will not be described in detail. Step 2 will be described in detail below. Figure 1 As shown:
[0025] The acquisition of localized resource files includes: determining the name of the resource file based on the user's localization selection (for example, if Chinese is selected, use the Resources.zh-CN.resx file name; if English is selected, use Resources.en.resx, and use ResourceManager to initialize a new instance), and then obtaining the displayed resource content based on the resource ID through the resource instance.
[0026] The automatic search for the dependent text properties of controls includes: since most controls have unique text dependent properties, these different dependent properties are put into a list (the list includes: HeaderProperty, TitleProperty, ContentProperty, DataProperty, TextProperty). In the implementation, the above list is looped to search for the matching text dependent property in the object properties. Once found, the loop is exited and the text dependent property name is recorded.
[0027] The present invention provides a universal localization method for developers, simplifies the development process, and further improves development efficiency.
[0028] According to the method proposed in the present invention, TextBlock, Button, MenuItem, Window, RadioButton, CheckBox and other controls are used for localized display, and the display is switched between Chinese and English through the "Chinese" and "English" buttons, such as Figure 2 and Figure 3 As shown, controls that need to be displayed locally are set by adding the attribute "local:LocalizeProvider.ContentID". The detailed implementation is as follows:
[0029] <Window x:Class="WpfApp5.MainWindow"
[0030] xmlns="http: / / schemas.microsoft.com / winfx / 2006 / xaml / presentation"
[0031] xmlns:x="http: / / schemas.microsoft.com / winfx / 2006 / xaml"
[0032] xmlns:d="http: / / schemas.microsoft.com / expression / blend / 2008"
[0033] xmlns:mc="http: / / schemas.openxmlformats.org / markup-compatibility / 2006"
[0034] xmlns:local="clr-namespace:WpfApp5"
[0035] mc:Ignorable="d"
[0036] local:LocalizeProvider.ContentID="WindowTitle" Height="300" Width="600">
[0037] <grid>
[0038] <Grid.ColumnDefinitions>
[0039] <columndefinition Width="*" / >
[0040] <columndefinition Width="200" / >
[0041] < / Grid.ColumnDefinitions>
[0042] <tabcontrol>
[0043] <tabitem local:localizeprovider.contentid="TabItem1">
[0044] <grid>
[0045] <Grid.RowDefinitions>
[0046] <rowdefinition Height="2.5*" / >
[0047] <rowdefinition Height="*" / >
[0048] <rowdefinition Height="*" / >
[0049] < / Grid.RowDefinitions>
[0050] <button Grid.Row="1" Width="80" Height="30" local:LocalizeProvider.ContentID="Button">< / button>
[0051] <menu grid.row="2" width="200" height="30" local:localizeprovider.contentid="">
[0052] <menuitem local:localizeprovider.contentid="MenuItem">
[0053] <menuitem local:LocalizeProvider.ContentID="SubMenuItem1">
[0054] <menuitem local:LocalizeProvider.ContentID="SubMenuItem2">
[0055] <menuitem local:LocalizeProvider.ContentID="SubMenuItem3">
[0056] < / menuitem>
[0057] < / menu>
[0058] <groupbox local:localizeprovider.contentid="GroupBox">
[0059] <stackpanel>
[0060] <textblock Grid.Row="0" Width="80" Height="30" Background="AliceBlue" local:LocalizeProvider.ContentID="TextBlock" / >
[0061] <radiobutton Width="100" local:LocalizeProvider.ContentID="RadioButton" Margin="5" / >
[0062] <checkbox Width="100" local:LocalizeProvider.ContentID="CheckBox" Margin="5" / >
[0063] <combobox width="100" selectedindex="0" margin="5">
[0064] <comboboxitem local:LocalizeProvider.ContentID="item1" / >
[0065] <comboboxitem local:LocalizeProvider.ContentID="item2" / >
[0066] <comboboxitem local:LocalizeProvider.ContentID="item3" / >
[0067] < / combobox>
[0068] < / stackpanel>
[0069] < / groupbox>
[0070] < / grid>
[0071] < / tabitem>
[0072] <tabitem local:localizeprovider.contentid="TabItem2">
[0073] < / tabitem>
[0074] < / tabcontrol>
[0075] <stackpanel grid.column="1" orientation="Vertical" verticalalignment="Center">
[0076] <button x:Name="btnCh" Content="中文" Width="80" Height="30" Click="btnCh_Click" Margin="5">< / button>
[0077] <button x:Name="btnEn" Content="英文" Width="80" Height="30" Click="btnEn_Click">< / button>
[0078] < / stackpanel>
[0079] < / grid>
[0080]
[0081] The above are all preferred embodiments of the present application, and are not intended to limit the scope of protection of the present application. Therefore, any equivalent changes made based on the structure, shape, and principle of the present application should be included in the scope of protection of the present application.
Claims
1. A general localization method in WPF, characterized in that, The following steps are involved: Step 1: Prepare different localized resource Resx files and match the corresponding resources in these resource files with the same resource identifier Id; Step 2. Design a localization class LocalizeProvider with additional properties. This class provides a ContentId additional property. The additional property automatically searches for the control's dependent text properties and resources based on the ID name and localized resource file. After obtaining the control text dependent property name, it binds and sets the value of the dependent property through object reflection. Automatically searching for the dependent text property of the control in step 2 includes: placing different dependent properties into the same list, looping through the list in implementation, searching for a matching text dependent property in the object properties, exiting the loop after finding the matching text dependent property, and recording the name of the text dependent property; The list includes: HeaderProperty, TitleProperty, ContentProperty, DataProperty, TextProperty.
2. A general localization method in WPF according to claim 1, characterized in that: The acquisition of the localized resource file in step 2 includes: determining the name of the resource file according to the user's localization selection, and acquiring the displayed resource content according to the resource instance and the resource ID.
Citation Information
Patent Citations
WPF interface multi-language implementation system and method
CN116880837A