Friday, October 8, 2010

Type of GLOBALIZATION CultureInfo Class & enum CultureTypes


Culture can be classified in 3 groups
1. Invariant Culture
2. Neutral Culture
3. Specific Culture


NEUTRAL vs. SPECIFIC:
Neutral Culture contains only Language not region or Country
e.g English (en), French (fr), and Spanish (sp).

Specific Culture contains detail of both Language and region.
e.g en-US(English in United States),es-US etc.

Note : For getting the Current Culture
CultureInfo cInfo = Thread.CurrentThread.CurrentCulture;
Console.WriteLine("Name {0}",cInfo.Name);

//O/P->en-US
Console.WriteLine("Display Name {0}", cInfo.DisplayName);
//O/P->English
Console.WriteLine("Native Name {0}", cInfo.NativeName);
//O/P->English
Console.WriteLine("TwoLetterISOLanguageName {0}", cInfo.TwoLetterISOLanguageName);
//O/P->en
Console.Read();

NOTE : CurrentUICulture must be set at the application’s startup

CultureInfo cInfo = Thread.CurrentThread.CurrentUICulture;
Result will be same like above.

The CurrentCulture can be manipulated at any time during execution. However, this is not the case
With the CurrentUICulture. To be used correctly, the CurrentUICulture must be set at the onset of the
application (ideally in the Main method) or in a form’s constructor. Failure to do so can result in unexpected behavior.


CultureTypes is a enumType & it is Used with the GetCultures() function.
e.g
CultureTypes.AllCultures;
CultureTypes.FrameworkCultures;
CultureTypes.NeutralCultures ;
CultureTypes.SpecificCultures;


Sample Code

foreach (CultureInfo UsersCulture in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
{
Console.WriteLine("Culture: " + UsersCulture.Name);
}
Console.Read();

Sample Example :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Windows.Forms;
using System.Threading;
namespace Globalization_Part1
{
/*
* Step 1. create a window applicatio.
* Add a comboBox and set its ID =cbCulture
* Add three labels set the ID and text of all the labels.
*/

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
/*
* Fill the comboBox at Form intialization
*/

foreach (CultureInfo UsersCulture in CultureInfo.GetCultures(CultureTypes.SpecificCultures ))
{
cbCulture.Items.Add(UsersCulture);
}
Console.Read();
}

private void button1_Click(object sender, EventArgs e)
{
if (txtCurrency.Text == "")
{
MessageBox.Show("Please supply a numeric value to currency e.g 1000"); }
else
{
//select the comboBox selected value
object val = cbCulture.SelectedItem;
CultureInfo cinfo = new CultureInfo(val.ToString());
Thread.CurrentThread.CurrentCulture = cinfo;
double cuurency = Convert.ToDouble(txtCurrency.Text);
//Set the currecy and dateTime with CultureInfo class setting.
lblCurrency.Text=cuurency.ToString("C", cinfo);
lbldate.Text = DateTime.Now.ToString(cinfo);
txtCurrency.Text="";
}
}
}
}


Points to be Remembered.
1. For creating a custom culture you should have to use CultureAndInfoBuilder Class.

2. Different Culture has different sorting and comparision rules.
to make a consistent sorting and comparision u should have to use CultureInfo.InvariantCulture.

3. In Formatting o/p ToString() and dateTime() helps.

No comments: