2012-06-19 5 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"의 값을 fileToolStripMenuItem1.Text에 지정하면

+1

가능한 중복 [어떻게 할 런타임에 WinForms 응용 프로그램의 culture 변경] (http://stackoverflow.com/questions/7556367/how-do-i-i-culture-of-a-winforms-applicati on-at-runtime) –

+0

다른 문제가 생겼습니다. menustrip의 subItem을 변경했지만, ApplyResources 함수에서 menuStrip을 반복하고 subItems를 건너 뜁니다! – Ehsan

답변

0

일부 테스트 winforms 앱을 작성하여 사용해 보았습니다. 컨트롤 Text 속성을 동적으로 잘 변경할 수 있습니다. 필요한 경우이 솔루션을 확장 할 수 있습니다.

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 
관련 문제