Use Resource Manager Information

You can create ASP.NET Web pages that use the information stored in the Resource Manager. The Resource Manager is a class utility that you can use to access the information stored in your resource files.

The culture for a user must be determined. One way you can determine this is to have the user select their culture from drop-down list. You need to make sure the response encoding is appropriate for the languages you are working with in your application. You can use UTF-8 to support multiple languages. You can add aliases to any namespaces for convenience.

You can use the Page_init function for reading the Resource Manager variable out of the application variable. When you have access to the Resource Manager object, you can pull the appropriate string out of the Resource Manager according to the culture. Once you have the string, you can then update the <span> on the page to display the localized statement.

You can build the ASP.NET Web page that will be used to access the data in the resource file and display a statement based on the culture selected.

USE RESOURCE MANAGER INFORMATION

USE RESOURCE MANAGER INFORMATION

Q Open the

GenericTemplate.aspx template from the Code Templates directory.

0 Create a drop-down list that has several languages from which to choose.

-0 Add a submit button control.

Add the @Page directive to the page and set the ResponseEncoding attribute to UTF-8, as well as the Language attribute to C#.

Add an alias to the System.Globalization, System.Resources, and System.Threading namespaces.

Q Open the

GenericTemplate.aspx template from the Code Templates directory.

Add a heading to the page. Add a message to the user. _Q Add a form control.

0 Create a drop-down list that has several languages from which to choose.

-0 Add a submit button control.

—Q Add a span control on the form to display messages.

Add the @Page directive to the page and set the ResponseEncoding attribute to UTF-8, as well as the Language attribute to C#.

Add an alias to the System.Globalization, System.Resources, and System.Threading namespaces.

Add the <script> tags.

Create a new ResourceManager variable.

LOCALIZATION AND ASP.NET

You can convert your resource files from their binary format into XML-formatted files. For example, you can use the resgen at the command line to convert the text.en-us.resources file into an XML file named text.en-us.resx.

Example:

resgen text.en-us.resources text.en-us.resx

You can convert from XML back to text as well. For example, you can use resgen at the command line to convert the text.en-us.resx file into an XML file named newtext.en-us.txt.

Example:

resgen text.en-us.resources newtext.en-us.resx

% Untitled - Notepad

File Edit Format Help

<%@Page Language-'C#" ResponseEncoding-'utt-8" %> ■e%@linport Namespace-'System.Globallzation"%> <%@lmport Namespace-'System.Resources"%-<%@lmport Namespace="System.Threading"%> iHTML> •=HEAD>

«SCRIPT LANGUAGE-'C#" RUNAT="Setver"> ResourceManager resourcemanagerWelcomeStatement;

void Pagejnlt(0bject sender, EventArgs args) { resourcemanagerWelcomeStatement - (ResourceManager)

Application["applicationResourceManager"];

void SubmltBtn_Clicklobject Source, EventArgs e) {

string stringCulture = dropdownllstCultures.Selectedltem.Value; Thread.CurrentThread.CurrentCulture = new Culturelnfo(stringCulture), Thread CurrentThread.CurrentUICulture - new Culturelnfo(strinqCulture)

spanMessage.InnerHtml ^ resourcemanaqerWelcomeStatement.GetStrlngf'welcomestatement")

Add the Page_Init event handler that reads the Resource Manager from the application variable.

le Create the SubmitBtn_Click function.

E Set the

CurrentCulture and CurrentUICulture based on the selection in the dropdown list.

le Set the message using the Resource Manager.

E Save the file and request it § Click the Submit button from the Web server.

le Click 2 to select a culture.

-■ The message is localized according to the culture selected.

Was this article helpful?

0 0

Post a comment