2011-09-26 16 views
10

Tôi đã tạo Chương trình Biểu mẫu Windows trong C#. Tôi có một số vấn đề với bản địa hóa. Tôi có các tệp tài nguyên bằng 2 ngôn ngữ (một là dành cho tiếng Anh và một ngôn ngữ khác là tiếng Pháp). Tôi muốn nhấp vào mỗi nút ngôn ngữ và thay đổi ngôn ngữ khi chạy.Làm cách nào để thay đổi văn hóa của ứng dụng WinForms khi chạy

Nhưng khi tôi nhấp vào nút, nó không hoạt động. tôi đang sử dụng mã này.

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)); 
    } 
} 

bất kỳ pls sẽ giúp về vấn đề này ......

Cảm ơn nhiều ....

Trả lời

18

Điều này đã làm việc:

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); 
    } 
} 

Hãy cẩn thận để tránh thêm còi như thế này mà không ai sẽ sử dụng.

+0

xin lỗi Tôi đã thử điều này, nhưng nó không hoạt động cho tôi .. –

+0

tôi có cần thêm bất kỳ tệp tài nguyên nào để tạo, tôi đã thay đổi thuộc tính bản địa hóa thành true và thay đổi ngôn ngữ tiếng Anh thành bỉ không nhưng không hiển thị ngôn ngữ tôi đã chọn ... và tôi thấy bất kỳ tệp tài nguyên bổ sung nào được thêm vào biểu mẫu ... –

+1

Bạn thậm chí không bắt đầu trên đó và bạn muốn biết cách chuyển đổi? Không biết "không hiển thị ngôn ngữ mà tôi đã chọn" có nghĩa là gì. Bạn sẽ cần chỉnh sửa thuộc tính. Sau khi thay đổi thuộc tính Ngôn ngữ, hãy đặt thuộc tính Văn bản của biểu mẫu chẳng hạn. Điều đó sẽ tự động tạo tệp Form1.fr-BE.resx. Mở nút bên cạnh biểu mẫu để xem. –

5

Bạn có thể phải gọi ApplyResources đệ quy trên điều khiển:

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

Tôi đã thử code của bạn nhưng nó không làm việc .... –

0

Cập nhật CultureInfo lúc chạy có thể đặt lại kích thước thành phần. Mã này bảo kích thước và vị trí của các nút điều khiển (vẫn sẽ nhấp nháy có thể nhìn thấy mặc dù, trong đó sử dụng SuspendLayout() không thể sửa chữa)


    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; 
    } 
Các vấn đề liên quan