2012-06-19 57 views
3

我已经阅读了关于如何在.Net中创建多语言程序的教程,它运行良好,但在这里我需要一个让运行时的所有内容更容易的想法。 在用户点击语言时的运行时。我改变文化,以例如选择适当的语言:在运行时更改语言的应用程序

Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en");  

,然后调用它设置文本为我的布局形式的函数:

private System.Resources.ResourceManager rm; 
fileToolStripMenuItem1.Text = rm.GetString("fileToolStripMenuItem1.Text"); 
settingsToolStripMenuItem.Text = rm.GetString("settingsToolStripMenuItem.Text"); 

,因为它似乎在查表当我为我的程序的每个组件设置文本时,它已经由.Net构建,与应该设置的属性相同。换句话说,“fileToolStripMenuItem1.Text”传递给GetString()函数,并且结果应该设置为fileToolStripMenuItem1.Text,所以我不知道我该怎么做,甚至无法使用哪个工具来迭代在rm的每个属性上,然后通过反射或其他方法将密钥的值分配给密钥。也就是说,假设“fileToolStripMenuItem1.Text”是查找表中的关键字,值是“A”,那么我该如何做到这一点:将“fileToolStripMenuItem1.Text”的值为“A”赋值给fileToolStripMenuItem1.Text

+1

可能重复[我怎样在运行时更改WinForms应用程序的文化](http://stackoverflow.com/questions/7556367/how-do-i-change-the-culture-of-a-winforms-applicati运行时) –

+0

我遇到了另一个问题,我改变了menutrip的子项,但是在你的函数ApplyResources中,它在menuStrip上迭代并跳过子项! – Ehsan

回答

0

我写了一些测试winforms应用程序,并尝试它,并可以更改控制文本属性动态地罚款。如果需要,您可以扩展此解决方案。

using System; 
using System.Collections; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Globalization; 
using System.Linq; 
using System.Reflection; 
using System.Resources; 
using System.Text; 
using System.Threading; 
using System.Windows.Forms; 

namespace ConsoleApplication5 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

    //main logic of switching language of UI 
    void ChangeCulture_Handler(CultureInfo culture) 
    { 
     //getting relative path of resource file for specific culture 
     var resourcePath = GetLocalizedResourceFile(culture); 
     //initialize new reader of resource file 
     var reader = new ResXResourceReader(resourcePath); 
     //getting enumerator 
     var resourceEnumerator = reader.GetEnumerator(); 
     //enumerate each record in resource file 
     while (resourceEnumerator.MoveNext()) 
     { 
      string resKey = Convert.ToString(resourceEnumerator.Key); 
      //we can add here some check if need 
      //(for example if in resource file exists not only controls resources with format <Control Name>.<Property> 
      //if(resKey.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries).Length == 2) 
      string resValue = Convert.ToString(resourceEnumerator.Value); 
      //actually update property 
      UpdateControl(resKey, resValue); 
     } 
    } 

    //main logic of updating property of one control 
    private void UpdateControl(string resKey, string resValue) 
    { 
     //we suppose that format of keys in resource file is <Control Name>.<Property> 
     var strs = resKey.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries); 
     var controlName = strs[0]; 
     var controlProp = strs[1]; 

     //find control of form by its name 
     var controls = this.Controls.Find(controlName, true); 
     if (controls.Length > 0) 
     { 
      //select first control 
      var control = controls[0]; 
      //getting type of it 
      var t = control.GetType(); 
      //getting property 
      var props = t.GetProperty(controlProp); 
      if (props != null) 
      { 
       //setting localized value to property 
       props.SetValue(control, resValue, null); 
      } 
     } 
    } 

    //build resource file path 
    string GetLocalizedResourceFile(CultureInfo ci) 
    { 
     string cultureCode = ci.TwoLetterISOLanguageName; 
     //for english language is default, so we don't have a need to add "en" part in path 
     return cultureCode != "en" ? string.Format("Resource1.{0}.resx", cultureCode) : "Resource1.resx"; 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     Thread.CurrentThread.CurrentCulture = new CultureInfo("es-MX"); 
     ChangeCulture_Handler(Thread.CurrentThread.CurrentCulture); 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); 
     ChangeCulture_Handler(Thread.CurrentThread.CurrentCulture); 
    } 
} 

}

资源英语(Resource1.resx)

button1.Text Change language to es 
button2.Text Change language to en 
label1.Text   label1 
label2.Text   label2 

资源西班牙语(Resource1.es.resx)的

button1.Text cambiar el idioma to es 
button2.Text cambiar el idioma to en 
label1.Text  lalble1 
label2.Text  lalble2