Sunday, May 2, 2010

Multilingual application on asp.net

Hello,

If you are creating multilingual application then based on query string or session set value in Basepage. I have created one sample for .net guys who really want to acheive multilingual application in asp.net. One more thing date respective and currency respective it will handle automatically. But one thing you have to take care while display currency it will display in with "comma" but you have to take care when you enter into database then 1,00 will not accept by database due to may be you sql is en-Us format then in this case you have to explicitly transalte curency into 1.00. So it will work proper but if you will not take care then this amount will throw exception or will become 1000.00 in database. Just take care such thing rest of thing is fine.


protected override void InitializeCulture()
{

// Based on Query string we set culture, else we set in web.congig as default country and language code

// Based on our requirement we have set greek
// Based on our requirement we have set greek

if (Request.QueryString.Count > 0)
{
if (Request.QueryString["lang"] != null)
{
pLanguegeCode = Convert.ToString(Request.QueryString["lang"]);
}
else
{
if (ConfigurationManager.AppSettings["DefaultLanguageCode"] != null)
{
pLanguegeCode = Convert.ToString(ConfigurationManager.AppSettings["DefaultLanguageCode"]);
}
}
}

if (ConfigurationManager.AppSettings["DefaultCountryCode"] != null)
{
pCountryCode = Convert.ToString(ConfigurationManager.AppSettings["DefaultCountryCode"]);
}
// based on language code we are setting Culture name
if (pLanguegeCode.ToLower() == "en")
{
Session["CultureName"] = "en-GB";
}
else if (pLanguegeCode.ToLower() == "el")
{
Session["CultureName"] = "el-GR";
}
else
{
Session["CultureName"] = "en-US";
}
if (Session["CultureName"] != null)
{
string cultureName = Session["CultureName"].ToString();
//Change language setting to user-chosen one
if (cultureName != null)
{
//Set Culture
CultureInfo objCultureInfo = new CultureInfo(cultureName);
Thread.CurrentThread.CurrentUICulture = objCultureInfo;

//Set Date Format as per your need
DateTimeFormatInfo objDateTimeFormatInfo = objCultureInfo.DateTimeFormat;
objDateTimeFormatInfo.ShortDatePattern = SetDateFormat();

//In web.config we set default date seprator, you can use . or \
//
objDateTimeFormatInfo.DateSeparator = ConfigurationManager.AppSettings["DateSeparator"].ToString();

objCultureInfo.DateTimeFormat = objDateTimeFormatInfo;

// Here specific culture will set
Thread.CurrentThread.CurrentCulture = objCultureInfo;
if (objDateTimeFormatInfo != null)
objDateTimeFormatInfo = null;
if (objCultureInfo != null) objCultureInfo = null;
}
}

base.InitializeCulture();
}

public string SetDateFormat()
{
string strFormat = "";
//
if (ConfigurationManager.AppSettings["DateFormat"] == null)
{
if (ConfigurationManager.AppSettings["DateSeparator"] == null)
{
strFormat = "MM/dd/yyyy";
}
else
{
//In web.config we set default date seprator, you can use . or \
//
strFormat = "MM" + Convert.ToString(ConfigurationManager.AppSettings["DateSeparator"]) + "dd" + Convert.ToString(ConfigurationManager.AppSettings["DateSeparator"]) + "yyyy";
}
}
else
{
//Based on culture it will set
strFormat = ConfigurationManager.AppSettings["DateFormat"].ToString(System.Globalization.CultureInfo.CurrentCulture);
}

return strFormat;
}

On my first sentence I had mentioned that set value in basepage, I want to say you please create one class give name as "BasePage" inherit from System.Web.UI.Page
so all page you may inherit from BasePage instead of System.Web.UI.Page, so automatically culture respective property will set in basepage, so no duplication of code.

one basically I have created on common function to format a date because. In my requirement "/" will be seprator and date format will be in two way if it is en-US
then MM/dd/yyyy and if it is en-GB/it-IT/de-de then format will be dd/MM/yyyy. If you will not set culture in web.config then it will take defaul i.e en-US due to only it is working for spanish ( I assume spanish format is MM/dd/yyyy and it
working not for default spanish it is working because default is en-US. I have created function because if any client will demand in format dd.MM.yyy or MM.dd.yyyy then for me it will be only configuration change instead of coding.