2011-08-08 69 views
5

He creado el programa Windows Form en C#. Tengo algunos problemas con la localización. Tengo archivos de recursos en 3 idiomas. Quiero hacer clic en cada botón de idioma y cambiar el idioma en tiempo de ejecución. Cuando cambio de idioma antes de InitializeComponent(), funciona. Pero cuando hago clic en el botón, no funciona. estoy usando este código.Localización en tiempo de ejecución

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

¿Has probado también a cambiar 'Thread.CurrentCulture' también? – abatishchev

+0

¿Ha intentado llamar a 'Refresh()' después de establecer el cultivo de hebras a otro idioma? –

+0

Actualizar() no ayuda. –

Respuesta

4

usted tendrá que volver a cargar los controles para que reflejen los valores de la nueva cultura

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

y luego se tendría que aplicar para cada control usando resources.ApplyResources

favor tienen una

1

Cambio de la corrienteUICulture no volverá a cargar automáticamente los recursos. Debe realizarlo manualmente (http://msdn.microsoft.com/en-us/magazine/cc163609.aspx#S8)

Puede copiar el código relacionado con la localización de InitializeComponent() en otra función :

void LoadResources(){ 

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

} 
-1

Gracias V4Vendetta look here y otros .. solución es ...

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

Aquí es una solución recursiva: void changeLanguage privada (CTL de control, cadena lang) { recursos ComponentResourceManager = new ComponentResourceManager (typeof (Form1)) ; resources.ApplyResources (ctl, ctl.Name, new CultureInfo (lang)); foreach (Control c en ctl.Controls) ChangeLanguage (c, lang); } – TaW

11

Escribí una clase RuntimeLocalizer con las siguientes características:

  • cambios y actualizaciones de localización para todos Control s y SubControl s en una forma
  • también cambia la localización de todos los SubItem s de todos MenuStrip s

Ejemplo de uso: 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

¡Eso funciona genial! ¡Gracias! ¡La mejor solución para la localización de tiempo de ejecución cambia y es muy fácil de implementar! –

+0

¡Muy útil!¡Gracias! ¡He intentado muchos acercamientos y solo tus trabajos gracias! – Roman

Cuestiones relacionadas