Monday, 14 November 2011

Language Translation using c#

global.aspx
Design::

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="text-align: center">
        <table>
            <tr>
                <td colspan="2" align="center">
                    <h3 style="color: Red">
                        Welcome To Globalization & Localization.</h3>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label1" runat="server"
                        Text="<%$ Resources:Global.aspx, Select Language %>"></asp:Label>
                <td>
                    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
                        Height="25px" Width="155px"
                        onselectedindexchanged="DropDownList1_SelectedIndexChanged">
                        <asp:ListItem Value="en-US">English</asp:ListItem>
                        <asp:ListItem Value="es-Mx">Spanish</asp:ListItem>
                        <asp:ListItem Value="de-DE">Germany</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label2" runat="server"
                        Text="<%$ Resources:global.aspx, Currency %>"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label3" runat="server"
                        Text="<%$ Resources:global.aspx, Language Name %>"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label4" runat="server"
                        Text="<%$ Resources:global.aspx, Long Date %>"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label5" runat="server"
                        Text="<%$ Resources:global.aspx, Amount %>"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <asp:Button ID="Button1" runat="server"
                        Text="<%$ Resources:global.aspx, Button %>" onclick="Button1_Click1"
                        Width="70px" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

Code::

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["culture"] == "es-Mx")
            {
                DropDownList1.SelectedIndex = 1;
            }
            else if (Session["culture"] == "en-US")
            {
                DropDownList1.SelectedIndex = 0;
            }
            else if (Session["culture"] == "de-DE")
            {
                DropDownList1.SelectedIndex = 2;
            }
        }
    }
    protected override void InitializeCulture()
    {
        base.InitializeCulture();
        if (Session["culture"] != null)
        {
            string cult = Session["culture"].ToString();
            if (cult != null)
            {
                Culture = cult;
                UICulture = cult;
            }
        }
        else
        {
            string cult = Request["DropDownList1"];
            if (cult != null)
            {
                Culture = cult;
                UICulture = cult;
            }
        }      
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["culture"] = DropDownList1.SelectedValue;
        Response.Redirect("global.aspx");
    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        Session["culture"] = DropDownList1.SelectedValue;
        Response.Redirect("local.aspx");
    }
===================================================================
global.aspx.resx
 Name        value
AmountAmount
CurrencyCurrency
Language NameLanguage Name
Long DateLong Date
welcomewelcome
Select LanguageSelect Language
ButtonButton

global.aspx.de-DE.resx
  Name
Select Language
 Value
Wählen Sie Sprache
Long DateLong Date
Language NameSprache Name
CurrencyWährung
ButtonTaste
AmountHöhe
global.aspx.es-Mx.resx
 Name                 Value
Amountcantidad 
Currencymoneda
Language Nameidioma Nombre
Long DateFecha de largo
welcomebienvenida
Select LanguageSelecciona Idioma de la
Buttonbotón

No comments:

Post a Comment