WPF & sivlerlight multi-language solution-localization

Source: Internet
Author: User

 

There are many ways to implement multiple languages. Recently, a WPF project needs to use multiple languages, with En-us as the basic language, so we finally find a solution.

1. using traditional. net resources. resx in WPF is feasible, but it is always easier to use WPF resource;

2. At first, the WPF resource file was unable to implement multi-language solutions. LaterCommunityFinally, I found this article to localize the application.ArticleSolved my problem.

3. The following describes my steps:

Step 1: Create the resource file language. XAML in the project root directory as follows:

<Resourcedictionary xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation%22
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml%22
Xmlns: system = "CLR-namespace: system; Assembly = mscorlib">
<System: String X: uid = "mainopen" X: Key = "mainopen"> open </system: String>
<System: String X: uid = "mainnew" X: Key = "mainnew"> New </system: String>
<System: String X: uid = "mainproperty" X: Key = "mainproperty"> pproperty </system: String>
<System: String X: uid = "mainsave" X: Key = "mainsave"> Save </system: String>
<System: String X: uid = "mainpagenew" X: Key = "mainpagenew"> new page </system: String>
<System: String X: uid = "mainpageproperty" X: Key = "mainpageproperty"> page property </system: String>
<System: String X: uid = "mainpagedel" X: Key = "mainpagedel"> Delete page </system: String>
</Resourcedictionary>

Step 2: In app. XAML, pay attention to source = "/language. XAML" before "/". I started this place for a long time:

<Application. Resources>

<Resourcedictionary>
<Resourcedictionary. mergeddictionaries>
<Resourcedictionary source = "/language. XAML"/>
</Resourcedictionary. mergeddictionaries>
</Resourcedictionary>
</Application. Resources>

In this way, you canProgramThis resource can be used anywhere in the. As for how to use resource files in WPF and Silverlight, it is not within the scope of this article. Please refer to the WPF resource tutorial.

Test the use of string resources, create a window wintest. Xmal

xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation%22
xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml%22
X: class = "Exio. nr. WPF. designer. wintest "
X: Name =" window "
Title =" wintest "
width =" 640 "Height =" 480 ">

<Stackpanel virtualizingstackpanel. configuralizationmode = "reconfiguring" horizontalalignment = "Left" verticalalignment = "TOP">
<Label content = "{dynamicresource mainnew}"/>
<Label content = "{dynamicresource mainopen}"/>
<Label content = "{dynamicresource mainpagedel}"/>
<Label content = "{dynamicresource mainpageproperty}"/>
<Label content = "{dynamicresource mainsave}"/>
</Stackpanel>
</WINDOW>

 

Step 3: Use msbuilder to compile

Open the project file XXX in text. csproj: Add <uiculture> en-US </uiculture> under each propertygroup. This step is copied from the Internet and an error will be reported later.

Run msbuilder to compile the SDK. For details about the command line, refer to this article on application Localization:

Msbuild/T: updateuid projname. csproj
Msbuild/T: checkuid projname. csproj
Msbuild projname. csproj

Step 4: troubleshoot

OK. After compilation, we can run the program and start the program. A series of reports will not be found in the app. the XAML error eventually found that the resource file of the corresponding language could not be found, because my system is a Chinese system, that is, the resources I compiled are en-us, but what should I do if my system needs ZH-CN?

Two solutions:

1) modify the project file XXX. csproj, use <uiculture> ZH-CN </uiculture> in each propertygroup, and compile it with msbuilder. The operation is okay;

2 ). however, all my resources are in English, and the project is huge. The first scheme is used, and I feel an alternative. There is also a method in our assemblyinfo. add a line [Assembly: neutralresourceslanguage ("En-us", ultimateresourcefallbacklocation. satellite)], this is the final rollback resource, when the system is Chinese, first go to the resources under ZH-CN, if ZH-CN \ projname. resources. when the DLL is unavailable, for example, if the DLL is deleted, the Chinese version of Windows will use en-US \ projname. resources. DLL. OK. Use msbuilder to compile and run the program;

In this way, we compile and generate the en-US resource file.

Step 4: Use locbaml to export (PARSE) resources and generate (trans) new language resources

For more information, see localized applications. However, in my project, I mainly use string resources. There are too many things exported through locbaml, which is hard to find. Second, it is easy to modify and make mistakes, what should I do?

I modified locabaml, and in translationdictionarieswriter. Write, there are 65 rows (write out each resource foreach). I only export string resource files.

Foreach (dictionaryentry entry in dict)
{
Bamllocalizableresourcekey key = (bamllocalizableresourcekey) entry. Key;
Bamllocalizableresource resource = (bamllocalizableresource) entry. value;
If (! Key. classname. Equals ("system. String") continue;
// Column 1: baml stream name
Writer. writecolumn (bamlstreamlist [I]. Name );

// Column 2: localizable Resource Key
Writer. writecolumn (locbamlconst. resourcekeytostring (key ));

......

Note: This is only suitable for my solution, because I have not used other resource files. For more information, see.

OK, use locbaml.exe/generate en-US/projname. Resources. dll/Trans: projName-cn.csv/out: e: \/cul: ZH-CN

Copy it to the ZH-CN folder in your running directory.

In this way, the language cannot be switched, But it determines your language based on your operating system, so that you do not need to select a language, the next article will introduce how to use WPF resource to implement language switching.

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.