IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Forms Discussion :

[C# 2.0] Changer la langue de l'interface graphique durant l'exécution


Sujet :

Windows Forms

  1. #1
    Membre régulier
    Inscrit en
    Mars 2002
    Messages
    118
    Détails du profil
    Informations forums :
    Inscription : Mars 2002
    Messages : 118
    Points : 70
    Points
    70
    Par défaut [C# 2.0] Changer la langue de l'interface graphique durant l'exécution
    Bonjour,

    Dans mon applications localisable et avec deux langues (FR et EN), je peux très bien voir l'application en Francais ou en Anglais si je sélectionne le langue dans l'IDE de Visual Studio ou si je change mes préférences régionales de Windows.

    Cependant, je voudrais permettre à mon utilisateur de modifier la langue d'affichage en direct... Sans avoir à redémarrer l'application ou quoique ce soit.

    J'utilise la ligne suivante, mais sans succès.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr");
    Est-ce qu'il y a un moyen de forcer .NET a m'afficher le tout dans la langue que je veux durant l'exécution ?

    Merci de votre aide !

    Martin

  2. #2
    Membre régulier
    Inscrit en
    Mars 2002
    Messages
    118
    Détails du profil
    Informations forums :
    Inscription : Mars 2002
    Messages : 118
    Points : 70
    Points
    70
    Par défaut
    J'ai trouvé ! Voici le code, il suffit d'appeler :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    ApplyCulture(new CultureInfo("fr"));
    Pour que l'interface se change en Français... Yeah !

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    private void ApplyCulture(CultureInfo culture)
    {
        // Applies culture to current Thread.
        Thread.CurrentThread.CurrentUICulture = culture;
     
        // Create a resource manager for this Form and determine its fields via reflection.
        ComponentResourceManager resources = new ComponentResourceManager(this.GetType());
        FieldInfo[] fieldInfos = this.GetType().GetFields(BindingFlags.Instance |
            BindingFlags.DeclaredOnly | BindingFlags.NonPublic);
     
        // Call SuspendLayout for Form and all fields derived from Control, so assignment of 
        //   localized text doesn't change layout immediately.
        this.SuspendLayout();
        for (int index = 0; index < fieldInfos.Length; index++)
        {    
            if (fieldInfos[index].FieldType.IsSubclassOf(typeof(Control)))
            {
                fieldInfos[index].FieldType.InvokeMember("SuspendLayout", 
                    BindingFlags.InvokeMethod, null, 
                    fieldInfos[index].GetValue(this), null);
            }
        }
     
        // If available, assign localized text to Form and fields with Text property.
        String text = resources.GetString("$this.Text");
        if (text != null)
            this.Text = text;
        for (int index = 0; index < fieldInfos.Length; index++)
        {
            if (fieldInfos[index].FieldType.GetProperty("Text", typeof(String)) != null)
            {
                text = resources.GetString(fieldInfos[index].Name + ".Text");
                if (text != null)
                {   
                    fieldInfos[index].FieldType.InvokeMember("Text",
                        BindingFlags.SetProperty, null,
                        fieldInfos[index].GetValue(this), new object[] { text });
                }
            }
        }
     
        // Call ResumeLayout for Form and all fields derived from Control to resume layout logic.
        // Call PerformLayout, so layout changes due to assignment of localized text are performed.
        for (int index = 0; index < fieldInfos.Length; index++)
        {
            if (fieldInfos[index].FieldType.IsSubclassOf(typeof(Control)))
            {    
                fieldInfos[index].FieldType.InvokeMember("ResumeLayout",
                        BindingFlags.InvokeMethod, null,
                        fieldInfos[index].GetValue(this), new object[] { false });
            }
        }
        this.ResumeLayout(false);
        this.PerformLayout();
    }

  3. #3
    Membre expérimenté Avatar de Mose
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    1 143
    Détails du profil
    Informations personnelles :
    Âge : 47
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 1 143
    Points : 1 379
    Points
    1 379
    Par défaut
    Juste une question bête et méchante :
    Est-ce-que la mémoire utilisée par ton application augmente de façon conséquente quand tu changes de langue ?
    Et si tu change 18 fois de langues, ça te mange beaucoup plus de mémoire, ou c'est bien géré ?

    Merci de satisfaire ma curiosité, et en espérant ne pas pointer un problème du doigt

  4. #4
    Membre régulier
    Inscrit en
    Mars 2002
    Messages
    118
    Détails du profil
    Informations forums :
    Inscription : Mars 2002
    Messages : 118
    Points : 70
    Points
    70
    Par défaut
    Effectivement, la mémoire utilisé augmente de 200-300ko. Puis plus je change, plus ca monte...

    Qu'est-ce qui t'a fait deviner ca ? Comment régler le tout ? Tu as une idée ?

  5. #5
    Membre expérimenté Avatar de Mose
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    1 143
    Détails du profil
    Informations personnelles :
    Âge : 47
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 1 143
    Points : 1 379
    Points
    1 379
    Par défaut
    Bein en fait, le mécanisme de globalization de .Net, grosso-modo, il consiste à créer une DLL par langue de ton appli.
    Au chargement, ton appli va consulter la culture courante et charger la DLL appropriée.
    Donc quand tu changes de langue, il va charger une nouvelle DLL.
    Etc...

    Je ne suis pas un grand spécialiste de la question, attendons de voir si qqn peut te conseiller mieux avant que je propose des solutions barbares

    Sinon, juste pour info : tu peux remplacer
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    this.ResumeLayout(false);
    this.PerformLayout();
    par :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    this.ResumeLayout(true);

  6. #6
    Membre régulier
    Profil pro
    Inscrit en
    Août 2003
    Messages
    59
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2003
    Messages : 59
    Points : 80
    Points
    80
    Par défaut
    Citation Envoyé par margagn
    J'ai trouvé ! Voici le code, il suffit d'appeler :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    ApplyCulture(new CultureInfo("fr"));
    Attention, ce code de ApplyCulture ne gère que la propriété Text. Or, d'autres propriétés sont souvent utilisées dans la localisation, comme Size ou Location.

    Il faut donc compléter ce code.
    Par ailleurs, je l'ai un peu modifier pour pour l'appeler sur n'importe quelle Form. Voici le code modifié (qui prend en compte également les propriétés Size et Location des contrôles) :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
     
            public static void ApplyCulture(Form form, CultureInfo culture)
            {
                // Create a resource manager for this Form and determine its fields via reflection.
                ComponentResourceManager resources = new ComponentResourceManager(form.GetType());
                FieldInfo[] fieldInfos = form.GetType().GetFields(BindingFlags.Instance |
                    BindingFlags.DeclaredOnly | BindingFlags.NonPublic);
     
                // Call SuspendLayout for Form and all fields derived from Control, so assignment of 
                //   localized text doesn't change layout immediately.
                form.SuspendLayout();
                for (int index = 0; index < fieldInfos.Length; index++)
                {
                    if (fieldInfos[index].FieldType.IsSubclassOf(typeof(Control)))
                    {
                        fieldInfos[index].FieldType.InvokeMember("SuspendLayout",
                            BindingFlags.InvokeMethod, null,
                            fieldInfos[index].GetValue(form), null);
                    }
                }
     
                // If available, assign localized text to Form and fields with Text property.
                // If available, assign localized Localtion and Size to fields
                System.Drawing.Point point;
                System.Drawing.Size size;
                String text = resources.GetString("$this.Text");
                if (text != null)
                    form.Text = text;
                for (int index = 0; index < fieldInfos.Length; index++)
                {
                    if (fieldInfos[index].FieldType.GetProperty("Text", typeof(String)) != null)
                    {
                        text = resources.GetString(fieldInfos[index].Name + ".Text");
                        if (text != null)
                        {
                            fieldInfos[index].FieldType.InvokeMember("Text",
                                BindingFlags.SetProperty, null,
                                fieldInfos[index].GetValue(form), new object[] { text });
                        }
                    }
                    if (fieldInfos[index].FieldType.GetProperty("Location", typeof(System.Drawing.Point)) != null)
                    {
                        point = (System.Drawing.Point)resources.GetObject(fieldInfos[index].Name + ".Location");
                        if (point != null)
                        {
                            fieldInfos[index].FieldType.InvokeMember("Location",
                                BindingFlags.SetProperty, null,
                                fieldInfos[index].GetValue(form), new object[] { point });
                        }
                    }
                    if (fieldInfos[index].FieldType.GetProperty("Size", typeof(System.Drawing.Size)) != null)
                    {
                        size = (System.Drawing.Size)resources.GetObject(fieldInfos[index].Name + ".Size");
                        if (size != null)
                        {
                            fieldInfos[index].FieldType.InvokeMember("Size",
                                BindingFlags.SetProperty, null,
                                fieldInfos[index].GetValue(form), new object[] { size });
                        }
                    }
                }
     
                // Call ResumeLayout for Form and all fields derived from Control to resume layout logic.
                // Call PerformLayout, so layout changes due to assignment of localized text are performed.
                for (int index = 0; index < fieldInfos.Length; index++)
                {
                    if (fieldInfos[index].FieldType.IsSubclassOf(typeof(Control)))
                    {
                        fieldInfos[index].FieldType.InvokeMember("ResumeLayout",
                                BindingFlags.InvokeMethod, null,
                                fieldInfos[index].GetValue(form), new object[] { true });
                    }
                }
                form.ResumeLayout(false);
                form.PerformLayout();
            }
    @ +

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Changer de langue sur une interface en mode direct
    Par machipot dans le forum NetBeans
    Réponses: 2
    Dernier message: 14/09/2012, 20h01
  2. Changer la langue de l'interface graphique durant l'exécution
    Par georgesvennis dans le forum Windows Forms
    Réponses: 0
    Dernier message: 04/05/2012, 16h57
  3. Réponses: 2
    Dernier message: 01/06/2007, 17h24
  4. [Debutant(e)] changer la langue dans eclipse
    Par omega dans le forum Eclipse Java
    Réponses: 6
    Dernier message: 16/03/2004, 16h47
  5. fonction MessageDlg Delphi :changer la langue
    Par julie20 dans le forum Composants VCL
    Réponses: 3
    Dernier message: 06/06/2003, 17h44

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo