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 in ASP.NET

Author: Mike Green

In today’s global environment, you need to create websites or applications that support different languages and country specific cultures. ASP.NET makes it easy for you.

 

What is localization?

Localization is the process of designing your website or application so that it provides support to the users to use this website or application in their own language and culture. So Localization is to customize your website or application for a specified culture and language. As users of your website or application are from different countries and cultures and they understand different languages, it is better to create Multilanguage website or application so that users can easily understand and use it.
 
Localization helps developers to create website or application that can be used by different users from different cultures. Users from a set of targeted countries can localize website or application according to their own language.
 
Localization is not only the way of translating your website; also it makes your website available for users that can use them in their language. So the process of translation is called Localization. Main focus of the localization is to provide the interface which is suitable for users according to their language and culture.
 
Localization in ASP.NET
 
If you want to develop a website or application with Localization support, ASP.NET provides an easy way to do this. The support for localization in ASP.NET is impressive. When you localize your website or application in ASP.NET, it automatically detects the user’s language and culture from browser and, if available, provides that language and culture to the user. It provides date format, currency, numeric values and accurately configured controls according to the mentioned language and culture.
 
What is Resource file?
 
In ASP.NET, A resource file is used to store user interface strings or other resources that are related to specific culture and language. A separate resource file is created for every specific language and culture.  A resource file has the .resx extension and it contains each resource in a key-value pair.
 
Local and Global Resources
 
There are two types of resources in ASP.NET
 
Local Resources (Implicit Localization)
Global Resources (Explicit Localization)
 
The resources that are specific to a single web page and provide the language and culture for that web page only, are local resources. These resources are stored in .resx file of that page under App_LocalResources folder. For example if a web page is with the name of Default.aspx then the local resource file name will be Default.aspx.resx. App_LocalResources folder must be at the root of the website. When Local resource files for specific pages are created, it is also called implicit localization.
 
On the other side, if you want resources with global scope for the entire application then resource file can be created under App_GlobalResources with .resx extension. This resource file will be available for whole application and resources in this file can be read by any page or code in the application. App_GlobalResources  folder must be at the root of the application. This process of creating global resources is called explicit Localization.
 
Culture and Language string format
 
Culture and Language is specified with a string format. The first part of the string contains lowercase two letters which denotes the language and the second part contains uppercase two letters which denotes the country or region. First and second parts are also called culture and subculture respectively. Both parts are separated with a dash (-). For example the string format for English in USA is en-US and French in France is fr-FR. ASP.NET uses this format in the name of resource file before .resx extension to specify a culture and language.
 

A simple example to create and use of Local Resource file for Localization 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. Click on Design for designer view of Default.aspx page
  6. Drag and drop a label and a button and change the text attribute as “My Label” and “My Button” respectively. Or write code below in source view.

    <asp:Label ID="Label1" runat="server" Text="My Label"></asp:Label>
       
    <br />
       
    <asp:Button ID="Button1" runat="server" Text="My Button"/>

     
  7. From Menu, go to Tools and click Generate Local Resources
  8. A folder App_LocalResources at the root of the website is created and a file Default.aspx.resx is created in the App_LocalResources folder.
  9. You can see in source file, the HTML code changed as under

       
    <asp:Label ID="Label1" runat="server" Text="My Label"
       
                meta:resourcekey="Label1Resource1"></asp:Label>
       
     <br />
       
     <asp:Button ID="Button1" runat="server" Text="My Button"
       
                meta:resourcekey="Button1Resource1" />

     
  10. You can also see that at the top in the page directive following attributes are added

    culture="auto" meta:resourcekey="PageResource1" uiculture="auto"
     
  11. Open Default.aspx.resx file. You will see two columns with Name and Value headings.
  12. Change the value in value column as “English Button” against the Name Button1Resource1.Text and Change value in the Value Column as “English Label” against the Name Label1Resource1.Text. Save the file and close
  13. Now right click on the Default.aspx.resx file and click Copy
  14. Right click on the App_LocalResources folder and click paste
  15. A new file with the name “Copy of Default.aspx.resx” is in the App_LocalResources folder now
  16. Right click on this file and click Rename
  17. Change the Name of the file as “Default.aspx.fr-FR.resx” and open file. This resource file will be used for French language.
  18. Change Value in value column as “French Button” against the Name Button1Resource1.Text and Change Value in value column as “French Label” against Name Label1Resource1.Text. Save the file and close.
  19. Right click on web site and Browse with Internet Explorer. You will see Label and Button has the text “English Label” and “English Button” respectively. This is the text you provided in Default.aspx.resx resource file.
  20. 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
     
  21. Now Right click again on web site and Browse with Internet Explorer. You will see this time Label and Button has the text “French Label” and “French Button” respectively. This is the text you provided in Default.aspx.fr-FR.resx resource file.
 

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Nested Master Pages in ASP.NET 4.0

Disclaimer - Privacy
© 2002-2017 DevASP.net