Browse DevX
Sign up for e-mail newsletters from DevX


Localizing ASP.NET 2.0 Applications : Page 3

As we compete in the global economy, the challenges in globalizing include understanding the language and culture of the local audience. An application written for the American market may not be useable in the Asian market. Learn to localize your application for the world market.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Explicit Localization
In implicit localization, each Web Form has a separate set of resource files. This method is useful for cases where you need to localize the output of controls. However, it is not feasible when you need to localize a large amount of text or have needs to constantly reuse them (such as welcome or error messages). In this case, explicit localization is more useful. Explicit localization allows you to define a set of resources that can be used by all the pages in your project.

To illustrate explicit localization, create a new folder named Resources (in the same project as discussed in the previous section) in Solution Explorer and then select Add New Item...Select Assembly Resource File (see Figure 10). Name the resource file Resource.resx. Make a copy of the resource file and name it Resource.zh-CN.resx.

Figure 10: Adding a resource file to the project.
Figure 11: The new resource files under the Resources folder.
Figure 11 shows what the Solution Explorer should now look like.

In the Resource.resx file, enter a new key/value pair as shown in Figure 12. Do the same for Resource.zh-CN.resx; this time the string is in Chinese.

Figure 12: Creating resource strings.
Add the following event handler for the Submit button in Registration.aspx.vb:

Sub btnSubmit_Click(ByVal sender As _ Object, ByVal e As System.EventArgs) Dim message As String = _ Resources.Resource._ ThankYouMessage.ToString Dim script As String = "alert('" & _ message & "');" Page.ClientScript.RegisterClientScriptBlock( _ Me.GetType, "MyKey", script, True) End Sub

The Resources class provides a programmatic way for dynamically accessing the resources located in the resource file. IntelliSense will automatically display the keys defined in the resource files. The message retrieved from the resource file is then displayed on the client side as a pop-up window (see Figure 13) using the RegisterClientScriptBlock method.

Figure 13: Displaying a localized string.
Figure 14: Binding a control to the Resource file.
Besides programmatically retrieving values from resource files, you can also do it declaratively. For example, you can bind a Label control's Text property to the resource file by using the (Expressions) field in the Properties window (see Figure 14).

Alternatively, you can also do it declaratively via the Source View:

<asp:Label ID="Label1" Runat="server" Text="<%$ Resources:Resource, ThankYouMessage %>"> </asp:Label>

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date