Introduction
This article is for those who are working with different languages in web application or in database level. Here, you can find culture specific formats for DateTime
, Number
, Currency
, Percent
, etc. for different languages. This article is based on .NET Framework 4.5 and SQL Server 2012.
I have put my time and efforts on all of my articles, Please don't forget to mark your votes, suggestions and feedback to improve the quality of this and upcoming articles. Thanks for reading.
Background
I have recently published an article titled ".NET Framework Cultures with Culture Specific Formats and Mapping with SQL Server Language." After going through that article, one of my friends suggested a simple utility that will give the required formats by specific culture. This is how I implemented his suggestion.
Using the Code
I have created a simple form with some label controls and textbox controls to display format specific data, I have not named most of the controls. :) (I have not used all best practices in this utility code.)
private void Form1_Load(object sender, EventArgs e)
{
LoadCultureList();
loadDefaultData();
}
The Form1_Load
calls two methods to load culture list and default values that are used in this utility.
The LoadCultureList()
method definition is given as below:
private void LoadCultureList()
{
CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
var qry = from c in cultures
orderby c.DisplayName ascending
select new
{
CultureCode = c.Name,
DisplayName = c.DisplayName
};
comboBox1.DisplayMember = "DisplayName";
comboBox1.ValueMember = "CultureCode";
var datasource = qry.ToList();
datasource.Insert(0, new { CultureCode = "", DisplayName = "--Select--" });
comboBox1.DataSource = datasource;
}
The GetCultures
assigns all available cultures to 'cultures
' variable and using linq, I have sorted it by displayname
. Then, I have used the sorted list to bind the culture list to combobox
.
private void button1_Click(object sender, EventArgs e)
{
loadFormats();
}
loadFormats();
method is used to load the culture specific data in appropriate controls. This method will be called onClick
of load button or refresh button. The method definition is given as below:
private void loadFormats()
{
try
{
DateTime date = DateTime.Now;
Int64 currency = Convert.ToInt64(textBox56.Text);
Int64 number = Convert.ToInt64(textBox55.Text);
Int64 percent = Convert.ToInt64(textBox54.Text);
string cultureCode = "en-US";
if (textBox1.Text != "")
{
cultureCode = textBox1.Text;
}
else
{
cultureCode = comboBox1.SelectedValue.ToString();
}
CultureInfo culture = new CultureInfo(cultureCode);
System.Threading.Thread.CurrentThread.CurrentCulture = culture;
textBox2.Text = culture.DateTimeFormat.Calendar.ToString().Replace
("System.Globalization.", "");
textBox3.Text = culture.DateTimeFormat.NativeCalendarName;
textBox4.Text = culture.DateTimeFormat.YearMonthPattern;
label64.Text = date.ToString(culture.DateTimeFormat.YearMonthPattern);
textBox5.Text = culture.DateTimeFormat.MonthDayPattern;
label65.Text = date.ToString(culture.DateTimeFormat.MonthDayPattern);
textBox13.Text = culture.DateTimeFormat.CalendarWeekRule.ToString();
textBox14.Text = culture.DateTimeFormat.FirstDayOfWeek.ToString();
textBox15.Text = culture.DateTimeFormat.DateSeparator;
label68.Text = date.ToShortDateString();
textBox16.Text = culture.DateTimeFormat.TimeSeparator;
label69.Text = date.ToShortTimeString();
textBox17.Text = culture.DateTimeFormat.AMDesignator;
textBox18.Text = culture.DateTimeFormat.PMDesignator;
textBox19.Text = culture.DateTimeFormat.ShortDatePattern;
label72.Text = date.ToShortDateString();
textBox20.Text = culture.DateTimeFormat.LongDatePattern;
label73.Text = date.ToLongDateString();
textBox21.Text = culture.DateTimeFormat.ShortTimePattern;
label74.Text = date.ToShortTimeString();
textBox22.Text = culture.DateTimeFormat.LongTimePattern;
label75.Text = date.ToLongTimeString();
textBox23.Text = culture.DateTimeFormat.FullDateTimePattern;
label76.Text = date.ToString(culture.DateTimeFormat.FullDateTimePattern);
textBox24.Text = culture.DateTimeFormat.SortableDateTimePattern;
label77.Text = date.ToString(culture.DateTimeFormat.SortableDateTimePattern);
textBox25.Text = culture.DateTimeFormat.UniversalSortableDateTimePattern;
label78.Text = date.ToString(culture.DateTimeFormat.UniversalSortableDateTimePattern);
textBox6.Text = string.Join(", ", culture.DateTimeFormat.MonthNames);
textBox7.Text = string.Join(", ", culture.DateTimeFormat.MonthGenitiveNames);
textBox8.Text = string.Join(", ", culture.DateTimeFormat.AbbreviatedMonthNames);
textBox9.Text =
string.Join(", ", culture.DateTimeFormat.AbbreviatedMonthGenitiveNames);
textBox10.Text = string.Join(", ", culture.DateTimeFormat.DayNames);
textBox11.Text = string.Join(", ", culture.DateTimeFormat.ShortestDayNames);
textBox12.Text = string.Join(", ", culture.DateTimeFormat.AbbreviatedDayNames);
textBox33.Text = culture.NumberFormat.CurrencySymbol.ToString();
label81.Text = currency.ToString("C");
textBox32.Text = culture.NumberFormat.CurrencyDecimalDigits.ToString();
label82.Text = currency.ToString("C");
textBox31.Text = culture.NumberFormat.CurrencyDecimalSeparator.ToString();
label83.Text = currency.ToString("C");
textBox30.Text = culture.NumberFormat.CurrencyGroupSeparator.ToString();
label84.Text = currency.ToString("C");
textBox29.Text = string.Join(", ", culture.NumberFormat.CurrencyGroupSizes);
label85.Text = currency.ToString("C");
textBox28.Text = culture.NumberFormat.CurrencyNegativePattern.ToString();
label86.Text = (-1234).ToString("C");
textBox27.Text = culture.NumberFormat.CurrencyPositivePattern.ToString();
label87.Text = (+1234).ToString("C");
textBox39.Text = culture.NumberFormat.NumberDecimalDigits.ToString();
label90.Text = number.ToString("N");
textBox38.Text = culture.NumberFormat.NumberDecimalSeparator.ToString();
label91.Text = number.ToString("N");
textBox37.Text = culture.NumberFormat.NumberGroupSeparator.ToString();
label92.Text = number.ToString("N");
textBox36.Text = string.Join(", ", culture.NumberFormat.NumberGroupSizes);
label93.Text = number.ToString("N");
textBox35.Text = culture.NumberFormat.NumberNegativePattern.ToString();
label94.Text = number.ToString("N");
textBox34.Text = culture.NumberFormat.PositiveSign.ToString();
label95.Text = number.ToString("N");
textBox46.Text = culture.NumberFormat.PercentSymbol.ToString();
label98.Text = percent.ToString("P");
textBox45.Text = culture.NumberFormat.PerMilleSymbol.ToString();
label99.Text = percent.ToString("P");
textBox44.Text = culture.NumberFormat.PercentDecimalDigits.ToString();
label100.Text = percent.ToString("P");
textBox43.Text = culture.NumberFormat.PercentDecimalSeparator.ToString();
label101.Text = percent.ToString("P");
textBox42.Text = culture.NumberFormat.PercentGroupSeparator.ToString();
label102.Text = percent.ToString("P");
textBox41.Text = string.Join(", ", culture.NumberFormat.PercentGroupSizes);
label103.Text = percent.ToString("P");
textBox40.Text = culture.NumberFormat.PercentNegativePattern.ToString();
label104.Text = (-1234).ToString("P");
textBox26.Text = culture.NumberFormat.PercentPositivePattern.ToString();
label105.Text = (+1234).ToString("P");
textBox48.Text = culture.NumberFormat.NegativeInfinitySymbol.ToString();
textBox47.Text = culture.NumberFormat.PositiveInfinitySymbol.ToString();
sqlculturemapping obj = new sqlculturemapping().getSqlMappings().Where
(c => c.LCID == culture.LCID.ToString()).FirstOrDefault();
if (obj == null)
{
obj = new sqlculturemapping();
}
textBox52.Text =Convert.ToString( obj.FullName);
textBox51.Text = Convert.ToString(obj.Alias);
textBox50.Text = culture.LCID.ToString();
textBox49.Text = Convert.ToString(obj.specificulture);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Let's go through the above method step by step. The below given block is used to load all the default values to appropriate local variables.
DateTime date = DateTime.Now;
Int64 currency = Convert.ToInt64(textBox56.Text);
Int64 number = Convert.ToInt64(textBox55.Text);
Int64 percent = Convert.ToInt64(textBox54.Text);
string cultureCode = "en-US";
After that, I have changed the current culture
to the selected culture
. The culture can be entered in the textbox
or selected in the combobox
.
CultureInfo culture = new CultureInfo(cultureCode);
System.Threading.Thread.CurrentThread.CurrentCulture = culture;
Once I changed the current culture
, all formats and samples will be displayed as per the changed culture
.
textBox21.Text = culture.DateTimeFormat.ShortTimePattern;
label74.Text = date.ToShortTimeString();
To get the SQL Server mapping, I used the below code. I have already created a class for SQLCultureMapping:
sqlculturemapping obj = new sqlculturemapping().getSqlMappings().Where
(c => c.LCID == culture.LCID.ToString()).FirstOrDefault();
Click here to read more on how sqlculturemapping is used in this utility.
Summary
In this article, I have created a utility for displaying ".NET Framework Cultures with Culture Specific Formats and Mapping with SQL Server Language." If I have missed anything or need any correction, then please let me know. I hope you have enjoyed this article and got some value addition to your knowledge.
You might be interested in the below articles:
I have put my time and effort in all of my articles. Please don't forget to mark your votes, suggestions and feedback to improve the quality of this and upcoming articles. Thanks for reading.
History
- 19th May, 2013: Initial version