Home > Net >  Change Text (translation) on other UserControl form, VB.NET Question
Change Text (translation) on other UserControl form, VB.NET Question

Time:07-09

I need to use labels, and also popups with text. This text needs to be translated in any language, and that's what I did, using a database reference of translations in various .resx files: any label/object has its ID, which I associate the translation, then with a combobox I select the languages that do the translation.

With the labels of the form that contains the combobox no issues, Since I'm using a SwitchCase that calls out translating functions:

Private Sub LangSelector_SelectedIndexChanged(sender As Object, e As EventArgs) Handles LangSelector.SelectedIndexChanged
    With LangSelector.Items
        Select Case Me.LangSelector.SelectedIndex
            Case Is = 0
                SetItalian()
            Case Is = 1
                SetEnglish()
            Case Is = 2
                SetFrench()
            Case Is = 3
                SetSpanish()
            Case Is = 4
                SetItalian()
        End Select
    End With
End Sub

And translating functions...

Public Sub SetItalian()
        OpacityLabel.Text = My.Resources._0_ITA_LangText_ScaffPanel.OpacityLabel
        optIta.Text = My.Resources._0_ITA_LangText_ScaffPanel.optIta
        optEng.Text = My.Resources._0_ITA_LangText_ScaffPanel.optEng
    optEsp.Text = My.Resources._0_ITA_LangText_ScaffPanel.optEsp
    LangSelector.TabIndex = 0(My.Resources._0_ITA_LangText_ScaffPanel.optIta)

End Sub

Those resx elements are tables with label/radio button's ID elements with associated text. With the elements where there is the combobox (UserControl) no issues. Problems are facing with the elements in another UserControl... How do I tell the elements are available to be translated?

And also, I don't know how to change text inside a popup, for the sake of translation (it's a tooltip).

How do I also change text in the combobox properly for the same translation purposes?

CodePudding user response:

Is the comboBox nescessary? Can't you let the Current Culture decide which language to use?

My VB.Net is a bit rusty but I think this should work similar to how you would do it in C#, see How to use localization in C#

The link goes in to a lot more details on how to do this, it's tagged as C# but I think it wouldn't be hard to make it work in VB.

Essentially, create a resource file e.g. strings.resx for your primary language which it sounds like you have done, ensure you always assign every bit of text to your controls from that.

Add all your languages as a new resource file with the language name in it. e.g. strings.it.resx and you wan't need to change a line of code if you want to add more languages later.

Does this help?

Private Sub LangSelector_SelectedIndexChanged(sender As Object, e As EventArgs) Handles LangSelector.SelectedIndexChanged
    With LangSelector.Items
        Select Case Me.LangSelector.SelectedIndex
            Case Is = 0
                SetLanguage("it")
            Case Is = 1
                SetLanguage("en")
            Case Is = 2
                SetLanguage("fr")
            Case Is = 3
                SetLanguage("es")
            Case Is = 4
                SetLanguage("it")
        End Select
    End With
End Sub

Private Sub SetLanguage(language as String)
  Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(language);
  For Each (Control c in ???.Controls)
    If c.GetType == typeof(YourType)
      (CType(c,YourType)).UpdateLanguage()
    End If
  Next
End Sub

  • Related