About making WPF software interfaces support globalization and Localization

Source: Internet
Author: User
About making WPF software interfaces support globalization and Localization
Zhou yinhui
A friend asked me how to enable the WPF Application to provide multi-language support (Chinese, English, Japanese .....), my suggestion is: (1) abandon the resource storage method in the WinForm era (2) use the dynamic resource features of WPF.
The following is a brief introduction and a simple DEMO:

1. How to store language resource files
We know that in the past (before WPF), we stored different languages on the interface in. in a resx file, use ResourceManager to obtain the corresponding resources and assign values to the interface text based on the Local CultureInfo. this is feasible. however, one problem in WPF is that we must Code these logics for each control in the background Code, because the XAML cannot be found. the. however, the vast majority of interface elements in WPF are defined in XAML, which is very troublesome. so we should discard this practice.
The resources in WPF inherit from the previous storage formats, but there are two rows in XAML and C # (or other), one is Content and the other is Resource, the former is "content", which is a loose resource link and the latter is a compiled and embedded resource. Note that, when setting the Resource file attributes, you will find another confusing Embedded Resource, which is also an Embedded Resource, but it is compressed into binary format, similar to the Embedded resources of WinForm in the past, the difference between Resource and Embedded Resource in encoding is that the former can be found through Uri, which is required by XAML, and the latter can be found through Stream, you can write c # code to find resources.
Therefore, Content is a good way to save language resources. Because it is a loose Resource Link, you can not re-compile it when adding a new Language Pack for the application, you only need to copy the language file of the object to the specified folder.

2. How to link interface elements to language text
This is what StaticResource and DynamicResource need to do, such as: <Button Content = "{DynamicResource OK}"/>

Until StaticResource or DynamicResource is used, it depends on whether you want to dynamically switch during the runtime, if you only switch the language StaticResource when the software is started.

3. OK:
3.1 create a project and create a new Lang folder in the project to save our language file

3.2 In the Lang folder, create "ResourceDictionary (WPF)" and name it "DefaultLanguage. and set its BuildAction to Page. This is a default language resource file that will be compiled (rather than loose links, this ensures that there is a default Interface language when the software Language Pack is lost or there is no corresponding language pack for a country or region): Here we use English as the default language: <ResourceDictionary xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: sys = "clr-namespace: System; assembly = mscorlib">

<Sys: String x: Key = "OK">
OK
</Sys: String>

<Sys: String x: Key = "Cancel">
Cancel
</Sys: String>

</ResourceDictionary>

Then, we add another language, such as Chinese, in The Lang folder, new "ResourceDictionary (WPF)", named "zh-CN.xaml", and set its BuildAction to Content, set CopyToOutputDirctory to "if new". In this way, the Chinese language file will be copied to the Lang directory under the application directory, this method should be used for other non-default language files. <ResourceDictionary xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: sys = "clr-namespace: System; assembly = mscorlib">

<Sys: String x: Key = "OK">
OK
</Sys: String>

<Sys: String x: Key = "Cancel">
Cancel
</Sys: String>

</ResourceDictionary>

3.3 In order for the coders to see the interface text as WYSIWYG In the designer (such as VS and Blend), we should add the default language resources to the application's resource list: <Application x: Class = "LangDemo. app"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri = "Window1.xaml"
>

<Application. Resources>
<ResourceDictionary>
<ResourceDictionary. MergedDictionaries>
<ResourceDictionary Source = "lang \ DefaultLanguage. xaml"/>
</ResourceDictionary. MergedDictionaries>
</ResourceDictionary>
</Application. Resources>

</Application>

In this way, we can use these resources in the designer:

We can see that the default language we load is English, and the corresponding English text is displayed on our window interface in the designer.

3.4 when the software is started, the corresponding localized language is loaded:

Public partial class App: Application
{

Protected override void OnStartup (StartupEventArgs e)
{
Base. OnStartup (e );
LoadLanguage ();
}

Private void LoadLanguage ()
{
CultureInfo currentCultureInfo = CultureInfo. CurrentCulture;

ResourceDictionary langRd = null;

Try
{
LangRd =
Application. LoadComponent (
New Uri (@ "Lang \" + currentCultureInfo. Name + ". xaml", UriKind. Relative ))
As ResourceDictionary;
}
Catch
{
}

If (langRd! = Null)
{
If (this. Resources. MergedDictionaries. Count> 0)
{
This. Resources. MergedDictionaries. Clear ();
}
This. Resources. MergedDictionaries. Add (langRd );
}


}
}

When the software is started, we load the corresponding language file based on the Local CultureInfo (if the language file exists). Because the text on the interface is not hardcoded, DynamicResource is used, when the background Resource changes, the reference on the foreground will also change dynamically. OK. After the software is started, our interface will be automatically switched. Chinese is displayed on my computer:

Of course, there is still a lot of content for localization, such as currency numbers and reading order. Here we only provide an idea. You can also download the DEMO here.

 

 

 

 

 

 

 

 

 

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.