Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [163]

By Root 5388 0
for global resources requires the App_GlobalResources folder and direct deployment of any RESX files.

Let’s find out more about what’s required to deal with globally defined resources.

Dealing with Global Resources


Using global resources programmatically entails writing for each page some code as shown next. The code will be invoked just before display:

protected void LocalizeTextInPage()

{

// For each control you expect in the page, retrieve the localized text

Label1.Text = MyResources.Literals.Welcome;

...

Label2.Text = HttpContext.GetGlobalResourceObject("globals.resx", "Description");;

...

}

If your global resources are stored through plain RESX files, you can retrieve it using either of the two expressions just shown. In addition to using the object expression that navigates into the class hierarchy of the resource assembly, you can also employ the GetGlobalResourceObject method of the HttpContext object. If the localized text resides elsewhere, the API for retrieving it is up to you.

Alternatively, if you prefer to take the declarative route, use the object expression within plain page markup and resort to the ASP.NET-specific $Resources expression builder for control attributes. Here’s an example:

$Resources refers to an ASP.NET built-in expression builder. It accepts a parameter that is a comma-separated string trailing the colon (:) symbol. The first token indicates the name of the RESX file that is the source of the localized text. The second token indicates the name of the resource item to read. There are no facilities to bind declaratively localized text stored outside of RESX files.

Dealing with Local Resources


Local resources are strictly page-specific in the sense that if it’s properly named after the ASPX source file, the content of a resource file can be referenced using direct syntax from the markup, as shown here:

meta:resourcekey="Label1_ResourceID" />

The resourcekey meta attribute indicates that property values for the Label1 control are to be taken from a page-specific resource file. If the resource file for the page contains an entry such as Label1_ResourceID.Text, the Text property of Label1 will be set to the stored value. The same can be done for any other properties of the control.

Resources and Cultures


A RESX file is a plain XML document. How can you distinguish a RESX file that represents French localized text from the RESX of German localized text? A RESX file name that doesn’t include culture information is assumed to contain language-neutral text with no culture defined.

To create a resource assembly for a specific culture—say, French—you need to name the resource file as follows: sample.aspx.fr.resx. The fr string should be replaced with any other equivalent string that identifies a culture, such as de for German or en for English.

When resources from multiple cultures are available in the AppDomain, the ASP.NET runtime machinery detects the underlying culture and picks up the matching resource file. I’ll return in a moment at how to set and change the culture programmatically.

Setting the Current Culture in .NET Applications


In the .NET Framework, the culture is set on the current thread through the CurrentCulture and CurrentUICulture properties. In general, both properties are necessary when you want to support multiple languages in a page or view. In fact, the two properties refer to distinct capabilities and have an impact on different areas of the user interface.

The CurrentCulture property affects the results of functions, such as the date, the number, and currency formatting. The CurrentUICulture property, on the other hand, determines the localized resource file from which page resources are loaded. The following code snippet shows a possible way to arrange a unit test aimed at testing whether culture-specific items are correctly retrieved. If you intend to test only whether resource files are being used as

Return Main Page Previous Page Next Page

®Online Book Reader