2011-09-26 14 views
10

C#でWindowsフォームプログラムを作成しました。ローカリゼーションにいくつか問題があります。私は2つの言語(1つは英語、もう1つはフランス語)のリソースファイルを持っています。実行時に各言語ボタンをクリックして言語を変更したいランタイム時にWinFormsアプリケーションのカルチャを変更する方法

ボタンをクリックしても機能しません。私はこのコードを使用しています。任意のplsはこの上で役立つだろう

private void btnfrench_Click(object sender, EventArgs e) 
{ 
    getlanguage("fr-FR"); 
} 

private void getlanguage(string lan) 
{ 
    foreach (Control c in this.Controls) 
    { 
     ComponentResourceManager cmp = 
      new ComponentResourceManager(typeof(BanksForm)); 
     cmp.ApplyResources(c, c.Name, new CultureInfo(lan)); 
    } 
} 

......

多くのおかげで....

答えて

18

これは機能しました:

private void button1_Click(object sender, EventArgs e) 
{ 
    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr-BE"); 
    ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1)); 
    resources.ApplyResources(this, "$this"); 
    applyResources(resources, this.Controls); 
} 

private void applyResources(ComponentResourceManager resources, Control.ControlCollection ctls) 
{ 
    foreach (Control ctl in ctls) 
    { 
     resources.ApplyResources(ctl, ctl.Name); 
     applyResources(resources, ctl.Controls); 
    } 
} 

誰も使用しないようなホイッスルを付けないように注意してください。

+0

申し訳ありませんが、これを試してみましたが、私にとってはうまくいきません。 –

+0

フォームにリソースファイルを追加する必要があります。ローカリゼーションプロパティをtrueに変更し、英語をベルギー語に変更しましたが、私は選択しました...そして私は余分なリソースファイルがフォームに追加されるのを見ています... –

+1

あなたはそれを開始していないし、切り替える方法を知りたいですか? 「私が選択した言語を表示していない」とはどういう意味か分かりません。プロパティを編集する必要があります。 Languageプロパティを変更した後、たとえば、フォームのTextプロパティを設定します。これにより自動的にForm1.fr-BE.resxファイルが作成されます。フォームの横にあるノードを開き、ノードを表示します。 –

5

あなたがコントロールに再帰的にApplyResourcesを呼び出す必要があります:

private void btnfrench_Click(object sender, EventArgs e) 
{ 
    ApplyResourceToControl(
     this, 
     new ComponentResourceManager(typeof(BanksForm)), 
     new CultureInfo("fr-FR")) 
} 

private void ApplyResourceToControl(
    Control control, 
    ComponentResourceManager cmp, 
    CultureInfo cultureInfo) 
{ 
    cmp.ApplyResources(control, control.Name, cultureInfo); 

    foreach (Control child in control.Controls) 
    { 
     ApplyResourceToControl(child, cmp, cultureInfo); 
    } 
} 
+0

私はあなたのコードを試してみましたが、それは働いていません.... –

0

実行時にCultureInfoを更新すると、コンポーネントのサイズがリセットされることがあります。このコードは、コントロール のサイズと位置を維持する(まだ(SuspendLayoutを使用している、しかし目に見えるちらつきが発生します)解決しませんでした)


    private void langItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e) 
    { 
     //I store the language codes in the Tag field of list items 
     var itemClicked = e.ClickedItem; 
     string culture = itemClicked.Tag.ToString().ToLower(); 

     Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(culture); 
     ApplyResourceToControl(
     this, 
     new ComponentResourceManager(typeof(GUI)), 
     new CultureInfo(culture));   
    } 

    private void ApplyResourceToControl(
     Control control, 
     ComponentResourceManager cmp, 
     CultureInfo cultureInfo) 
    { 
     foreach (Control child in control.Controls) 
     { 
      //Store current position and size of the control 
      var childSize = child.Size; 
      var childLoc = child.Location; 
      //Apply CultureInfo to child control 
      ApplyResourceToControl(child, cmp, cultureInfo); 
      //Restore position and size 
      child.Location = childLoc; 
      child.Size = childSize; 
     } 
     //Do the same with the parent control 
     var parentSize = control.Size; 
     var parentLoc = control.Location; 
     cmp.ApplyResources(control, control.Name, cultureInfo); 
     control.Location = parentLoc; 
     control.Size = parentSize; 
    } 
関連する問題