Resource File :

• Resource Files are most beneficial while developing multilingual Web applications
• A resource file is a XML file that contains the strings that we want to
a. Translate into different languages.
b. Can be updated dynamically so that user themselves can modify values in resource files once the application is deployed on the server without re-compiling the entire application itself.

• The resource file contains key / value pairs.
• Each pair is an individual resource.
• Key names are not case sensitive.

Types of Resources:

There are 2 types of Resource files :

1.Global Resource file :

This file is present in a specialized folder called /App_GlobalResources, located at the root of the application. All pages, user-controls, etc. can access these resources, so they typically are used as shared resources. The resources which should be available throughout the application are stored in this file.

2.Local Resource file :

These files are present in /App_LocalResources folder. These files follow the naming convention of the associated page, user-control, or master-page, along with the culture definition. These files are only accessible by the associated page.

Sample Code for Resource File :

I Created four label and one dropdown and based on the dropdown selection the language should change. I Created three resource files for English, French and Tamil. In Name Column [Key] I give the Control Name and the Value Column [Value] I give the Text to be displayed.

//To get the Default Language from the browser.
strDefaultLang = Request.Headers["Accept-Language"].Split(',')[0].Trim();

//To specify the Language to be displayed.
if(ddlLanguage.SelectedItem.Text == "English")
strDefaultLang = "en-us";
else if(ddlLanguage.SelectedItem.Text == "Tamil")
strDefaultLang = "ta-IN";
else if (ddlLanguage.SelectedItem.Text == "French")
strDefaultLang = "fr-FR";


//To set the Language for the Control to displayed:

System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo(Convert.ToString(strDefaultLang).ToLower());
lblAddress1.Text = Resources.Language.lbladdress1;
lblAddress2.Text = Resources.Language.lbladdress2;
lblName1.Text = Resources.Language.lblname1;

lblName2.Text = Resources.Language.lblname2;

Language is the name of the Resource file name and the Key name entered in the resource file are populated automatically. Select the key name to populate the value for the control.

The Resource file Name should be unique. the file name in this scenario is Language.resx for default Language [English]. Language.fr.resx for French, Language.ta.resx for Tamil.

Category : | Read More......