Enable multilanguage application in .NET :
Here i'm going to explain about how to enable multilanguage support in windows application ( VB.NET ).
Multilanguage can be done through resources file creating. Multilanguage enabled Resource files can be created in various ways based on your requirements.
A. System Resource files Generation
-----------------------------------------------
1. Resource files generation for each form.
You have to do the following steps for each form.
1.Create a new Windows Application named "WindowsApplication1". For details, see How to:
Create a Windows Application Project.
2.In the Properties window, set the form's Localizable property to true.
The Language property is already set to (Default).
3.Drag a Button control from the Windows Forms tab of the Toolbox to the form, and set its Text
property to Hello World.
4.Set the form's Language property to German (Germany).
5.Set the button's Text property to Hallo Welt.
6.Set the form's Language property to French (France).
7.Set the button's Text property to Bonjour le Monde. You can resize the button to accommodate the
longer string, if necessary.
8.Save and build the solution.
9.Click the Show All Files button in Solution Explorer.
The resource files appear underneath Form1.vb, Form1.cs, or Form1.jsl. Form1.resx is the
resource file for the default culture, which will be built into the main assembly. Form1.de-DE.resx is the
resource file for German as spoken in Germany. Form1.fr-FR.resx is the resource file for French as
spoken in France.
In addition, you will see files appear named Form1.de.resx and Form1.fr.resx. Visual Studio
automatically creates these files in order to work around a limitation in Visual SourceSafe having to do
with adding new files to a project during a save operation. The .resx files are empty and contain no
resources.
10.Press the F5 key or choose Start from the Debug menu.
Assigning Language based on selection : Selection control may be link or combobox or form load etc..
on selection changed , plz add code like this
' Visual Basic
' Sets the UI culture to French (France).
Thread.CurrentThread.CurrentUICulture = New CultureInfo("fr-FR")
// C#
// Sets the UI culture to French (France).
Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR");
Note : this seems easier but in real time, we dont want to translate in each screen which already
translated.
ex. customer code label control will be used through out the application or in various places.
now nobody will like to translate customer code label in all forms. so in this case, we can go for
Manual Resource file approach.
2. common resource files for whole application.
Here we can muliple resource files in MyProject like Resources.resx, Resources.zh-CN.resx. In each file
you can transate it but key should be same in both file.
Getting Resource file values :
My.Resources.<Key>.
when you change culture to Zh-CN then it would take values from specified language resource file
automatically.
B. Manual Resource file
-------------------------
Instead of transating on each screen (Approach 1 here), we can cumulate all distinct words together and
make a separate resource file. then convert it once.
but you have to assign manually to a control or you can write common function to load.
Reading value from resource file :
1.On the Project menu, click Add New Item.
2.In the Templates box, select the Assembly Resource File template. Type the file name
"WinFormStrings.resx" in the Name box. The file WinFormStrings.resx will contain fallback resources
in English. These resources will be accessed whenever the application cannot find resources more
appropriate to the UI culture.
The file is added to your project in Solution Explorer and automatically opens in the XML Designer in
Data view.
3.In the Data Tables pane, select data.
4.In the Data pane, click an empty row and enter strMessage in the name column and Hello World in the
value column.
You do not need to specify the type or mimetype for a string; they are used for objects. The type
specifier holds the data type of the object being saved. The MIME type specifier holds the base type
(base64) of the binary information stored, if the object consists of binary data.
5.On the File menu, click Save WinFormStrings.resx.
6.Do steps 1-5 twice more to create two more resource files named WinFormStrings.de-DE.resx and
WinFormStrings.fr-FR.resx, with the string resources specified in the following table. The file
WinFormStrings.de-DE.resx will contain resources that are specific to German as spoken in
Germany. The file WinFormStrings.fr-FR.resx will contain resources that are specific to French as
spoken in France.
To access the manually added resources :
' Visual Basic
Imports System.Resources
// C#
using System.Resources;
' Visual Basic
' Declare a Resource Manager instance.
Dim LocRM As New ResourceManager("WindowsApplication1.WinFormStrings",
GetType(Form1).Assembly)
' Assign the string for the "strMessage" key to a message box.
MessageBox.Show(LocRM.GetString("strMessage"))
// C#
// Declare a Resource Manager instance.
ResourceManager LocRM = new
ResourceManager("WindowsApplication1.WinFormStrings",typeof(Form1).Assembly);
// Assign the string for the "strMessage" key to a message box.
MessageBox.Show(LocRM.GetString("strMessage"));
Example :
I'm assiging cultures in form load here.
Public Sub Load_Grid_ResourceFile(ByVal LangId As String)
Dim _Curr_culture As String = My.Application.UICulture.Name
Dim _culture As String
'' RESOURSE SETTINGS
'' gRm_Screen instance is for Screen Labels
'' resource_manager instance is for grid columns text
'' _Culture instance is for error msgs
If LangId = "2" Then
resource_manager = New System.Resources.ResourceManager("<Other language Resource file
name>", Me.GetType.Assembly)
gRm_Screen = New Resources.ResourceManager("<Other language Resource file name>",
Me.GetType.Assembly)
_culture = "zh-CN"
Else
resource_manager = New System.Resources.ResourceManager("<English language Resource
file name>", Me.GetType.Assembly)
gRm_Screen = New Resources.ResourceManager("<English language Resource file name>",
Me.GetType.Assembly)
_culture = "en-US"
End If
Dim _cultureinfo As New System.Globalization.CultureInfo(_culture)
'_cultureinfo.DateTimeFormat.DateSeparator = "/"
My.Application.ChangeUICulture(_culture)
BusinessLogicLayer.My.Resources.Culture = _cultureinfo
DataAccessLayer.My.Resources.Culture = _cultureinfo
END sub
Common function to load to controls :
Public Sub get_Literals(ByRef FrmObj As Object, ByVal LangId As String)
If gRm_Screen Is Nothing Then Exit Sub
Write_Literals(FrmObj, Nothing)
For Each Cntrl In FrmObj.Controls
GetControls(FrmObj, Cntrl)
Next
End Sub
Private Sub Write_Literals(ByRef FrmObj As Object, ByRef Cntrl As Control)
If Cntrl Is Nothing Then
If Trim(FrmObj.Text) <> "" Then
Dim _ResourceValue = get_ResourceString(FrmObj.Text)
If Trim(_ResourceValue) <> "" Then
FrmObj.Text = _ResourceValue
End If
End If
Else
If Trim(Cntrl.Text) <> "" Then
'Try
Dim _ResourceValue = get_ResourceString(Cntrl.Text)
If Trim(_ResourceValue) <> "" Then
Cntrl.Text = _ResourceValue
End If
'Catch ex As Exception
'End Try
End If
End If
End Sub
Public Function get_ResourceString(ByVal pValue As String, Optional ByVal
ReturnOldValueWhenNotFound As Boolean = False) As String
Dim _ValidValue As String
Dim _ResourceValue As String
'' Converting current value to resource file's format value
_ValidValue = get_ValidString(pValue)
'' Getting value from resource file
_ResourceValue = gRm_Screen.GetString(_ValidValue)
If Trim(_ResourceValue) <> "" Then
Return _ResourceValue
Else
If ReturnOldValueWhenNotFound Then
Return pValue
Else
Return ""
End If
End If
End Function
Private Function get_ValidString(ByVal pValue As String) As String
Return UCase(Replace(Replace(Trim(pValue), " ", "_"), vbCrLf, "_"))
End Function
Private Sub GetControls(ByRef FrmObj As Object, ByRef Cntrl As Control)
If Cntrl.HasChildren = True Then
Write_Literals(FrmObj, Cntrl)
For Each chldcntrl In Cntrl.Controls
GetControls(FrmObj, chldcntrl)
Next
Else
Write_Literals(FrmObj, Cntrl)
End If
End Sub
You can refer this to get clear idea
http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/57a00566-e13d-449d-bd30-a53a9dc6b838
http://msdn.microsoft.com/en-us/library/y99d1cd3(v=VS.80).aspx (Windows Forms)
http://msdn.microsoft.com/en-us/library/c6zyy3s9.aspx (ASP.NET)
Here i'm going to explain about how to enable multilanguage support in windows application ( VB.NET ).
Multilanguage can be done through resources file creating. Multilanguage enabled Resource files can be created in various ways based on your requirements.
A. System Resource files Generation
-----------------------------------------------
1. Resource files generation for each form.
You have to do the following steps for each form.
1.Create a new Windows Application named "WindowsApplication1". For details, see How to:
Create a Windows Application Project.
2.In the Properties window, set the form's Localizable property to true.
The Language property is already set to (Default).
3.Drag a Button control from the Windows Forms tab of the Toolbox to the form, and set its Text
property to Hello World.
4.Set the form's Language property to German (Germany).
5.Set the button's Text property to Hallo Welt.
6.Set the form's Language property to French (France).
7.Set the button's Text property to Bonjour le Monde. You can resize the button to accommodate the
longer string, if necessary.
8.Save and build the solution.
9.Click the Show All Files button in Solution Explorer.
The resource files appear underneath Form1.vb, Form1.cs, or Form1.jsl. Form1.resx is the
resource file for the default culture, which will be built into the main assembly. Form1.de-DE.resx is the
resource file for German as spoken in Germany. Form1.fr-FR.resx is the resource file for French as
spoken in France.
In addition, you will see files appear named Form1.de.resx and Form1.fr.resx. Visual Studio
automatically creates these files in order to work around a limitation in Visual SourceSafe having to do
with adding new files to a project during a save operation. The .resx files are empty and contain no
resources.
10.Press the F5 key or choose Start from the Debug menu.
Assigning Language based on selection : Selection control may be link or combobox or form load etc..
on selection changed , plz add code like this
' Visual Basic
' Sets the UI culture to French (France).
Thread.CurrentThread.CurrentUICulture = New CultureInfo("fr-FR")
// C#
// Sets the UI culture to French (France).
Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR");
Note : this seems easier but in real time, we dont want to translate in each screen which already
translated.
ex. customer code label control will be used through out the application or in various places.
now nobody will like to translate customer code label in all forms. so in this case, we can go for
Manual Resource file approach.
2. common resource files for whole application.
Here we can muliple resource files in MyProject like Resources.resx, Resources.zh-CN.resx. In each file
you can transate it but key should be same in both file.
Getting Resource file values :
My.Resources.<Key>.
when you change culture to Zh-CN then it would take values from specified language resource file
automatically.
B. Manual Resource file
-------------------------
Instead of transating on each screen (Approach 1 here), we can cumulate all distinct words together and
make a separate resource file. then convert it once.
but you have to assign manually to a control or you can write common function to load.
Reading value from resource file :
1.On the Project menu, click Add New Item.
2.In the Templates box, select the Assembly Resource File template. Type the file name
"WinFormStrings.resx" in the Name box. The file WinFormStrings.resx will contain fallback resources
in English. These resources will be accessed whenever the application cannot find resources more
appropriate to the UI culture.
The file is added to your project in Solution Explorer and automatically opens in the XML Designer in
Data view.
3.In the Data Tables pane, select data.
4.In the Data pane, click an empty row and enter strMessage in the name column and Hello World in the
value column.
You do not need to specify the type or mimetype for a string; they are used for objects. The type
specifier holds the data type of the object being saved. The MIME type specifier holds the base type
(base64) of the binary information stored, if the object consists of binary data.
5.On the File menu, click Save WinFormStrings.resx.
6.Do steps 1-5 twice more to create two more resource files named WinFormStrings.de-DE.resx and
WinFormStrings.fr-FR.resx, with the string resources specified in the following table. The file
WinFormStrings.de-DE.resx will contain resources that are specific to German as spoken in
Germany. The file WinFormStrings.fr-FR.resx will contain resources that are specific to French as
spoken in France.
To access the manually added resources :
' Visual Basic
Imports System.Resources
// C#
using System.Resources;
' Visual Basic
' Declare a Resource Manager instance.
Dim LocRM As New ResourceManager("WindowsApplication1.WinFormStrings",
GetType(Form1).Assembly)
' Assign the string for the "strMessage" key to a message box.
MessageBox.Show(LocRM.GetString("strMessage"))
// C#
// Declare a Resource Manager instance.
ResourceManager LocRM = new
ResourceManager("WindowsApplication1.WinFormStrings",typeof(Form1).Assembly);
// Assign the string for the "strMessage" key to a message box.
MessageBox.Show(LocRM.GetString("strMessage"));
Example :
I'm assiging cultures in form load here.
Public Sub Load_Grid_ResourceFile(ByVal LangId As String)
Dim _Curr_culture As String = My.Application.UICulture.Name
Dim _culture As String
'' RESOURSE SETTINGS
'' gRm_Screen instance is for Screen Labels
'' resource_manager instance is for grid columns text
'' _Culture instance is for error msgs
If LangId = "2" Then
resource_manager = New System.Resources.ResourceManager("<Other language Resource file
name>", Me.GetType.Assembly)
gRm_Screen = New Resources.ResourceManager("<Other language Resource file name>",
Me.GetType.Assembly)
_culture = "zh-CN"
Else
resource_manager = New System.Resources.ResourceManager("<English language Resource
file name>", Me.GetType.Assembly)
gRm_Screen = New Resources.ResourceManager("<English language Resource file name>",
Me.GetType.Assembly)
_culture = "en-US"
End If
Dim _cultureinfo As New System.Globalization.CultureInfo(_culture)
'_cultureinfo.DateTimeFormat.DateSeparator = "/"
My.Application.ChangeUICulture(_culture)
BusinessLogicLayer.My.Resources.Culture = _cultureinfo
DataAccessLayer.My.Resources.Culture = _cultureinfo
END sub
Common function to load to controls :
Public Sub get_Literals(ByRef FrmObj As Object, ByVal LangId As String)
If gRm_Screen Is Nothing Then Exit Sub
Write_Literals(FrmObj, Nothing)
For Each Cntrl In FrmObj.Controls
GetControls(FrmObj, Cntrl)
Next
End Sub
Private Sub Write_Literals(ByRef FrmObj As Object, ByRef Cntrl As Control)
If Cntrl Is Nothing Then
If Trim(FrmObj.Text) <> "" Then
Dim _ResourceValue = get_ResourceString(FrmObj.Text)
If Trim(_ResourceValue) <> "" Then
FrmObj.Text = _ResourceValue
End If
End If
Else
If Trim(Cntrl.Text) <> "" Then
'Try
Dim _ResourceValue = get_ResourceString(Cntrl.Text)
If Trim(_ResourceValue) <> "" Then
Cntrl.Text = _ResourceValue
End If
'Catch ex As Exception
'End Try
End If
End If
End Sub
Public Function get_ResourceString(ByVal pValue As String, Optional ByVal
ReturnOldValueWhenNotFound As Boolean = False) As String
Dim _ValidValue As String
Dim _ResourceValue As String
'' Converting current value to resource file's format value
_ValidValue = get_ValidString(pValue)
'' Getting value from resource file
_ResourceValue = gRm_Screen.GetString(_ValidValue)
If Trim(_ResourceValue) <> "" Then
Return _ResourceValue
Else
If ReturnOldValueWhenNotFound Then
Return pValue
Else
Return ""
End If
End If
End Function
Private Function get_ValidString(ByVal pValue As String) As String
Return UCase(Replace(Replace(Trim(pValue), " ", "_"), vbCrLf, "_"))
End Function
Private Sub GetControls(ByRef FrmObj As Object, ByRef Cntrl As Control)
If Cntrl.HasChildren = True Then
Write_Literals(FrmObj, Cntrl)
For Each chldcntrl In Cntrl.Controls
GetControls(FrmObj, chldcntrl)
Next
Else
Write_Literals(FrmObj, Cntrl)
End If
End Sub
You can refer this to get clear idea
http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/57a00566-e13d-449d-bd30-a53a9dc6b838
http://msdn.microsoft.com/en-us/library/y99d1cd3(v=VS.80).aspx (Windows Forms)
http://msdn.microsoft.com/en-us/library/c6zyy3s9.aspx (ASP.NET)
No comments:
Post a Comment