2011-09-26 62 views
10

我已经在C#中创建了Windows窗体程序。我在本地化方面遇到一些问题。我有两种语言的资源文件(一个是英文,另一个是法文)。我想单击每个语言按钮并在运行时更改语言。如何在运行时更改WinForms应用程序的文化

但是,当我点击按钮,它不起作用。我正在使用此代码。

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

您甚至没有开始使用它,并且想要知道如何切换?不知道什么“不显示我选择的语言”可能意味着什么。你需要编辑属性。更改语言属性后,设置表单的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; 
    } 
相关问题