Universal localization method in WPF
By designing a localizeProvider with additional attributes, the unified localization binding of controls in WPF is realized, which solves the problems of complex binding and lack of universality in the existing technology, and improves development efficiency.
Patent Information
- Application Number
- CN202510429722.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-08
- Publication Date
- 2025-05-13
- Estimated Expiration
- 2045-04-08
AI Technical Summary
In WPF, the existing localization method requires developers to make complex bindings based on the properties of different controls, which lacks universality, resulting in complex and inefficient development processes.
A general localization method is proposed. By designing the localizeProvider for additional attributes, the contentId additional attributes are used to automatically find the control's dependency text attributes and resources, and a unified binding method is realized.
It simplifies the development process and improves development efficiency, so that all controls can be localized and bound through ContextId, realizing unified localized management.
Smart Images

Figure CN119987742A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of software development, 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 in line with international standards, the software will integrate support for multiple languages. Users from 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, and there is no 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] Among them, the method of realizing localization through DynamicResource requires developers to perform corresponding binding according to 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 writing 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 attributes and provides a unified binding method. All controls can use ContextId to complete localization binding.
[0006] A general localization method in WPF provided by this application adopts the following technical solution: A general localization method in WPF includes the following steps: Step 1: Prepare different localized resource Resx files, such as Chinese resource file Resources.zh-CN.resx, English resource file Resources.en.resx, French resource file Resources.fr.resx, etc. The corresponding resources in these resource files have 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 the localization resource file. After obtaining the control text dependent property name, it binds and sets the value of the dependent property through object reflection.
[0007] 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 (such as selecting Chinese, using the Resources.zh-CN.resx file name; selecting English, using Resources.en.resx, and using ResourceManager to initialize a new instance), and obtaining the displayed resource content through the resource instance and then according to the resource ID.
[0008] Furthermore, the automatic search for dependent text properties of controls 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.
[0009] In summary, the present application includes at least one of the following beneficial technical effects: The present invention provides a universal localization method for developers, simplifies the development process, and further improves development efficiency. BRIEF DESCRIPTION OF THE DRAWINGS
[0010] Figure 1 This is a flowchart of the application; Figure 2 This is the English display effect diagram of this application; Figure 3 This is the Chinese display effect diagram of this application. DETAILED DESCRIPTION
[0011] 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; obviously, the described embodiments are only part of the embodiments of the present application, rather than all the embodiments, and all other embodiments obtained by ordinary technicians in this field based on the embodiments in the present application without making creative work are within the scope of protection of the present application.
[0012] In the description of the present application, it should be noted that the terms "upper", "lower", "inner", "outer", "top / bottom" and the like indicate positions or positional relationships based on the positions or positional relationships shown in the drawings, and are only for the convenience of describing the present application and simplifying the description, and do not indicate or imply that the device or element referred to must have a specific position, be constructed and operated in a specific position, and therefore cannot be understood as limiting the present application. In addition, the terms "first" and "second" are used for descriptive purposes only, and cannot be understood as indicating or implying relative importance.
[0013] In the description of this application, it should be noted that, unless otherwise clearly specified and limited, the terms "installed", "provided with", "set / connected", "connected", etc. should be understood in a broad sense. For example, "connected" can be 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 it can be indirectly connected through an intermediate medium, or it can be the internal communication of two components. For ordinary technicians in this field, the specific meanings of the above terms in this application can be understood according to specific circumstances.
[0014] The present application is further described in detail below in conjunction with the accompanying drawings.
[0015] This embodiment discloses a general localization method in WPF, including the following steps: Step 1: Prepare different localized resource Resx files, such as Chinese resource file Resources.zh-CN.resx, English resource file Resources.en.resx, French resource file Resources.fr.resx, etc. The corresponding resources in these resource files have 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 the localization resource file. After obtaining the control text dependent property name, it binds and sets the value of the dependent property through object reflection.
[0016] Step 1 is an operation well known to those skilled in the art and will not be described in detail. Step 2 is described in detail below. Figure 1 As shown: The acquisition of localized resource files 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.
[0017] 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.
[0018] The present invention provides a universal localization method for developers, simplifies the development process, and further improves development efficiency.
[0019] 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, the controls that need to be displayed locally are set by adding the attribute "local:LocalizeProvider.ContentID". The detailed implementation is as follows: <Window x:Class="WpfApp5.MainWindow" xmlns="http: / / schemas.microsoft.com / winfx / 2006 / xaml / presentation" xmlns:x="http: / / schemas.microsoft.com / winfx / 2006 / xaml" xmlns:d="http: / / schemas.microsoft.com / expression / blend / 2008" xmlns:mc="http: / / schemas.openxmlformats.org / markup-compatibility / 2006" xmlns:local="clr-namespace:WpfApp5" mc:Ignorable="d" local:LocalizeProvider.ContentID="WindowTitle" Height="300" Width="600"> <grid> <Grid.ColumnDefinitions> <columndefinition Width="*" / > <columndefinition Width="200" / > < / Grid.ColumnDefinitions> <tabcontrol> <tabitem local:localizeprovider.contentid="TabItem1"> <grid> <Grid.RowDefinitions> <rowdefinition Height="2.5*" / > <rowdefinition Height="*" / > <rowdefinition Height="*" / > < / Grid.RowDefinitions> <button Grid.Row="1" Width="80" Height="30" local:LocalizeProvider.ContentID="Button">< / button> <menu grid.row="2" width="200" height="30" local:localizeprovider.contentid=""> <menuitem local:localizeprovider.contentid="MenuItem"> <menuitem local:LocalizeProvider.ContentID="SubMenuItem1"> <menuitem local:LocalizeProvider.ContentID="SubMenuItem2"> <menuitem local:LocalizeProvider.ContentID="SubMenuItem3"> < / menuitem> < / menu> <groupbox local:localizeprovider.contentid="GroupBox"> <stackpanel> <textblock Grid.Row="0" Width="80" Height="30" Background="AliceBlue" local:LocalizeProvider.ContentID="TextBlock" / > <radiobutton Width="100" local:LocalizeProvider.ContentID="RadioButton" Margin="5" / > <checkbox Width="100" local:LocalizeProvider.ContentID="CheckBox" Margin="5" / > <combobox width="100" selectedindex="0" margin="5"> <comboboxitem local:LocalizeProvider.ContentID="item1" / > <comboboxitem local:LocalizeProvider.ContentID="item2" / > <comboboxitem local:LocalizeProvider.ContentID="item3" / > < / combobox> < / stackpanel> < / groupbox> < / grid> < / tabitem> <tabitem local:localizeprovider.contentid="TabItem2"> < / tabitem> < / tabcontrol> <stackpanel grid.column="1" orientation="Vertical" verticalalignment="Center"> <button x:Name="btnCh" Content="中文" Width="80" Height="30" Click="btnCh_Click" Margin="5">< / button> <button x:Name="btnEn" Content="英文" Width="80" Height="30" Click="btnEn_Click">< / button> < / stackpanel> < / grid> .
[0020] The above are all preferred embodiments of the present application, and the protection scope of the present application is not limited thereto. Therefore, any equivalent changes made according to the structure, shape, and principle of the present application should be included in the protection scope 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 the localization resource file. After obtaining the control text dependent property name, it binds and sets the value of the dependent property through object reflection.
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.
3. A general localization method in WPF according to claim 2, characterized in that: The automatic search for the dependent text properties of the control in step 2 includes: putting different dependent properties into the same list, looping the list in implementation, searching for the matching text dependent property in the object properties, exiting the loop after finding it, and recording the text dependent property name.
4. A general localization method in WPF according to claim 3, characterized in that: The list includes: HeaderProperty, TitleProperty, ContentProperty, DataProperty, TextProperty.
Citation Information
Patent Citations
WPF interface multi-language implementation system and method
CN116880837A
Method and device for presenting localized content and medium
CN117251245A
Information processing method and device, equipment and storage medium
CN118689757A
Method for implementing language resource localization of software
CN1547116A
System and method for automated localization of software applications and user interfaces
US20240242038A1