With ASP.NET applications, localization happens in a similar way to Windows applications. Chapter 7, “ASP.NET Pages,” discusses the functionality of ASP.NET applications; this section discusses the loclization issues of ASP.NET applications. ASP.NET 2.0 and Visual Studio 2008 have many new features to support localization. The basic concepts of localization and globalization are the same as discussed before. However, some specific issues are associated with ASP.NET.
As you have already learned, with ASP.NET you have to differentiate between the user interface culture and the culture used for formatting. Both of these cultures can be defined on a web and page level, as well as program.matically. To be independent of the web server’s operating system, the culture and user interface culture can be defined with the <globalization> element in the configuration file web. config:
The user can configure the language with the browser. With Internet Explorer, this setting is defined with the Language Preference options (see Figure 21-.19). If the page language should be set depending on the language setting of the client, the culture of the thread can be set programmatically to the language setting that is received from the client. ASP.NET 2.0 has an automatic setting that does just that. Setting the culture to the value Auto sets the culture of the thread depending on the client’s settings.
In dealing with resources, ASP.NET differentiates resources that are used for the complete web site and resources that are needed only within a page
. If a r source is used within a page, you can create resources for the page by selecting the Visual Studio 2008 menu Tools ¢ Generate Local Resource in the design view. This way, the subdirectory App_LocalResources is created where a resource file for every page is stored. These resources can be localized similarly to Windows applications. The association between the web controls and the local
resource files happens with a meta: resourcekey attribute as shown here with the ASP.NETLabel control. LabelResour~el is the name of the resource that can be changed in the local resource file:
For the resources that should be shared between multiple pages, you have to create a subdirectory, Appl_GlobalResources. In this directory, you can add resource files, for example, Messages. resx
with its resouces. Toassociate the web controls with these resources, you can use Expressions in the property editor. Clicking the Expressions button opens the Expressions dialog (see Figure 21-20).Here, you can select the expression type Resources, set the name of the ClassKey (which is the name of the .resource file – here, a strongly typed resource file is generated), and the name of the ResourceKey, which is the name of the resource.
In the ASPXfile, you can see the association to the resource with the binding expressions syntax <%$:
Localization with WPF
Visual Studio 2008 doesnot have great support for localization of WPF (Windows Presentation Foundation) applications. However, you do not have to wait until the next version to localize your WPF application. WPF has localization support built-in from the beginning, and you have several options to localize your applications. Youcan use .NET resources similar to what you’ve done with Windows Forms and ASP.NETapplications, but you can also use an XAML(XMLfor Applications Markup Language) resource dictionary.
These options are discussed next. You can read more about WPF and XAMLin Chapters 34, “Wmdows Presentation Foundation,” and 35, “Advanced WPF.”
To demonstrate the use of resources with a WPF application, create a simple WPF application containing just one button, as shown in Figure 21-21.
The XAML code for this application is shown here
With the handler code for the click-event of the button, just a message box containing a sample message pops ~p:
You can add. ET resources to a WPF application similar to the way you have with other applications. Define the resources named ButtonlText and ButtonlMessage in the file Resources. resx.
To use the generated resource class, you need to change the XAML code. Add an XML narnespace alias to reference the .NET namespace Wrox. ProCSharp. Localization. Properties as shown. Here, the alias is set to the value props. From XAML elements, properties of this class can be used with the x: Static markup extension. The Content property of the Button is set to the ButtonlText property of the Resources class.
Because resources added in a Visual Studio Solution lulve the internal access modifier with the resource class and it,Smembers, and there is not an option to change this, you should add a custom build step to use the Resgen utility with the /publicClass option and add the generated class to the project. Otherwise, the WPF project won’t compile
To use the .NET resource from code-behind, you can just access the ButtonlMessage property directly in the same way you did with Wmdows Forms applications:
Localization with XAML
Instead of using .NETresources for localization of WPF applications, you can work directly with XAML to create localized content. The steps for a localization process can be described by these actions:
¤ Create a satellite assembly from the main conte
¤ Use resource dictionaries for localizable content
¤ Add x :~d attributes to elements that should be localized
¤ Extract localization content from an assembly
¤ Translate the content
¤ Create satellite assemblies for ~very language
When compiling a WPFapplication, the XAMLcode is compiled to a binary format’ BAMLthat is stored into an assembly. Tomove the BAMLcode from the main assembly to a separate satellite assembly, you can change the . csproj build file and add a <UICul ture> element as shown as a child to the <PropertyGroup> el’ement.The culture, here en-US, defines the default culture of the project. Building the project with this build-setting creates a subdirectory en-US and creates a satellite assembly containing BAMLcode for the default language
Separating the BAMLinto a satellite assembly, you should also apply the NeutralResourcesLanguage attribute and sUllU’lythe resource fallback location to a satellite assembly. If you decide to keep BAMLin the main assembly (by not defining the <UICul ture> to the . csproj file), the • Ultima~esourceFallbackLocation should be set to MainAssembly
For code-behind content that needs to be localized, a resource dictionary can be added. Using XAML, you can define resources within the <ResourceDictionary> element as shown. With Visual Studio, you can create a new resource dictionary by adding a new resource dictionary item and defining the file name. In the example here, the resource dictionary contains one string item. To.get access to t:l¥ Sering type from the System narnespace, an XMLnarnespace alias needs to be defined. Here, the alias system
is set to the clr-namespace System in the assembly mscorlib. The string that is defined can be accessed with the key messagel. This resource dictionary is defined in the file LocalizedStrings. xaml.
To have the resource dictionary available with the application, it must be added to the resources. If the resource dictionary would be required only within a window or just within a specific WPF element, it can be added to the resources collection of the specific window or WPF element. Here, the resource dictionary is added to the file App. xaml within the <Applica tion> element, and thus is available to the complete application.
With WPF elements, the x: uid attribute is used as a unique identifier for elements that need localization. You don’t have to apply this attribute manually to the XAML content; instead you can use the msbuild command with this option:
When you call this command in the directory where the project file is located, the XAML files of the project are modified to add an x: Uid attribute with a unique identifier to every element. The same XAML as shown .before now has the new attributes applied:
If you change the XAMLfile after x: Uid attributes have been added, you.can verify correctness of the x: Uid attributes with the option It: checkuid.
Compiling the project creates a satellite assembly containing the B~\.fL code. From this satellite assembly, you can extract the content that needs to be localized with classes from the System. windows .Markup.Localizer namespace. With the Windows SDK you will find the sample program LocBaml.This program can be used to extract localization content from BAML.Youneed to copy the executable, the satellite assembly with the default content, and LocBaml . exe to one directory and start the sample program to produce a . csv file with the localization content: LocBaml Iparse WPFandXAMLresources.resources.dll lout: trans.csv You can use Microsoft Excel to open the. csv file and translate its content. An extract from the ‘.csv file that lists the content of the button and the message from the resource dictionary is shown here: WPFandXAMLResources.g.en-US.resources:localizationstrings.bamI, system:String_l:System.String.$Content,None,True
,True”English Message WPFandXAMLResources.g.en-
. esources:windowl.baml, buttonl:System.Windows.Controls.ContentControl.Content,Button,True,True” English Button This file contains.these field
¤ Name or’the BAML
¤ The identifier of the resource
¤ The category of the resource that gives the type of the content
¤ A Boolean value if the resource’is visible for translation (readable
¤ A Boolean value if the resource can be modified for the translation (modifiable
¤ Localization comments
¤The value of the resource
After localization of the resource, you can create a new directory for the new language (for example, de for German). The directory structure follows the same convention as shown earlier in this chapter with satellite assemblies. With the LocBaml tool, you can create satellite assemblies with the translated content:
LocBaml Igenerate WPFandXAMLResources.resources.dll Itrans:trans_de.csv lout:, ../de Icul:de-DE
Now the same rules for setting the culture of the thread and finding satellite assemblies as shown with Windows Forms applications apply here.