Create And Use Resources

You can use resource files to store localization information for your ASP.NET Web application, which enables you to separate all of the information that is specific to a language or a locale from the application functions. This capability makes it easier to add more languages to the application.

Resource files are composed of name/value pairs. Once you have created the resource file, you need to convert the file to a binary output by using the resgen command at the command line. To access the information in the resource file, you can use a class utility called a Resource Manager. You can load the

Resource Manager into an application variable by using the Application_OnStart event handler in the Global.asax.

You can save the resource files in a subdirectory to organize you files. If you want to do this, you can use the Server.MapPath function in the Global.asax to map to the subdirectory.

You can create a resource file and compile the resource file using the resgen command utility. You can then create a Global.asax file that that will load the resources into a Resource Manager.

CREATE AND USE RESOURCES

Type [strings].

Add the welcome statement=Welcome!

name/value pair.

□ Save the file with a .txt extension.

CREATE AND USE RESOURCES

C:\WINNT\System32\cmd.exe Microsoft Windows 2000 [Version 5.00.2195] CCD Copyright 1985-2000 Microsoft Corp.

C:\>cd C:\InetPubVwwwroot\resources

C:\InetpubVwwwroot\resources>resgen text.en-us.txt Head in 1 resources from "text.en-usTtxt" Writing resource file... Done.

C:\InetpubVwwwroot\resources>_

Type [strings].

■ Repeat steps 2 to 4 for each Open the command culture.

C:\WINNT\System32\cmd.exe Microsoft Windows 2000 [Version 5.00.2195] CCD Copyright 1985-2000 Microsoft Corp.

C:\>cd C:\InetPubVwwwroot\resources

C:\InetpubVwwwroot\resources>resgen text.en-us.txt Head in 1 resources from "text.en-usTtxt" Writing resource file... Done.

C:\InetpubVwwwroot\resources>_

■ Repeat steps 2 to 4 for each Open the command culture.

Note: The resource files are saved in a subdirectory called resources.

Note: This task works with three cultures (English, Spanish, and Japanese). You can expand to other cultures.

prompt and go to the directory where your resource files are located.

-Q Type the resgen command to create the resource file from the text file.

■ Repeat step 6 for each resource file.

LOCALIZATION AND ASP.NET

You can try and read the user language from the Web browser. You can put the following function in your Global.asax file to set the culture for all requests. It attempts to read the user language if possible and sets the language to en-us for a default value.

TYPE THIS:

void Application_BeginRequest(Object sender, EventArgs args) { try {

Thread.CurrentThread.CurrentCulture =

new CultureInfo(Request.UserLanguages[0]);

catch(Exception) {

Thread.CurrentThread.CurrentCulture =

new CultureInfo("en-us");

Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

RESULT:

The culture is set based on information that was sent from the user's Web browser.

□ Open your text editor to create a Global.asax file.

° Add aliases to System.Globalization, System.Resources, System.Threading, and System.IO.

— Save the file as Global.asax to the Web site.

□ Open your text editor to create a Global.asax file.

° Add aliases to System.Globalization, System.Resources, System.Threading, and System.IO.

_• Create the Application_OnStart event handler.

Create a Resource Manager application variable and initialize it.

— Save the file as Global.asax to the Web site.

■ You can now use the Application variable to access the information in the resource files.

Was this article helpful?

0 0

Post a comment