3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Formのローカリゼーションを一括切り替え

Posted at

アプリケーション内で動的にローカリゼーションを切り替えるサンプル。
子フォームや子コントロールの言語を再帰的に更新してきます。

MainForm.cs
namespace Hoge
{
    public partial class MainForm : Form
    {
        #region メニューバー「Language
        private void englishToolStripMenuItem_Click( object sender, EventArgs e )
        {
            // メニューバー「Language」-「English」
            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo( "" );
            this.ChangeLanguage( this, null, Thread.CurrentThread.CurrentUICulture );
        }
        private void japaneseToolStripMenuItem_Click( object sender, EventArgs e )
        {
            // メニューバー「Language」-「Japanese」
            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo( "ja" );
            this.ChangeLanguage( this, null, Thread.CurrentThread.CurrentUICulture );
        }
        #endregion  // メニューバー「Language」

        /// <summary>
        /// フォーム、コントロール、メニューバー、ツールバー、ステータスバーの言語を再帰的に更新する
        /// </summary>
        private void ChangeLanguage( object obj, ComponentResourceManager resources, CultureInfo culture )
        {
            if ( obj is Form ) {
                Form form = obj as Form;
                ComponentResourceManager form_resources = new ComponentResourceManager( form.GetType() );
                form_resources.ApplyResources( form, "$this", culture );
                foreach ( Form fm in form.OwnedForms ) { this.ChangeLanguage( fm, form_resources, culture ); }
                foreach ( Form fm in form.MdiChildren ) { this.ChangeLanguage( fm, form_resources, culture ); }
                foreach ( Control c in form.Controls ) { this.ChangeLanguage( c, form_resources, culture ); }
            }
            else if ( obj is ToolStripItem ) {
                ToolStripItem item = obj as ToolStripItem;
                resources.ApplyResources( item, item.Name, culture );
                if ( item is ToolStripDropDownItem ) {
                    ToolStripDropDownItem dditem = item as ToolStripDropDownItem;
                    foreach ( ToolStripItem tsi in dditem.DropDownItems ) { this.ChangeLanguage( tsi, resources, culture ); }
                }
            }
            else if ( obj is Control ) {
                Control ctrl = obj as Control;
                resources.ApplyResources( ctrl, ctrl.Name, culture );
                foreach ( Control c in ctrl.Controls ) { this.ChangeLanguage( c, resources, culture ); }
                if ( ctrl is ToolStrip ) {
                    ToolStrip ts = ctrl as ToolStrip;
                    foreach ( ToolStripItem tsi in ts.Items ) { this.ChangeLanguage( tsi, resources, culture ); }
                }
            }
        }
    }
}

もっと良い方法ないかな・・・
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?