Share via

How to avoid setting all control modifiers to internal

Hans 251 Reputation points
2021-02-20T15:47:01.48+00:00

Hi,

I am looking at how to make a program multilingual. In this case Dutch and English. I made 2 resource files and you can switch languages ​​with the function "SetLanguageMenuItem ()". Because I have put the language switching in a separate class I have to adapt the modifier of the components on the main screen from private to internal. This seems right to me. How can that be better?

In the main form:

private void SetLanguageMenuItem()
{
    if (JsonObjSettings.AppParam[0].Language == "en-US")
    {
        ToolStripMenuItemLanguageEnglish.Checked = true;
        ToolStripMenuItemLanguageDutch.Checked = false;
        cul = CultureInfo.CreateSpecificCulture("en-US");
    }
    else if (JsonObjSettings.AppParam[0].Language == "nl-NL")
    {
        ToolStripMenuItemLanguageEnglish.Checked = false;
        ToolStripMenuItemLanguageDutch.Checked = true;
        cul = CultureInfo.CreateSpecificCulture("nl-NL");
    }

    SwitchLanguage sl = new SwitchLanguage(this, cul);
    sl.SetLanguage();
}

The separate class:

public class SwitchLanguage 
{
    ResourceManager res_man;    // Declare Resource manager to access to specific cultureinfo
    CultureInfo cul;            // Declare culture info
    public FormMain Parent { get; set; }

    public SwitchLanguage(FormMain Parent, CultureInfo cul)
    {
        res_man = new ResourceManager("TopData.Resource.Res", typeof(FormMain).Assembly);
        this.cul = cul;
        this.Parent = Parent;
    }

    public  void SetLanguage()
    {
        System.Windows .Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
        //MainForm menu
        //oolStripMenuItemLanguage.Text = res_man.GetString("Language", cul);
        Parent.ToolStripMenuItemProgram.Text = res_man.GetString("MainForm_Menu_Program", cul);
        Parent.ToolStripMenuItemChangeUser.Text = res_man.GetString("MainForm_Menu_Program_Switch_user", cul);
        Parent.ToolStripMenuItemClose.Text = res_man.GetString("MainForm_Menu_Program_Exit", cul);

        Parent.ToolStripMenuItemMaintain.Text = res_man.GetString("MainForm_Menu_Manage", cul);
        Parent.ToolStripMenuItemMaintainQueries.Text = res_man.GetString("MainForm_Menu_Manage_Queries", cul);
        Parent.ToolStripMenuItemMaintainDbConnections.Text = res_man.GetString("MainForm_Menu_Manage_Connections", cul);
        Parent.ToolStripMenuItemmaintainUsers.Text = res_man.GetString("MainForm_Menu_Manage_Users", cul);
        Parent.ToolStripMenuItemMaintainQueryGroups.Text = res_man.GetString("MainForm_Menu_Manage_QueryGroups", cul);

        Parent.ToolStripMenuItemOptions.Text = res_man.GetString("MainForm_Menu_Options", cul);
        Parent.ToolStripMenuItemConfigure.Text = res_man.GetString("MainForm_Menu_Options_Settings", cul);
        Parent.ToolStripMenuItemLanguage.Text = res_man.GetString("MainForm_Menu_Options_Language", cul);
        Parent.ToolStripMenuItemLanguageDutch.Text = res_man.GetString("MainForm_Menu_Options_Language_NL", cul);
        Parent.ToolStripMenuItemLanguageEnglish.Text = res_man.GetString("MainForm_Menu_Options_Language_EN", cul);
        Parent.ToolStripMenuItemAbout.Text = res_man.GetString("MainForm_Menu_Options_Language_Info", cul);

        System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
    }
}

Greetings, Hans

Developer technologies | Windows Forms
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.


1 answer

Sort by: Most helpful
  1. Alberto Poblacion 1,571 Reputation points
    2021-02-21T17:33:47.497+00:00

    If you are following the standard winforms convention for placing and naming the languaje resources, then you can change them all at a time using ComponentResourceManager.ApplyResources. There are plenty of examples around the 'net for for how to use this.

    This method has the advantage that you do not have to enumerate the controls one by one like you are doing in your code. Therefore, the change-language routine would be the same for all your forms. That being the case, you can place the routine in a parent class for all your forms, and then you would not need to mark the controls as internal. Just make the routine public and invoke it from wherever you want.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.