将This(我的主要形式)传递给处理方向更改/和语言更改的类

本文关键字:方向 处理 语言 我的 This | 更新日期: 2023-09-27 17:54:21

我有这个麻烦,我想达到的目标是:我有一个主表单是启动表单(MDI Container)然后几子表单的形式,我想实现的是以下,这是代码,是发生在每一个屏幕,我想建立成一个类,但我不知道如何通过(单击事件发生在每个表单,我想要运行一个菜单点击MDI父(容器):

更新

我已经得到它与所有的ChildForms工作,谢谢@JamesBarras这就是我的代码现在是如何工作的,我已经删除了按钮点击事件,因为它只是从MenuStripItem_Click事件

#region Change Language
  private void englishToolStripMenuItem_Click(object sender, EventArgs e) {
     Class1 cls = new Class1();
     /// This is for Main and all the forms of MainForms Children
     foreach (var childForm in this.MdiChildren) {
        cls.ChangeLanguage(sender, log, childForm, this, this.menuStrip, "en");
        //ChangeLanguage("en", childForm);
     } 
  }
  private void arabicToolStripMenuItem_Click(object sender, EventArgs e) {
     Class1 cls = new Class1();
     /// This is for Main and all the forms of MainForms Children
     foreach (var childForm in this.MdiChildren) {
        cls.ChangeLanguage(sender, log, childForm, this, this.menuStrip, "ar");
        //ChangeLanguage( "ar", childForm);
     }
  }
  #endregion

我刚刚创建这个类的代码运行在相同的页面上单击事件作为一个方法,我希望我使用/创建的类(ChangeLanguage)知道,因为在正常的形式,我可以说但在一个类不知道的形式,我有我的点击事件,这是我在我创建的类代码:

更新

我已经很好地调整了类来做我需要它做的事情,然而,我仍然无法翻译MenuStripItems,我正在获得这个的值,但仍然没有菜单条翻译只有方向开关工作

namespace languageChange.Classes {   class Class1 {
  #region Methods
  /// <summary>
  /// This is mainly to change the language and the layout direction
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="log"></param>
  /// <param name="form"></param>
  /// <param name="thiss"></param>
  /// <param name="strip"></param>
  /// <param name="lang"></param>
  public void ChangeLanguage(object sender, Logger log, Form form, Form thiss, Control strip, string lang) {
     string senderText = sender.GetType().ToString();
       RightToLeft direction = RightToLeft.No;
     if (lang == "ar") {
        direction = RightToLeft.Yes;
     }
     thiss.RightToLeft = direction;
     CultureInfo CurrentLocale = new CultureInfo(lang);
     Thread.CurrentThread.CurrentCulture = new CultureInfo(lang);
     Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
     foreach (Control childForm in form.Controls) {
        ComponentResourceManager resources = new ComponentResourceManager(form.GetType());
        resources.ApplyResources(childForm, "$this");
        childForm.RightToLeft = direction;
        if (log.isDebugEnabled) log.Debug("--------------------------------------> c = " + childForm.Name);
        RefreshResources(log, lang, childForm, resources, CurrentLocale, strip);
     }
  }
  /// <summary>
  /// This is to for the Refresh all the Resources
  /// </summary>
  /// <param name="log"></param>
  /// <param name="lang"></param>
  /// <param name="ctrl"></param>
  /// <param name="resources"></param>
  /// <param name="CurrentLocale"></param>
  /// <param name="strip"></param>
   private static void RefreshResources(Logger log, string lang, Control ctrl, ComponentResourceManager resources, CultureInfo CurrentLocale, Control strip) {
     ctrl.SuspendLayout();
     resources.ApplyResources(ctrl, ctrl.Name, CurrentLocale);
     foreach (Control control in ctrl.Controls) {
        RefreshResources(log, lang, control, resources, CurrentLocale, strip); // recursion
        if (strip is ToolStrip) {
           RefreshResources(((ToolStrip)strip).Items, resources, CurrentLocale);
        }
        ctrl.ResumeLayout(true);
        if (log.isTraceEnabled) log.Trace("c=" + ctrl.Name);
     }
  }
  /// <summary>
  /// Which is done here [Refer to previous Summary]
  /// </summary>
  /// <param name="col"></param>
  /// <param name="resources"></param>
  /// <param name="CurrentLocale"></param>
  private static void RefreshResources(ToolStripItemCollection col, ComponentResourceManager resources, CultureInfo CurrentLocale) {
     foreach (ToolStripMenuItem item in col) {
        if (item is ToolStripMenuItem) {               
           RefreshResources(((ToolStripMenuItem)item).DropDownItems, resources, CurrentLocale);
        }
        resources.ApplyResources(item, item.Name, CurrentLocale);
     }
  }
  #endregion   }}

更新

所以我得到了所有的翻译,并改变方向除了MenuStripItems和Menustrip。

请帮助我,因为这真的很重要,我正在挣扎。

这是一个使用Visual Studio 2013 Ultimate的Winforms应用程序,使用c#。

不要介意记录器,它是我刚刚添加的跟踪和调试日志

将This(我的主要形式)传递给处理方向更改/和语言更改的类

ChangeLanguage(string lang)的签名更改为ChangeLanguage(Form form, string lang),以便您可以将希望更改的表单传递给它。

之后,您现在需要将正在处理的表单引用为传入的表单。因此,将该方法中的this替换为form,将typeof(Form1)类型替换为form.GetType()

在任何你想改变语言的地方,你现在可以调用

Class1.ChangeLanguage(this, "en");

其中this为当前形式

要更改MDIParent的语言,您需要这样做:

  private void englishToolStripMenuItem_Click(object sender, EventArgs e) 
  {
     Class1.ChangeLanguage(log, this, "en");
     foreach(var child in this.MdiChildren)
     {
         Class1.ChangeLanguage(log, child, "en");
     }
  }

注意,要改变菜单栏本身的语言,你需要添加一个RefreshResources重载来处理ToolStripItemCollections(它们是基于组件的,而不是基于控件的)

    private static void RefreshResources(ToolStripItemCollection col, ComponentResourceManager resources, CultureInfo CurrentLocale)
    {
        foreach(ToolStripItem item in col)
        {
            if (item is ToolStripMenuItem)
            {
                RefreshResources(((ToolStripMenuItem)item).DropDownItems, resources, CurrentLocale);
            }
            resources.ApplyResources(item, item.Name, CurrentLocale);
        }
    }
并将以下代码添加到原始的RefreshResources
    if (ctrl is ToolStrip)
    {
        RefreshResources(((ToolStrip)ctrl).Items, resources, CurrentLocale);
    }