Multi-language support in Silverlight 2 (I)

Source: Internet
Author: User
Introduction

Recently, the project needs to implement localization in the Silverlight 2 application. I thought the process was very simple, but I didn't expect the implementation to take a three-fold turn. Fortunately, the result is still good. This is the requirement. The default value is English when the user accesses the system for the first time. After the user selects a display language, the information is recorded locally and the selected language is displayed directly when the user accesses the system for the next time. Now I have recorded the entire implementation process and hope to help you.

This section describes how to implement multiple languages in Silverlight 2.

Preparations

We found that the ResourceManager class is provided in Silverlight 2, which means that it can be implemented through the resource file. First, create a folder in the Silverlight project, name it Resources, and add a resource file Strings. resx and add several values, as shown in:

This file will be used as the default resource file and set Access Modifier to Public. It will generate the Strings. Designer. cs file, as shown in:

 

Create resource files in several other languages, such as Chinese, French, and Norwegian. Note that resource files are named in Strings + language and culture, for the code of each language file, you can refer to MSDN.

The Access Modifier of these other files is set to No Code generation. With the above preparations, we will proceed with the implementation.

Implementation

With the above resources, we can easily think of using static resources in Silverlight to bind to the UI control. First, define the resource file:

Bind to the UI control. For example, place three buttons:

The last step is to specify the language culture and UI Language Culture of the current thread. We can implement this in the Application_Startup event, and set it to zh-CN, as shown in the following code:

At this point, we have found this error during compilation:

Type 'strings 'is not usable as an object element because it is not public or does not define a public parameterless constructor or a type converter

Obviously, Strings constructors are not accessible in public mode. Open Strings. design. as you can see in the cs file, its constructor is internal, which is easy to handle. Manually change it to public (maybe this problem will be solved in RTM ). However, it should be noted that each time after the Resource file is modified, it will change to the internal type again.

Problem

Now the compilation is successful, but the expected results are not displayed at runtime. We will first think about whether the resource file is downloaded locally after compilation, that is, whether it is packaged into the xap file. This is a good confirmation. We can decompress the xap file to see if it is as expected, as shown in:

In the xap file, except the Silverlight project assembly, there is nothing related to the resource file. There is a "brute force" method to solve this problem, that is, we copy the resource files in the bin \ Debug folder of the Silverlight project and manually package the xap files, appManifest in the xap file. manually add AssemblyPart To The xaml folder, as shown in the following code:

It turns out that this can indeed be supported in multiple languages. The problem is that after each compilation, the xap file will be generated again. Manual packaging is not a long term, so you must try to solve the problem completely. Maybe Silverlight 2.0 RTM does not exist.

Problem Solving

Open the Silverlight project file (. csproj), you can see that there is a SupportedCultures item. If you set it here, the resource file will be automatically packaged when the Silverlight project is compiled, as shown in, we add a zh-CN:

Decompress the xap file and check the effect. Now, the zh-CH resource file is packaged in the xap file:

Run the program and you can see that it is displayed as Chinese:

 

At this point, we can finally breathe a sigh of relief. Next we will implement other functions.

Add Local Storage

Now we need to implement the local storage function. This is not difficult. It can be achieved through the isolated storage in Silverlight 2. After you select a language, save it locally:

Then, when the application starts, read the Configuration:

Now let's take a look at the effect:

English

Chinese:

 

Summary

Now, the simple multi-language support is implemented. In fact, there are other problems that have not been solved. I will analyze them in the next article. Finally, I wrote another sentence: Silverlight 2 Beta 2's multi-language support is really...

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.