2011-08-08 98 views
5

我已經在C#中創建了Windows窗體程序。 我有一些本地化問題。 我有3種語言的資源文件。 我想單擊每個語言按鈕並在運行時更改語言。 當我在InitializeComponent()之前更改語言時,它可以正常工作。 但是當我點擊按鈕時,它不起作用。 我正在使用此代碼。運行時本地化

private void RussianFlag_Click(object sender, EventArgs e) 
{ 
    Thread.CurrentThread.CurrentUICulture = new CultureInfo("ru-RU"); 
} 
+0

您是否嘗試過更改'Thread.CurrentCulture'? – abatishchev

+0

在將線程文化設置爲另一種語言之後,您是否嘗試過調用'Refresh()'? –

+0

刷新()不起作用。 –

回答

4

您將需要重新加載控件,以反映新的文化價值觀

ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1)); 

,然後你就必須申請使用resources.ApplyResources

每個控制

請有look here

1

更改CurrentUICulture不會自動重新加載資源。您需要手動執行它(http://msdn.microsoft.com/en-us/magazine/cc163609.aspx#S8)

您可以將與本地化有關的代碼從InitializeComponent() 複製到另一個函數:

void LoadResources(){ 

    this.Title = MyApp.Resources.MainFormCaption; 
    this.lblWelcomeMessage.Text = MyApp.Resources.UserWelcome; 

} 
-1

感謝V4Vendetta等.. 解決辦法是...

private void RussianFlag_Click(object sender, EventArgs e) 
     { 
      if (currentLanguage != "RUS") 
      { 
       Thread.CurrentThread.CurrentUICulture = new CultureInfo("ru-RU"); 
       ChangeLanguage("ru-RU"); 
      } 
     } 

.... .... ...

private void ChangeLanguage(string lang) 
     { 
      foreach (Control c in this.Controls) 
      { 
       ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1)); 
       resources.ApplyResources(c, c.Name, new CultureInfo(lang)); 
       if (c.ToString().StartsWith("System.Windows.Forms.GroupBox")) 
       { 
        foreach (Control child in c.Controls) 
        { 
         ComponentResourceManager resources_child = new ComponentResourceManager(typeof(Form1)); 
         resources_child.ApplyResources(child, child.Name, new CultureInfo(lang)); 
        } 
       } 
      } 
     } 
+0

這是一個遞歸解決方案:Private void ChangeLanguage(Control ctl,string lang) {ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1)) ; resources.ApplyResources(ctl,ctl.Name,new CultureInfo(lang)); foreach(Control in ctl.Controls)ChangeLanguage(c,lang); } – TaW

11

我寫了一個RuntimeLocalizer類具有以下功能:

  • 更改和更新本地化所有Control S和SubControl S IN表單
  • 也改變了國產化,爲所有SubItem S的所有MenuStrip小號

用法示例:RuntimeLocalizer.ChangeCulture(MainForm, "en-US");


using System.Windows.Forms; 
using System.Globalization; 
using System.Threading; 
using System.ComponentModel; 

public static class RuntimeLocalizer 
{ 
    public static void ChangeCulture(Form frm, string cultureCode) 
    { 
     CultureInfo culture = CultureInfo.GetCultureInfo(cultureCode); 

     Thread.CurrentThread.CurrentUICulture = culture; 

     ComponentResourceManager resources = new ComponentResourceManager(frm.GetType()); 

     ApplyResourceToControl(resources, frm, culture); 
     resources.ApplyResources(frm, "$this", culture); 
    } 

    private static void ApplyResourceToControl(ComponentResourceManager res, Control control, CultureInfo lang) 
    { 
     if (control.GetType() == typeof(MenuStrip)) // See if this is a menuStrip 
     { 
      MenuStrip strip = (MenuStrip)control; 

      ApplyResourceToToolStripItemCollection(strip.Items, res, lang); 
     } 

     foreach (Control c in control.Controls) // Apply to all sub-controls 
     { 
      ApplyResourceToControl(res, c, lang); 
      res.ApplyResources(c, c.Name, lang); 
     } 

     // Apply to self 
     res.ApplyResources(control, control.Name, lang); 
    } 

    private static void ApplyResourceToToolStripItemCollection(ToolStripItemCollection col, ComponentResourceManager res, CultureInfo lang) 
    { 
     for (int i = 0; i < col.Count; i++)  // Apply to all sub items 
     { 
      ToolStripItem item = (ToolStripMenuItem)col[i]; 

      if (item.GetType() == typeof(ToolStripMenuItem)) 
      { 
       ToolStripMenuItem menuitem = (ToolStripMenuItem)item; 
       ApplyResourceToToolStripItemCollection(menuitem.DropDownItems, res, lang); 
      } 

      res.ApplyResources(item, item.Name, lang); 
     } 
    } 
} 
+0

這很好!謝謝!運行時本地化改變的最佳解決方案,並且非常容易實現! –

+0

非常有幫助!謝謝!我已經嘗試了很多方法,只有你的作品謝謝! – Roman