WPF uses x:static for multi-language support

Source: Internet
Author: User

Original: WPF uses x:static multi-language support

To allow the program to support multiple languages, it is common to store the strings that need to be displayed in a static property of a resource class.

<!--[if!supportlists]--> <!--[endif]-->

Microsoft's WPF program multi-lingual Support official solution: using resource, and the resource by language compiled into a standalone DLL, the program will automatically load the most appropriate resources according to the system's current language settings. (This method is less flexible, and does not meet the diverse needs, so the online variety of multi-lingual solutions.) Here is a further explanation of the official plan.

Use XML to save language files: Put them in just because there is really such an explanation on the internet, although there is no practical value ..., resource is XML, and you define an XML with yourself, XmlDataProvider, and xml-based Data Binding, watching all tired ...
Use Project resource: Similar to the above, but put the strings in project resource, then use the ObjectDataProvider, and then the data Binding.

Assembly language: All languages supported in each assembly, setting the software language with a configuration file, and more than Microsoft's, but there are also multiple language support issues for WPF programs.

<!--[if!supportlists]--><!--[endif]--><!--[if!supportlists]--><!--[endif]--><!--[if !supportlists]--><!--[endif]-->

None of the above scenarios solves both of these problems at the same time:

<!--[if!supportlists]--> <!--[endif]-->

Switch languages at run time.

Add a new language without having to recompile the software.

<!--[if!supportlists]--><!--[endif]-->
Here, we introduce a more flexible, multi-language support scheme for WPF programs that solves both of the above issues.

The basic way is to use resource, except that resource is loaded at runtime. The structure of the solution is as shown.

The resource files for each language file are placed in the Resources/langs folder, which is not compiled into assembly, and the file structure is compiled as shown, and the language files are copied to the output folder as is.

Let's take a look at how the program works, and then look at the code more intuitively.

Here is the code for this interface.

  1. MainWindow
  2. <window x:class= "Localization.demowindow"
  3. Xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"
  5. Xmlns:c= "Clr-namespace:localization.backend.commands"
  6. Title= "{DynamicResource MainWindowTitle}"
  7. Width= "height=">
  8. <dockpanel Lastchildfill= "False">
  9. <menu dockpanel.dock= "Top">
  10. <Menu.CommandBindings>
  11. <x:static Member= "c:languagecommands.openlanguagebinding"/>
  12. </< span>menu.commandbindings>
  13. <menuitem Header= "{DynamicResource Languagemenuheader}">
  14. <menuitem Header= "{DynamicResource Englishmenuheader}"
  15. click= "Onloadenglishclick"/>
  16. <menuitem Header= "{DynamicResource Chinesemenuheader}"
  17. click= "Onloadchineseclick" />
  18. <Separator/>
  19. <menuitem Command= "C:languagecommands.openlanguage"
  20. Header= "{DynamicResource openlanguagefilemenuheader}"/>
  21. </< span>menuitem>
  22. </< span>menu>
  23. </< span>dockpanel>
  24. </< span>window>

All the text on the interface uses DynamicResource to refer to the string in the resource file. The format of the resource file is as follows (example of an English resource file):

  1. <resourcedictionary xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  2. xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"
  3. xmlns:s= "Clr-namespace:system;assembly=mscorlib">
  4. <s:string x:key= "MainWindowTitle">Localization Demo</< span>s:string>
  5. <s:string x:key= "Languagemenuheader">_language</< span>s:string>
  6. <s:string x:key= "Englishmenuheader">_english</< span>s:string>
  7. <s:string x:key= "Chinesemenuheader"> Chinese (_c)</< span>s:string>
  8. <s:string x:key= "Openlanguagefilemenuheader">_open Language File</< span>s: String>
  9. </< span>resourcedictionary>

Language files are not compiled into assembly and are somewhat different in use. The following is how the default load language for application is set in the App.xaml file.

  1. <application x:class= "Localization.app"
  2. Xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"
  4. Startupuri= "Ui\demowindow.xaml">
  5. <Application.Resources>
  6. <ResourceDictionary>
  7. <ResourceDictionary.MergedDictionaries>
  8. <resourcedictionary Source= "Pack://siteoforigin:,,,/resources/langs/en-us.xaml"/>
  9. </< span>resourcedictionary.mergeddictionaries>
  10. </< span>resourcedictionary>
  11. </< span>application.resources>
  12. </< span>application>

The previous content is basically nothing different from other scenarios, the following is the most important point, is how to switch the language when running it? The answer is, just replace the ResourceDictionary in the above code and the interface will refresh automatically. The following is the code that implements the Replace function.

  1. public class Languagehelper
  2. {
  3. <summary>
  4. ///
  5. </< span>summary>
  6. <param name= "Languagefilename"></< span>param>
  7. public static void Loadlanguagefile (String languagefilename)
  8. {
  9. Application.current.resources.mergeddictionaries[0] = new ResourceDictionary ()
  10. {
  11. Source = new Uri (Languagefilename, Urikind.relativeorabsolute)
  12. };
  13. }
  14. }

The parameter languagefilename can be an absolute path to a file, such as a C:\en-US.xaml or a relative path as in App.xaml. By the way, the "Pack://siteoforigin:,,," is nothing more than the directory where the program is currently executing.

Judging by the current test results, even the interface has a large number of fine-grained text. Switching language speed is also an instant thing, if slow, also because the XAML file is too large, read the file for a lot of time.

WPF program multi-language support flaw

In fact, this is the most important, a lot of articles about a technology will be praised this technology hype, but to the potential defects or problems avoided.

The flaw is that not all things can be run and are updated. For example, the last menu item is implemented with command, as shown in the following code:

    1. <menuitem Command= "C:languagecommands.openlanguage"
    2. Header= "{DynamicResource openlanguagefilemenuheader}"/>

Routeduicommand itself has defined the Text property to be displayed on the interface, and there is absolutely no need to set the header property for the MenuItem using the command. But why is it still set up here? Because there is no simple plan to change the command text after the automatic update of the interface. Because the command's Text property is not a dependency attribute. In order to automatically update the interface, you have to set the header property for MenuItem.

WPF uses x:static for multi-language support

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.