Hello Friends,
We often came across the problems related to the geographical scope of applications. The costs of not knowing who will be using the applications are increasing as well. Dealing with issues as an afterthought is always more costly than designing for them, and building applications for different locales is no different in this respect. The .NET Framework provides the System.Globalization namespace to help developers address such concerns.
CultureInfo Class
One of the core tools for manipulating and retrieving information about the cultural context an application is running in is the CultureInfo class. This class provides culture-specific information such as the format of numbers and dates, and casing conventions. More broadly, it represents the name of a culture, the culture's writing system, the culture's calendar, the culture's language and sublanguages if applicable, and the country and region of the culture, and it provides methods to manipulate all these aspects. The basic uses of the CultureInfo class are shown in the following list:
Control how string comparisons are performed.
Control how number comparisons and formats are performed.
Control how date comparisons and formats are performed.
Control how resources are retrieved and used.
As a rule, a culture will be grouped into one of three categories: an invariant culture, a neutral culture, or a specific culture. The distinctions between these categories are detailed in the following list:
Invariant Culture This culture category is culture-insensitive. The category is to be used as essentially a default culture when consistency is desired. One situation where this category might be desirable to use is creating a trial application with a hard-coded expiration date. Using an invariant will allow you to check for a specific date, regardless of the culture's format, which will greatly simplify the task of comparing these dates. The invariant culture is not based on the English language per se, but it is associated with it and bears more similarities to it than to any other identifiable culture. Although it might be tempting to use the invariant culture for every possible comparison and just ignore specific cultural comparisons, doing so is a great mistake. Without intending to do so, you can overuse an invariant culture and end up with language that is syntactically incorrect or inappropriate.
Neutral Culture English (en), French (fr), and Spanish (sp) are all neutral cultures. A neutral culture is associated with a language but has no relationship to countries or regions. For instance, the English spoken in England is different from that spoken in the United States. The same holds true for Spanish spoken in Mexico versus that spoken in Spain. As mentioned earlier, the neutral culture will be designated by the first two characters in the CultureInfo class. If only two letters are specified, they will be the Neutral class. Although neutral cultures, like the invariant culture, might be tempting to use, they should be avoided as well, if possible, for the same reasons that invariants should be avoided. The language spoken in different countries that are covered by a neutral culture will almost certainly be different in at least a few respects. In reality, the differences will be many. Therefore, overuse of neutral cultures can result in incorrect or inappropriate language.
Specific Culture This is the most precise of the three categories and is represented by a neutral culture, a hyphen, and then a specific culture abbreviation. For instance, in the designations "fr-FR" and "en-US", fr and en represent the neutral culture (French and English, respectively), and FR and US represent the specific culture (France and the United States, respectively). Specific cultures should be used if at all possible.
To detect a user's current culture information, use the CurrentCulture property of the executing thread's CurrentThread property, as shown in the following code sample:
CultureInfo UsersCulture = Thread.CurrentThread.CurrentCulture;
MessageBox.Show("The current culture of this application is : " + UsersCulture.Name);
MessageBox.Show("The Display Name of this application is : "
+ UsersCulture.DisplayName);
MessageBox.Show("The Native Name of this application is : "
+ UsersCulture.NativeName);
MessageBox.Show("The ISO Abbreviation of this application is : "
+ UsersCulture.TwoLetterISOLanguageName);
Setting the current culture is similar to retrieving it. CurrentCulture is a property of the CurrentThread property of the Thread class; therefore, all that's needed to change or set this property is to specify a different value for it, as shown in the following code:
// C#
CultureInfo UsersCulture = Thread.CurrentThread.CurrentCulture;
MessageBox.Show("The current culture of this application is : "
+ UsersCulture.Name);
//Change this to Spanish/Venezuela
Thread.CurrentThread.CurrentCulture = new CultureInfo("es-VE");
MessageBox.Show("The current culture of this application is : "
+ Thread.CurrentThread.CurrentCulture);
Thanks,
Paras Sanghani
Hi Paras, I like your way of explanation. It's very easy to understand. Very useful Blog I must say.
ReplyDeleteKalyan Ch.,
Thanks Dear.
ReplyDelete