Internationalization of ASP.net application

The concept:
The process of making application ready for multi-lingual support is called Internationalization of the application. It includes following three steps:
  1. Globalization is the process of designing and developing applications that function for multiple cultures. Thus globalization is a concept which underlines the fact that the application needs to be neutral culture and nothing related to culture is hard coded. The primary task in this phase is to identify the various locale-sensitive resources and to isolate these resources from the executable code. Following are key points to make your application globalized:
    1. Do not hard code cultures like date, currency etc.
    2. Do not hard code texts, put it in resource files.
  2. Localizability: An application that has been globalized must be tested to ensure that its executable code is independent of the culture and language-specific data. This is called localizability testing. The focus of localizability testing is to check how the application adapts itself to different locales
  3. Localization is the process of customizing your application for a given culture and locale. Localization consists primarily of translating the user interface.
Let’s talk now of Implementation:
ASP.NET enables you to create a page that can obtain content and other data based either
  1. On the preferred language setting for the browser or
  2. Based on the user's explicit choice of language.
Content and other data is referred to as resources and such data can be stored in resource files or other sources. In the ASP.NET Web page, you configure controls to get their property values from resources. At run time, the resource expressions are replaced by resources from the appropriate resource file.
Resource files:
Resource files is an XML that contains the string that has to be translated in different languages or paths to images. The resource file contains key/value pairs. Each pair is an individual resource.
Separate resource file has be created either
  1. For each language (hindi & english)
  2. For each language and culture (UK English, US English)
Resource files in ASP.NET have an .resx extension. At run time, the .resx file is compiled into an assembly, which is sometimes referred to as a satellite assembly. Because the .resx files are compiled dynamically, like ASP.NET Web pages, you do not have to create the resource assemblies.
When you create resource files, you start by creating a base .resx file. For each language that you want to support, create a new file that has the same file name. But in the name, include the language or the language and culture (culture name). For example:
  1. WebResources.resx - The base resource file. This is the default (fallback) resource file.
  2. WebResources.en.resx - A resource file for English. This is also called neutral culture file.
  3. WebResources.en-GB.resx - A resource file for english (UK) specifically.
Please note Culture is used to format localize info for non-UI things such as Date, Numbers, Currency, while UICulture is for specifying the locallized UI info (which UI resource set to use), it controls resource loading, translated text and localized control properties like color or font.
Generating resource files:
Resource files in ASP.net are created on the basis of scope required.
  1. Global resource file - To create global resource file, we have to put resource file in the App_GlobalResources folder at the root of the application. One can read these file from any page or code of the application.
  2. Local resource file - To create local resource file, we may either put files in the reserved folder App_LocalResources or any other folder in the application. You associate a set of resources files with a specific Web page by using the name of the resource file. For example, if there is a file name called index.aspx, :
  1. Index.resx - The base resource file. This is the default (fallback) resource file.
  2. Index.en.resx - A resource file for English. This is also called neutral culture file.
  3. Index.en-GB.resx - A resource file for english (UK) specifically.
You can use any combination of global and local resource files in the Web application. Generally, you add resources to a global resource file when you want to share the resources between pages. However, global resource files can become large, if you store all localized resources in them. Global resource files can also be more difficult to manage, if more than one developer is working on different pages but in a single resource file, in that case local resource is preferable.
Using resource files in Pages:
To use the resource files in the web pages, there are two ways:
  1. Implicit localization - it works with local resources and lets you automatically set control properties to matching resources.
  2. Explicit localization - it lets you use a resource expression to set a control property to a specific resource in a local or global resource file.
References:
http://msdn.microsoft.com/en-us/library/c6zyy3s9.aspx
http://www.beansoftware.com/ASP.NET-Tutorials/Globalization-Localization.aspx
http://www.codeproject.com/Kb/aspnet/localizationByVivekTakur.aspx
http://www.hanselman.com/blog/GlobalizationInternationalizationAndLocalizationInASPNETMVC3JavaScriptAndJQueryPart1.aspx

Unique Visitor Counter In Asp.Net

Introduction:

It is sometimes useful to display the number of visits a page on a Web site has experienced using a counter that's displayed as part of the page's content. While adding a counter to a page written in ASP or ASP.NET is relatively straight-forward, adding a counter to a plain HTML page (no scripting of any kind) is a little different.

