Creating multilingual websites
Code to set the culture and get the currency symbol for that culture.
- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.Threading;
- using System.Globalization;
- /// <summary>
- /// Set the culture and get the currency symbol
- /// </summary>
- public sealed class MyCulture
- {
- public MyCulture()
- {
- //
- // TODO: Add constructor logic here
- //
- }
- public void SetCulture(string CultureCode)
- {
- Thread.CurrentThread.CurrentCulture =
- CultureInfo.CreateSpecificCulture(CultureCode);
- Thread.CurrentThread.CurrentUICulture =
- new CultureInfo(CultureCode);
- }
- public string GetCurrencySymbol(string CultureCode)
- {
- SetCulture(CultureCode);
- CultureInfo UsersCulture = Thread.CurrentThread.CurrentCulture;
- RegionInfo myRegion = new RegionInfo(UsersCulture.LCID);
- return myRegion.CurrencySymbol;
- }
- }
- MyCulture obj = new MyCulture();
- String symbol = obj.GetCurrencySymbol("ta-IN");
When we are passing the input culture is necessary.
i.e. we have to pass "ta-IN" or "ta" but not "IN".
List of CultureCodes
How to add currency symbol in the gridview?
We can do it both from the server side or from the client side.
- //Setting currency symbol from the service side
- protected void grdWithCurrencySign_RowDataBound(object sender, GridViewRowEventArgs e)
- {
- if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
- {
- MyCulture obj = new MyCulture();
- string symbol = obj.GetCurrencySymbol("ta-in");
- e.Row.Cells[1].Text = symbol + " " + e.Row.Cells[1].Text;
- }
- }
- public string AddCurrency(string amount)
- {
- MyCulture obj = new MyCulture();
- return amount + " " + obj.GetCurrencySymbol("ta-in");
- }
- <asp:gridview id="grdWithCurrencySign" runat="server" autogeneratecolumns="false" onrowdatabound="grdWithCurrencySign_RowDataBound">
- <columns>
- <asp:boundfield datafield="EmpID" headertext="EmpID">
- <asp:boundfield datafield="Salary" headertext="Salary">
- <asp:templatefield headertext="Salary">
- <itemtemplate>
- <%# AddCurrency(Convert.ToString(Eval("Salary")))%>
- </itemtemplate>
- </asp:templatefield>
- </asp:boundfield></asp:boundfield></columns>
- </asp:gridview>
No comments:
Post a Comment