Showing posts with label Globalization. Show all posts
Showing posts with label Globalization. Show all posts

Monday, December 21, 2009

Globalization in Windows Forms - Application with multi language support

This blog is the continuation of .Net Globalization blog.
If you want to have a global market for your project then you have to plan for implementing multi language support.

Create a form as shown below.










Add Another form. This is the form whose language text we are planning to change based on the selection from the combobox of first form. Give English Texts to Form and Controls.










Select the form set the Localizable property to True and Language to French(France).

















Now select the existing controls and change its Text property to corresponding French equivalent as shown in the figure.



















Go back to the first form where you can select the language.
In this form we are going to change the culture of the current thread based on the culture
Explanations and additional information will follow soon.


using System.Globalization;
using System.Threading;

private void cboLang_SelectedIndexChanged(object

sender, EventArgs e)
{
if(cboLang.Text == "French")
Thread.CurrentThread.CurrentUICulture = new

CultureInfo("fr-FR");
else
Thread.CurrentThread.CurrentUICulture = new

CultureInfo("en-US");

}

private void btnShowForm_Click(object sender, EventArgs

e)
{
frmLang fl = new frmLang();
fl.Show();
}

VB .Net

Imports System.Globalization
Imports System.Threading

Public Class Form1


Private Sub cboLang_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboLang.SelectedIndexChanged

If cboLang.Text = "English" Then
Thread.CurrentThread.CurrentUICulture = New CultureInfo("en-US")
Else
Thread.CurrentThread.CurrentUICulture = New CultureInfo("fr-FR")
End If

End Sub


Private Sub btnShowForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowForm.Click

Dim fl As New frmLang
fl.Show()
End Sub

End Class


Now when you run the application, select French from the ComboBox and click show form the French form will appear and when you select English, English form will appear.



















Windows Forms VB .Net Generated Code
'Form1


Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.cboLang = New System.Windows.Forms.ComboBox
Me.btnShowForm = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(52, 70)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(88, 13)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Select Language"
'
'cboLang
'
Me.cboLang.FormattingEnabled = True
Me.cboLang.Items.AddRange(New Object() {"English", "French"})
Me.cboLang.Location = New System.Drawing.Point(159, 62)
Me.cboLang.Name = "cboLang"
Me.cboLang.Size = New System.Drawing.Size(121, 21)
Me.cboLang.TabIndex = 1
'
'btnShowForm
'
Me.btnShowForm.Location = New System.Drawing.Point(159, 113)
Me.btnShowForm.Name = "btnShowForm"
Me.btnShowForm.Size = New System.Drawing.Size(75, 23)
Me.btnShowForm.TabIndex = 2
Me.btnShowForm.Text = "&Show Form"
Me.btnShowForm.UseVisualStyleBackColor = True
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.btnShowForm)
Me.Controls.Add(Me.cboLang)
Me.Controls.Add(Me.Label1)
Me.Name = "Form1"
Me.Text = "Shalvin.com"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub

Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents cboLang As System.Windows.Forms.ComboBox
Friend WithEvents btnShowForm As System.Windows.Forms.Button






'frmLang


Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.txtName = New System.Windows.Forms.TextBox
Me.Label2 = New System.Windows.Forms.Label
Me.txtPhone = New System.Windows.Forms.TextBox
Me.btnSave = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(41, 55)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(35, 13)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Name"
'
'txtName
'
Me.txtName.Location = New System.Drawing.Point(103, 48)
Me.txtName.Name = "txtName"
Me.txtName.Size = New System.Drawing.Size(100, 20)
Me.txtName.TabIndex = 1
'
'Label2
'
Me.Label2.AutoSize = True
Me.Label2.Location = New System.Drawing.Point(41, 105)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(38, 13)
Me.Label2.TabIndex = 2
Me.Label2.Text = "Phone"
'
'txtPhone
'
Me.txtPhone.Location = New System.Drawing.Point(103, 98)
Me.txtPhone.Name = "txtPhone"
Me.txtPhone.Size = New System.Drawing.Size(100, 20)
Me.txtPhone.TabIndex = 3
'
'btnSave
'
Me.btnSave.Location = New System.Drawing.Point(103, 140)
Me.btnSave.Name = "btnSave"
Me.btnSave.Size = New System.Drawing.Size(75, 23)
Me.btnSave.TabIndex = 4
Me.btnSave.Text = "Save"
Me.btnSave.UseVisualStyleBackColor = True
'
'frmLang
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.btnSave)
Me.Controls.Add(Me.txtPhone)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.txtName)
Me.Controls.Add(Me.Label1)
Me.Name = "frmLang"
Me.Text = "English"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents txtName As System.Windows.Forms.TextBox
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents txtPhone As System.Windows.Forms.TextBox
Friend WithEvents btnSave As System.Windows.Forms.Button

Monday, May 19, 2008

.Net Globalization

Globalization is the process of designing and developing applications that function for multiple cultures.

Lets begin our discussion on .Net Globalization with an ASP .Net example. Start an asp. Net project. Go to web.config and type the following:






Place a Calendar control on the form. When you run the application you will see an Arabic calendar with the Hijri year.


















Displaying All Cultures

Here I am creating an array of CultureInfo class. CultureInfo class contains information about a culture like its name, Calendar, Day Names, Month Names, Currency Format, etc.

SpecificCulture is a culture type where there is language code as well as country code. Examples are ar-SA, ml-IN, fr-FR. Here the first portion represents the Language Code and the second portion represents the Country Code.
using System.Globalization;

private void Form1_Load(object sender, EventArgs e)
{
CultureInfo[] cis = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
foreach (CultureInfo ci in cis)
listBox1.Items.Add(String.Format( ci.ToString()));
}

Displaying DisplayName and NativeName and Day Names of a Selected Culture

























using System.Globalization;
private void Form1_Load(object sender, EventArgs e)
{
CultureInfo[] cis = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
foreach (CultureInfo ci in cis)
comboBox1.Items.Add(String.Format( ci.ToString()));
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
lstDetails.Items.Clear();
CultureInfo ci = new CultureInfo(comboBox1.Text);
lstDetails.Items.Add(ci.DisplayName.ToString());
lstDetails.Items.Add(ci.NativeName.ToString());
lstDetails.Items.Add("");
lstDetails.Items.Add("Day names");
string[] strDayNames = ci.DateTimeFormat.DayNames;
foreach (string strDay in strDayNames)
lstDetails.Items.Add(strDay);

lstDetails.Items.Add("");
lstDetails.Items.Add("Month Names");
string[] strMonthNames = ci.DateTimeFormat.MonthNames;
foreach(string strMonth in strMonthNames)
lstDetails.Items.Add(strMonth);
}



Related Blog
Globalization in Windows Forms - Application with multi language support


Happy Programming
shalvin.com