articles

Resources in .NET

priyanka kushwaha 4646 26-Feb-2015


 In this article, I’m explaining about resources in .NET

What is Resources file?

A resources file is a XML file that contains the string that we want to:

a.       Translate into different language.

b.      Can be updated dynamically so that user themselves can modify values in resources files one the application is deployed on the server without re-compiling the entire application itself. 

 c.       The resource file contains key/value pairs.

d.      Each pair is an individual resources.

e.      Key name are not case sensitive.

Types of resources:

1.       Local resources

2.       Global resources 


Local resources: 

  1.       Local resources:

1.       Local resources is specific to a single web page and used for providing version of a web page in different language.

2.       Local resources must be stored in App_LocalResources sub folder.

3.       Local resources must be named in format <WebPageName>[.language/language and culture etc]

4.       Example: Default.aspx.resx- Base resource file.  

2.       Global resources:

1.       Global resource can be read from any  page or code that is  in the application.

2.       Global resource must be stored in App_GlobalResources at the root of the application.

Example:

1.       Create a Default.aspx file.

<%@ PageLanguage="C#"AutoEventWireup="true"CodeBehind="DetailForm.aspx.cs"Inherits="ResourceSam.DetailForm"  Culture="auto"UICulture="auto"%>

 

<!DOCTYPEhtml>

 

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headrunat="server">

    <linkhref="css/bootstrap.css"rel="stylesheet"/>

    <title></title>

</head>

<body>

    <formid="form1"runat="server">

       

        <table>

            <tr><td>Select language</td><td><asp:DropDownListID="DropDownList1"runat="server"AutoPostBack="True">

            <asp:ListItemValue="en-us">English</asp:ListItem>

            <asp:ListItemValue="hi-in">हिंदी</asp:ListItem>

  <asp:ListItemValue="Ur">اردو</asp:ListItem>

        </asp:DropDownList><br/></td></tr>

            <tr>

                <td><asp:LabelID="lblinformation"runat="server"Text="Personal informatiom"meta:resourceKey="lblinformation"></asp:Label></td>

                <td>

            

             </td>

            </tr>

            <tr>

                <td><asp:LabelID="lblFirstName"runat="server"Text="First Name"  meta:resourceKey="lblFirstName"></asp:Label></td>

                <td>

                    <asp:TextBoxID="txtFirstName"runat="server"></asp:TextBox></td>

            </tr>

            <tr>

                <td><asp:LabelID="lblLastName"runat="server"Text="Last Name"meta:resourceKey="lblLastName"></asp:Label></td>

                <td>

                    <asp:TextBoxID="txtLastName"runat="server"></asp:TextBox></td>

            </tr>

            <tr>

                <td><asp:LabelID="lblAge"runat="server"Text="Age"meta:resourceKey="lblAge"></asp:Label></td>

                <td>

                    <asp:TextBoxID="txtAge"runat="server"></asp:TextBox></td>

            </tr>

            <tr>

                <td colspan="2">

                    <asp:Button ID="btnReg" runat="server" Text="Register" meta:resourceKey="btnReg" /></td>

            </tr>

        </table>

    </form>

</body>

</html>

 

 

2.      Write in Default.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Reflection;

using System.Resources;

using System.Globalization;

using System.Threading;

namespace ResourceSam

{

    publicpartialclassDetailForm : System.Web.UI.Page

    {

        protectedvoid Page_Load(object sender, EventArgs e)

        {

 

        }

        protectedoverridevoid InitializeCulture()

        {

            if (Request.Form["DropDownList1"] != null)

            {

                UICulture = Request.Form["DropDownList1"];

                Culture = Request.Form["DropDownList1"];

            }

            base.InitializeCulture();

        }

       

    }

}

 

 

3.    Right click on application>> select Add Option>>select  Add ASP.NET Folder>>select>>App_LocalResources>>Right click on App_localResouces>> Select Resource file>> 

1.       DetailForm.aspx.resx for English

2.       DetailForm.aspx.hi-in.resx for Hindi     

Resources in .NET

Resources in .NET

Output

Resources in .NET

 

 

Resources in .NET


Updated 07-Sep-2019

Leave Comment

Comments

Liked By