Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

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

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 ); }
                }
            }
        }
    }
}



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

krsak
仕事でC++(Qt)、趣味でC#
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away