Monday, September 24, 2012

Get Currency Symbol by LanguageCode

Tutorial on using CultureInfo and RegionInfo

Creating multilingual websites

Code to set the culture and get the currency symbol for that culture.
  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Web;  
  5. using System.Web.Security;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8. using System.Web.UI.WebControls.WebParts;  
  9. using System.Web.UI.HtmlControls;  
  10.   
  11. using System.Threading;  
  12. using System.Globalization;  
  13.   
  14. /// <summary>  
  15. /// Set the culture and get the currency symbol  
  16. /// </summary>  
  17. public sealed class MyCulture  
  18. {  
  19.     public MyCulture()  
  20.     {  
  21.         //  
  22.         // TODO: Add constructor logic here  
  23.         //  
  24.     }  
  25.   
  26.     public void SetCulture(string CultureCode)  
  27.     {  
  28.         Thread.CurrentThread.CurrentCulture =   
  29.             CultureInfo.CreateSpecificCulture(CultureCode);  
  30.   
  31.         Thread.CurrentThread.CurrentUICulture =   
  32.             new CultureInfo(CultureCode);  
  33.     }  
  34.   
  35.     public string GetCurrencySymbol(string CultureCode)  
  36.     {  
  37.         SetCulture(CultureCode);  
  38.   
  39.         CultureInfo UsersCulture = Thread.CurrentThread.CurrentCulture;  
  40.         RegionInfo myRegion = new RegionInfo(UsersCulture.LCID);  
  41.   
  42.         return myRegion.CurrencySymbol;  
  43.     }  
  44. }  
How to pass the input?
  1. MyCulture obj = new MyCulture();  
  2. 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.
  1. //Setting currency symbol from the service side  
  2.         protected void grdWithCurrencySign_RowDataBound(object sender, GridViewRowEventArgs e)  
  3.         {  
  4.             if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)  
  5.             {  
  6.                 MyCulture obj = new MyCulture();  
  7.                 string symbol = obj.GetCurrencySymbol("ta-in");  
  8.   
  9.                 e.Row.Cells[1].Text = symbol + " " + e.Row.Cells[1].Text;  
  10.             }  
  11.         }  
  12.   
  13.         public string AddCurrency(string amount)  
  14.         {  
  15.             MyCulture obj = new MyCulture();  
  16.             return amount + " " + obj.GetCurrencySymbol("ta-in");  
  17.         }  
  1. <asp:gridview id="grdWithCurrencySign" runat="server" autogeneratecolumns="false" onrowdatabound="grdWithCurrencySign_RowDataBound">  
  2.                 <columns>  
  3.                     <asp:boundfield datafield="EmpID" headertext="EmpID">  
  4.                     <asp:boundfield datafield="Salary" headertext="Salary">  
  5.                     <asp:templatefield headertext="Salary">  
  6.                         <itemtemplate>  
  7.                             <%# AddCurrency(Convert.ToString(Eval("Salary")))%>  
  8.                         </itemtemplate>  
  9.                     </asp:templatefield>  
  10.                 </asp:boundfield></asp:boundfield></columns>  
  11.             </asp:gridview>  

No comments: