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
 

Localization with Global Resources in ASP.NET

Author: Mike Green

Global Resource files are created when resources are required for entire application.

 

In a previous article on DevASP.NET, I have explained about localization, local and global resource files and how to create local resource file in ASP.NET. In this article, I will explain how to create global resource file with an example.

Global Resources or Explicit Localization in ASP.NET

Global resources allow you to create resource files that are available for entire application rather than to create resource files for every page. A resource file that contains global resources exists under App_GlobalResources folder. Global resources can be read by all pages or code in the application. In explicit localization, a property value is set by an expression which is pointing to resource files. At run time, the expression is evaluated and the value for property is read from the resource file.
 
How to create and use global resource file in ASP.NET
 
  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. Right click on website > Add ASP.NET Folder  > Click on App_GlobalResources
  6. Right Click on the folder > Add New Item
  7. Select Resource File and Click Add
  8. Open Resource.resx file
  9. Write “Welcome” in first row of Name column then write “Global Resource File for English” in value column
  10. Now right click on the Resource.resx file and click Copy
  11. Right click on the App_GlobalResources folder and click Paste
  12. A new file with the name “Copy of Resource.resx” is in the App_GlobalResources folder now
  13. Right click on this file and click Rename
  14. Change the Name of the file as “Resource.fr-FR.resx” and open file. This resource file will be used for French language.
  15. Write “Welcome” in first row of Name column then write “Global Resource File for French” in value column
  16. Now open Default.aspx page and Drag and Drop a Label or write code below

    <asp:Label ID="Label1" runat="server" Text="<%$ Resources:Resource, Welcome%>"></asp:Label>


    The expression in “Text” attribute of Label is used to retrieve value from Resource files. “Resource” is the Name of the Resource file and “Welcome” is the key for which we are retrieving value for the Label Text. Note that we are not providing the full name of resource file with extension and with language and culture string in the name. At run time, ASP.NET examines the browser settings for language and, if available, retrieves value from that specific resource file and provides the value to the attribute.
     
  17. You must have to specify culture settings in web.config file for global resources. Add below <globalization> tag in between <system.web> 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. You will see Label has the text “Global Resource File for English”. This is the text you provided in Resource.resx resource file.

     
  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. You will see this time Label has the text “Global Resource File for English”. This is the text you provided in Resource.fr-FR.resx resource file.

 

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

Disclaimer - Privacy
© 2002-2017 DevASP.net