Introduction
Globalization means the process of preparing your code to be able to support different languages. This is done by eliminating language or culture dependencies from your code to become culture-neutral. That is to avoid using hard coded strings or messages to be displayed to the user. .NET has a bunch of classes integrated to support the development of international software. These classes are located in the namespaces System.Globalization
and System.Resources
. CultureInfo
is the class that holds information about a certain language, as formatting of numbers and dates, calendar to use, decimal character...etc.
Details
The Culture names follow the RFC 1766 standard in the format <languagecode>-<country/regioncode> where <languagecode> is a lowercase two-letter code derived form ISO639-1 and <country/regioncode> is an uppercase two-letter code derived from ISO 3166. The cultures are generally grouped into three sets:
- Invariant culture:- It is associated with the English language but not with any country/region.
- Neutral culture:- It is associated with language but not with the country/region code.
- Specific culture :- It is associated with language and country/region code.
A DateTimeFormatInfo
can be created only for the invariant culture or for specific culture, not for a neutral culture. DateTimeFormatInfo
inherits from Object
and implements ICloneable
and IFormarProvider
interfaces. For date and time, several standard patterns are defined with the default property values. We can also use the custom format using a set of format pattern characters. CultureInfo
class encapsulates information about handling according to the special requirements of a particular culture and language. DateTime
values are formatted by the ParseExact()
and ToString()
methods according to Custom or Standard patterns. The Standard format patterns and the Custom format patterns are as follows:
Format Specifier Format Pattern
d MM/dd/yyyy
D dddd*, MMMM* dd, yyyy
f dddd*, MMMM* dd, yyyy HH*:mm*
F dddd*, MMMM* dd, yyyy HH*:mm*:ss*
g MM/dd/yyyy HH*:mm*
G MM/dd/yyyy HH*:mm*:ss*
m, M MMMM* dd
t HH*:mm*
T HH*:mm*:ss*
U dddd*, MMMM* dd, yyyy HH*:mm*:ss*
y, Y YYYY MMMM*
About Source Code
In the enclosed source code, add the the Globalization
namespace.
using System.Globalization;
Now, collect all the culture with the following code of line:
CultureInfo [] ci = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
CultureInfo.GetCulture
gets the list of supported cultures filtered by the specified CultureType
and it returns an array of type CultureInfo
.
cbCultures.Items.Add(new cultureinfo(ci[i].Name));
c[i].Name
gets the Culture name in the Format �<languagecode>-<CountryCode>�. To change this to the required string, pass this to the cultureinfo
class which is derived from CultureInfo
class. cultureinfo
class contains ToString()
method to change the culture in the localized version of the .NET framework. Now collect all the date time format.
For example:
patterns = dtf.GetAllDateTimePatterns('D');
for (int i = 0;i < patterns.GetLength(0);i++)
{
updLongDatePattern.Items.Add(patterns[i]);
}
where D
is the Format specifier for the Long date pattern.
private void updLongDatePattern_SelectedItemChanged(object sender,
System.EventArgs e)
{
try
{
textLongDate.Text = dt.ToString(updLongDatePattern.Text,
curci.DateTimeFormat);
}
catch {}
}
Use the ToString()
method for the given date which converts the value of this instance to its equivalent string representation using specified format and culture-specific format information.