This article describes a counter I created (see the following figure) using ASP.NET. . The code also maintains its own little "database" so that you don't have to FTP to your Web server to create a new counter. I use and designed this counter for my site listings and various other pages; so it is use a XMLfile to store the noumber of counts.

The Counter Lok like this image                      Visitor Count

000024


Creating The Counter:

Firstly you have to create a project in VS Studio And also you have to add a XML file and name it HitCounter to store the Hit counts, in the Global.asax file Write Down the following code:

Global.asax
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace HitCounter
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
           //  Code that runs on application start
        }

        protected void Application_End(object sender, EventArgs e)
        {
            //  Code that runs on application shutdown

        }

        void Session_Start(object sender, EventArgs e)
        {
            //Get & increment visitor count
            UserVisitCount();

            DataSet tmpDs = new DataSet();
            try
            {
                tmpDs.ReadXml(Server.MapPath("~/App_Data/HitCounter.xml"));
            }
            catch (System.IO.FileNotFoundException)
            {
                throw;
            }
            catch
            {
                //no need to throw exception for this
            }

            //set in Session
            Session["hits"] = tmpDs.Tables[0].Rows[0]["hits"].ToString();
        }

        /// <summary>
        /// Increments visit count on every user session
        /// </summary>
        private void UserVisitCount()
        {
            try
            {
                DataSet tmpDs = new DataSet();
                //read hit count
                tmpDs.ReadXml(Server.MapPath("~/App_Data/HitCounter.xml"));
                int hits = Int32.Parse(tmpDs.Tables[0].Rows[0]["hits"].ToString());

                hits += 1;

                //write hit count
                tmpDs.Tables[0].Rows[0]["hits"] = hits.ToString();
                tmpDs.WriteXml(Server.MapPath("~/App_Data/HitCounter.xml"));
            }
            catch (System.IO.FileNotFoundException)
            {
                throw;
            }
            catch
            {
                //no need to throw for this
            }
        }
    }
}

After Writing these code you have to take now a user control to to render the Hit Counter.And give the name HitCounterControl.ascx. in the code behind file write the following code: To generate the table for the counter to show on the web form.

UserControl:


HitCounterControl.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="HitCounterControl.ascx.cs"
    Inherits="RebelsWeb.SiteControls.HitCounterControl" %>
<div style="padding-top: 15px; text-align: center">
    <span style="font-size:small">Visitor Count </span>
    <asp:Literal ID="ltlCounter" runat="server"></asp:Literal>
</div>

HitCounterControl.ascx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace RebelsWeb.SiteControls
{
    public partial class HitCounterControl : System.Web.UI.UserControl
    {
        int totalDigits = 6;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ltlCounter.Text = FormatHTMLCounter(Session["hits"].ToString());
            }
        }

       /// <summary>
       /// Calculates and formats the vistor count number
       /// </summary>
       /// <param name="visitCount"></param>
       /// <returns></returns>
        private string FormatHTMLCounter(string visitCount)
        {
            int noOfDigits = visitCount.Length;
            int zeroesToPrefix = totalDigits - noOfDigits;
            StringBuilder strCounterHtml = new StringBuilder();
          
            strCounterHtml.Append(@"<table align=""center"" style=""width: 120px; background-color: Black; color: White; text-align: center; font-weight: bold;""><tr>");

            for (int i = 0; i < zeroesToPrefix; i++)
            {
                strCounterHtml.Append(@"<td style=""border:1px solid white; padding:2px"">");
                strCounterHtml.Append("0");
                strCounterHtml.Append("</td>");
            }

            char[] visitCountChar = visitCount.ToCharArray();
            for (int i = 0; i < visitCountChar.Length; i++)
            {
                strCounterHtml.Append(@"<td style=""border:1px solid white; padding:2px"">");
                strCounterHtml.Append(visitCountChar[i]);
                strCounterHtml.Append("</td>");
            }

            strCounterHtml.Append("</tr></table>");
            return strCounterHtml.ToString();
        }
    }
}
Now, After writing these code you can put this user control on web form to display the counter.


Summary

This article described how to create a fast page hit counter that's easy to use with plain HTML. The code uses ASP.NET and the C#.NET programming language to, automatically creating new counters, and preventing users from artificially inflating the value of the counter.