Professional DotNetNuke 4: Open Source Web Application Framework for ASP.NET 2.0 (Programmer to Programmer)

The API

The DotNetNuke.Services.Localization.Localization class provides the methods necessary for localizing strings. These methods are described in Table 11-1.

Table 11-1: Localization Methods

Method

Description

AddLocale

Used to add a locale to the list of supported locales in the App_GlobalResources/Locales.xml file.

GetResourceFile

Returns the path and filename of the resource file for a specified control.

GetString

Returns the localized string based on the resource key specified.

GetSupportedLocales

Returns the list locales from the App_GlobalResources/Locales .xml file.

GetSystemMessage

Localizes a string and replaces system tokens with personalized strings.

GetTimeZones

Returns a key/value pair collection of time zones.

LoadCultureDropDownList

Fills a DropDownList control with the supported cultures.

LoadTimeZoneDropDownList

Fills a DropDownList control with the list of time zones.

LocalizeDataGrid

Localizes the headers in a DataGrid control.

LocalizeRole

Localizes the three system roles.

The GetString Method

Of the methods in Table 11-1, the most widely used is GetString. It performs localization based on the resource key passed into it. GetString has five overloaded methods, detailed here:

The GetSystemMessage Method

The GetSystemMessage method is used throughout the core code to produce localized and personalized strings. For example, it is used frequently to send e-mail to registered users. The user registration page calls GetSystemMessage to localize the content of the e-mail that gets sent to the user. The e-mail contains personalized content, too, so rather than concatenating several dozen strings using the GetString method and wrapping them around personalized data, GetSystemMessage takes care of all of this with just one call.

GetSystemMessage has eight overloaded methods, described here:

When you use GetSystemMessage, you can specify several system tokens in the localized string. The tokens are used as keys to render property values from either the UserInfo or PortalSettings objects. Here's an example:

DotNetNuke.Services.Localization.Localization.GetSystemMessage(PortalSettings, _ "EMAIL_USER_REGISTRATION_PRIVATE_BODY", objNewUser, Me.LocalResourceFile)

This code calls the GetSystemMessage method to localize and personalize the body of an e-mail message that is sent to a newly registered user. The code is found in the Admin/Security/Register.ascx portal module. The MessageName parameter value is EMAIL_USER_REGISTRATION_PRIVATE_BODY. It is the resource key to look up the system message in the resource file. The resource key and associated translation from the resource file /Admin/Security/App_LocalResources/Register.ascx.resx is shown in Listing 11-1.

Listing 11-1: System Message Resource Example

<data name="EMAIL_USER_REGISTRATION_PRIVATE_BODY.Text"> <value> Dear [User:FullName], Thank you for registering at the [Portal:PortalName] portal website. Please read the following information carefully and be sure to save this message in a safe location for future reference. Portal Website Address: [Portal:URL] Username: [Membership:UserName] Password: [Membership:Password] Your account details will be reviewed by the portal Administrator and you will receive a notification upon account activation. Thank you, we appreciate your support... [Portal:PortalName] </value> </data>

GetSystemMessage first localizes the string within the <value> XML node. Then it iterates through the system tokens (enclosed in brackets), replacing the tokens with the appropriate property values. For example, in Listing 11-1 you can see the token [User:FullName]. This will be replaced with the FullName property value of the User object. In this case, the User object is the objUser object passed into the GetSystemMessage method. Listing 11-2 shows that the system message has been personalized and localized with the en-US locale.

Listing 11-2: System Message Rendered Example

Dear John Doe, Thank you for registering at the DotNetNuke portal website. Please read the following information carefully and be sure to save this message in a safe location for future reference. Portal Website Address: http://test.dotnetnuke.com Username: jdoe1234 Password: pwdjdoe1234 Your account details will be reviewed by the portal Administrator and you will receive a notification upon account activation. Thank you, we appreciate your support... DotNetNuke

Категории