Click here to Skip to main content
Click here to Skip to main content

Create Resource Files for ASP.NET Web Sites

, 13 May 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
Introduction to multi language applications based on the .NET Framework

Screenshots

1.JPG

2.JPG

3.JPG

4.JPG

Introduction

In an ASP.NET Web page, you can set to two culture values, the Culture and UICulture properties. The Culture value determines the results of culture-dependent functions, such as the date, number, and currency formatting, and so on. The UICulture value determines which resources are loaded for the page.

Background

This is a very simple article for those who know object oriented programming.

Using the Code 

The code is arranged in:

  • App_Code mybasePage class
  • App_LocalResources
  • aspx pages

App_Code mybasePage Class 

Class mybasepage is derived from System.Web.UI.Page. Public property CultureName sets cultureName. Set the CurrentUICulture and CurrentCulture properties of the current thread to the UI culture and culture, respectively. The CurrentUICulture property takes a language and culture information string. To set the CurrentCulture property, you create an instance of the CultureInfo class and call its CreateSpecificCulture method.

public class mybasePage : System.Web.UI.Page 
{
    public mybasePage()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    static string cultureName;

    public static string CultureName
    {
        get { return cultureName; }
        set { cultureName = value; }
    }

    protected override void InitializeCulture()
    {   
            Thread.CurrentThread.CurrentCulture =
                CultureInfo.CreateSpecificCulture(cultureName);
            Thread.CurrentThread.CurrentUICulture = new
                CultureInfo(cultureName);
       
        base.InitializeCulture();
    }
}        
  <table>
        <tr>
            <td >
                <asp:Label ID="Label1" runat="server" Text="Select Language"></asp:Label>
            </td>
            <td>:</td>
            <td>
            <asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem Text="English" Value="en-US"></asp:ListItem>
        <asp:ListItem Text="Marathi" Value="mr-IN"></asp:ListItem>
        <asp:ListItem Text="Gujarathi" Value="gu-IN"></asp:ListItem>
        </asp:DropDownList>    
            </td>
        </tr>
        <tr>           
            <td colspan="3">
                </td>
        </tr>        
        <tr>           
            <td colspan="3" align="center">
            <asp:Button ID="Button1" runat="server" 
			Text="set Language" onclick="Button1_Click" 
                     style="height: 26px" />
                    </td>
        </tr>        
        <tr>            
            <td></td>
        </tr>        
        <tr>            
            <td>
             <asp:LinkButton ID="LinkButton1" runat="server" 
		PostBackUrl="~/Sample.aspx">Go to Sample Page</asp:LinkButton>
            </td>
        </tr>        
    </table>
 protected void Button1_Click(object sender, EventArgs e)
    {       
        mybasePage.CultureName = DropDownList1.SelectedItem.Value.ToString();
    }

Go to the sample page. There is a link button to navigate to another page. 

The sample page shows Language setting words.

App_LocalResources 

Creating Resources from a Web Page  

A resource file is an XML file that can contain strings and other resources, such as image file paths. Resource files are typically used to store user interface strings that must be translated into other languages. This is because you can create a separate resource file for each language into which you want to translate a Web page. 

Global resource files are available to any page or component in your Web site. Local resource files are associated with a single Web page, user control, or master page, and contain the resources for only that page.

To generate a local resource file from an ASP.NET Web page: 

  1. Open the page for which you want to create a resource file.
  2. Switch to Design View.
  3. In the Tools menu, click Generate Local Resource. Visual Web Developer creates the App_LocalResources folder if it does not already exist. Visual Web Developer then creates the culturally neutral base resource file for the current page, which includes a key/name pair for each control property or page property that requires localization. Finally, Visual Web Developer adds a meta attribute to each ASP.NET Web server control to configure the control to use implicit localization for more information about implicit and explicit localization.
  4. Type values for each resource that you need in your application, and then save the file.
  5. If the latest resource changes are not displayed, refresh Design view by switching to Source view and then switching back to Design view.
  6. To create resource files for additional languages, copy the file in Solution Explorer or in Windows Explorer, and then rename like:
    • Sample.aspx.gu-IN.resx 
    • Sample.aspx.mr-IN.resx

    Sample.aspx page contains a label. This label text changes if you make a change in the default.aspx page.

Points of Interest  

  • To design the same web site for national/international language.

History

  • 12th May, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

girishdpatil

India India
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 Pinmembermaxcom11-Dec-12 16:37 
QuestionResource file PinmemberShivarajbk7-Dec-12 0:04 
is there any ristrictions in giving file name?
GeneralCreating Plain text resource file entry PinmembermeetBinu20-Dec-09 18:18 
GeneralMy vote of 2 PinmemberSamer Abu Rabie13-May-09 20:37 
QuestionWhat about Culture & UICulture Pinmemberkanu@adatapost13-May-09 17:27 
AnswerRe: What about Culture & UICulture Pinmembergirishdpatil13-May-09 17:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 13 May 2009
Article Copyright 2009 by girishdpatil
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid