アプリケーション内で動的にローカリゼーションを切り替えるサンプル。
子フォームや子コントロールの言語を再帰的に更新してきます。
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 ); }
}
}
}
}
}
もっと良い方法ないかな・・・