There are already many articles about Silverlight localization. In the garden, NASA's Silverlight localization is a good article. This article will analyze this knowledge from other perspectives. This article will include the following content:
- Significance of localization/globalization
- Using. resx in Silverlight to implement localization/globalization
- Implement localization/globalization through mvvm in Silverlight
- Example and source code
Significance of localization/globalization
Localization/globalizationIt is a problem that will inevitably occur when our applications/products reach a certain stage, I don't think we should consider localization/globalization in the early stages of the product because the idea that "our customers are all domestic companies. The reason is:
1. We will never be able to predict future events.
2. There are two concepts of domestic companies: local companies and multinational companies. Multinational companies do not have to say that even projects used in local companies will inevitably encounter "old foreign inspections.
3. In the early stage of development, it is far easier to consider localization/globalization issues than to do such work in the maintenance phase. Looking at the previous project code (especially it may not be written by yourself) and modifying the Code is one of the most annoying tasks for programmers. It would be a nightmare if they encounter code in disorder.
4. The localization/globalization solution provided in. NET is simple, convenient, and powerful, and the cost increase is limited.
To sum up, unless it is a small project (such as a small enterprise website project), I suggest that you consider localization/globalization at the beginning of the product.
Localization/globalization in Silverlight
The Silverlight framework is a subset of the. NET Framework and provides complete support for localization and globalization. Similar to common. NET applications, the. resx file is used as a localized resource file. (Of course, you can also use other localization/globalization solutions, such as hard encoding to set constants. This article mainly describes how to use resource files)
The following uses project2007 of MS as an example to describe a classic scenario of using resx:
The words "new" appear in both. If we want to change it to a traditional or English version, then in the traditional way, we must open Visual Studio to modify the text of the two parts separately, and then try again and again. (Tens of thousands of software entries in some industries are the same)
After using the. resx file, we only need to modify the corresponding entry of the corresponding language.
In this layer, the use of. resx also follows the dry principle.
Note: In Silverlight, because the term "resource" has a special meaning, ". resx" is used in this article to indicate localized resource files.
Use. resx in Silverlight to implement localization/globalization
As the saying goes, "select only the right one, not the expensive one ". This sentence can also be understood as the best choice. In Silverlight, there are many ways to implement localization/globalization by using. resx. No solution is the best, which should be selected based on your preferences and the project's own situation (such as surrounding resources and average project team capabilities.
Here I will list some solutions. They all have their own advantages and disadvantages:
- Method 1: business application
After installing. Net WCF Ria services, you can find this template in the window for creating a Silverlight project. Projects created using this template will automatically create a resourcewrapper class under the helpers folder to wrap all. resx class instances.
The advantage is that the Code is automatically created by the template, saving a lot of effort.
- Method 2: Hard-coding in the background
A traditional method, such as writing in the background
public string ResourceLabelDate { get { return MyStrings.Label_Date; } }
Can be written at the front end
<TextBlock x:Name="lblDate"/>
Its advantages are: Code awareness, strong type, and front-end XAML clean and tidy.
- Method 3: mvvm, the main character of today
The background is the same
public string ResourceLabelDate { get { return MyStrings.Label_Date; } }
Front-end
<TextBlock Text="{Binding ResourceLabelDate}"/>
Advantages: The mvvm mode has inherent advantages. It is convenient to perform unit tests on viewmodel, and the front-end XAML is also very clean.
In addition, there are other methods such as localizationmanager produced by telerik, which will not be listed here.
In the next article, we will implement localization/globalization step by step through the mvvm mode from scratch and provide source code.