Wednesday, 18 April 2012

Create/Read global resource file code asp.net

How to Create/Read global resource file using asp.net code behind c#:

How to create Global resource file:

Steps:
1.Right click on web project and select Add --> Add ASP.Net Folder option
2.App_GlobalResources folder will be added.
3.Right click on "App_GlobalResources" folder and select Add --> Resource File --> give name  --> ok

example: 

readglobalresoircefile.resx  // global/default language
readglobalresoircefile.en-US.resx  // english language
readglobalresoircefile.fr-FR.resx  // french language
readglobalresoircefile.es-ES.resx  // spanish language

4.Add new key value. for example:
Key        value
------     ------
aa         English aa test
bb         English bb test


How to read/get Global resource file key value from code behind asp.net C#

Add the following Namespaces.

using System.Resources;
using System.Globalization;

Add the following code inside your custom function/method or in page load.

Source code sample:

ResourceManager rm = new System.Resources.ResourceManager("Resources.readglobalresoircefile", System.Reflection.Assembly.Load("App_GlobalResources"));
string ress = "en-US";  // to read us english resource.. if it is french then the culture code will be fr-FR likewise.
CultureInfo ci = new System.Globalization.CultureInfo(ress);
string resstring = rm.GetString("aa", ci);  // where aa indicates global resource file key value

we could assign this "resstring" string variable to any of asp.net control like textbox, label..

No comments:

Post a Comment