Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to retrieve resource values programmatically in ASP.NET

Author: Mike Green

Local and Global Resource Values can be retrieved programmatically.

 

The article will explain how we can retrieve resource values from local or global resource files programmatically.
 
  1. Open MS Visual Studio 2010
  2. File > New > Website > Visual C# or Visual Basic > ASP.NET Empty Web Site
  3. Select Web Location as File System and Click OK
  4. From Menu, Website > Add New Item > Select Web Form and Click Add
  5. Drag and drop three Labels or write code below in aspx page

    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
       
    <br />
       
    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
       
    <br />
       
    <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>

     
  6. Right click on website > Add ASP.NET Folder  > Click on App_LocalResources
  7. Right Click on the folder > Add New Item > Select Resource File and Click Add
  8. Rename the Resource.resx file as “Default.aspx.resx” and open file. Write “Label1” in first row of the Name column and write “Label Value in Local Resource File for English” in Value column.
  9. Right Click on “Default.aspx.resx” file and click Copy. Right Click on App_LocalResources folder and click Paste.
  10. Rename “Copy of Default.aspx.resx” file as “Default.aspx.fr-FR.resx” and open file. Write “Label1” in first row of the Name column and write “Label Value in Local Resource File for French” in Value column
  11. Now Right click on website > Add ASP.NET Folder > Click on App_GlobalResources. Right Click on the folder > Add New Item. Select Resource File and Click Add
  12. The file Name will be “Resource.resx”. Open file and write Write “Label2” in first row of the Name column and write “Label Value in Global Resource File for English” in Value column
  13. Right Click on “Resource.resx” file and click Copy. Right Click on App_GlobalResources folder and click Paste
  14. Rename “Copy of Resource.resx” file as “Resource.fr-FR.resx” and open file. Write “Label2” in first row of the Name column and write “Label Value in Global Resource File for French” in Value column
  15. Now write code below in code behind file of Default.aspx

    C#

    protected override void InitializeCulture() {
           UICulture = Request.UserLanguages[0];
           base.InitializeCulture();
    }

    VB.NET

    Protected Overrides Sub InitializeCulture()
           UICulture = Request.UserLanguages(0)
           MyBase.InitializeCulture()
    End Sub
     
    InitializeCulture() method has the responsibility to set the Culture and UICulture properties of the page. We have to override this method to set the UICulture property. This property establishes which global or local resource is to be loaded for the page. UserLanguage gets the sorted array of string for user preferred language. It is the property of Request object. At the end base InitializeCulture() method of the page is called. 

     
  16. Write code below in Page Load method

    C#

    Label1.Text = GetLocalResourceObject("Label1").ToString();
    Label2.Text = GetGlobalResourceObject("Resource", "Label2").ToString();
    Label3.Text = Resources.Resource.Label2;

    VB.NET
     
    Label1.Text = GetLocalResourceObject("Label1").ToString()
    Label2.Text = GetGlobalResourceObject("Resource", "Label2").ToString()
    Label3.Text = Resources.Resource.Label2
     
    GetLocalResourceObject() method is used to get resources from local resource files. It takes resource name as parameter.
    GetGlobalResourceObject() method takes resource class and resource name as parameter. These methods return object so we have to cast it to appropriate type.
    We can retrieve values from global resource files in another way. Every resource class lies under “Resources” namespace and resources of the class become members of that class. So the syntax to retrieve resource values by this pattern is Resources.Class.Resource. using this pattern, values for label2 resources is retrieved and displayed on label3.
     
  17. You must have to specify culture settings in web.config file for global resources. Add below tag in between tag. You can set “Culture” and “uiCulture” attributes for any specific culture and language (for example “fr-FR” for French) but I have set it “Auto” so that ASP.NET automatically detects the culture from browser.

    <system.web>
       
            <globalization culture="Auto" uiCulture="Auto"/>
        </system.web>
     
  18. Right click on web site and Browse with Internet Explorer
  19. Change browser settings
     
    You must have to change your browser settings to view result of other resource file for French.
     
    For Internet Explorer
     
    From Menu > Tools > Internet Options
    Click Language > Click Add
    Select French (France) [fr-FR]
    Move up to the first and Click OK
     
  20. Now Right click again on web site and Browse with Internet Explorer

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Localization with Global Resources in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net