Localizing the first form
-
Wednesday, March 29, 2006 12:08 PMHello all,
I am working on an application localized for 4 languages. The main (default) language is french. I am able to programatically change the CurrentUICulture at runtime, for all the forms except for the first one. I have 2 questions:- Is there any way to change the interface of the first form? Now I do that by "manually" reading from the ressource file (say Form1.en-GB.resx), but I would like this to be done automatically, just like with the other forms.
- Why switching back from english to french doesn't work? French is the default language. I can switch from any of the 4 languages to any other, but not to french. I also tried to remove the french ressource file, because I have read that if there is no ressource file for the specified culture, the default one is loaded - no working in my case. When I choose french at runtime, no text is displayed.
Eva
Answers
-
Thursday, March 30, 2006 1:15 PM
I found out how to solve the above problem. It's quite primitive approach, and I don't know exactly why it works, but here is the relevant code:
...
Select Case Me.cbLangue.SelectedItem.ToString
Case "English"
Thread.CurrentThread.CurrentUICulture = New CultureInfo("en-GB")
ChangeLabels("en-GB")
Case "Français"
Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(fmEPATRAS))
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture
With resources
.ApplyResources(Me.btnExit, "btnExit")
.ApplyResources(Me.btnCreate, "btnCreate")
.ApplyResources(Me.btnImport, "btnImport")
.ApplyResources(Me.lblCode, "lblCode")
.ApplyResources(Me.lblLanguage, "lblLanguage")
End With
Thread.CurrentThread.CurrentUICulture = New CultureInfo("fr-BE")
...
All Replies
-
Thursday, March 30, 2006 1:15 PM
I found out how to solve the above problem. It's quite primitive approach, and I don't know exactly why it works, but here is the relevant code:
...
Select Case Me.cbLangue.SelectedItem.ToString
Case "English"
Thread.CurrentThread.CurrentUICulture = New CultureInfo("en-GB")
ChangeLabels("en-GB")
Case "Français"
Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(fmEPATRAS))
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture
With resources
.ApplyResources(Me.btnExit, "btnExit")
.ApplyResources(Me.btnCreate, "btnCreate")
.ApplyResources(Me.btnImport, "btnImport")
.ApplyResources(Me.lblCode, "lblCode")
.ApplyResources(Me.lblLanguage, "lblLanguage")
End With
Thread.CurrentThread.CurrentUICulture = New CultureInfo("fr-BE")
... -
Tuesday, November 14, 2006 5:13 PM
With little help from Microsoft's fragmented Help system but through much trial and error, I found the simple way to do this. After discovering the solution, I found a hint at:
http://msdn2.microsoft.com/en-us/library/b28bx3bh(VS.80).aspx
The solution is to create a default constructor. (Visual Studio will insert a call to InitializeComponent() into the constructor.) Then add code as in the example below.
Public Sub New()
My.Application.ChangeUICulture(My.Application.Culture.Name)
' This call is required by the Windows Form Designer.
InitializeComponent()' Add any initialization after the InitializeComponent() call.
End Sub
If you want to use the regional setting in Control Panel (as I do) then you only need to set the UICulture. However, if you want to set the culture from a Registry value, an initialization file, an application setting, or something else, you will need to change both the UICulture and the Culture as in the MSDN article referenced above.
Other ways to change the setting to conform to the Control Panel regional setting are:
My.Application.ChangeUICulture(System.Globalization.CultureInfo.CurrentCulture.Name)
My.Application.ChangeUICulture(System.Threading.Thread.CurrentThread.CurrentCulture.Name)
System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture
System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.CurrentCultureAll of these approaches seem to work equally well. If one is preferable to the others, perhaps someone can comment. The .NET Compact Framework does not support per-thread CurrentUICulture settings which may be a concern for some. (http://msdn2.microsoft.com/en-gb/library/2weec7k5(VS.80).aspx)
Of course, this code can be shortened if you include the appropriate Imports statements.
I have discovered one quirk that only occurs in debug mode. (It works fine if I compile my application and run the executable.) If I change my Control Panel regional setting, I have to run my program twice in order to pick up the new setting. It always works on the second try, even if I put a breakpoint on the Public Sub New() statement and terminate the debug session at that point. (If I change the Control Panel setting and reboot, it works on the first try.)
And perhaps someone can also explain why the Form.Localizable property is undocumented.

