Changes the value of a user control's Description Attribute depending on the language used.

jacky Perpète 81 Reputation points
2024-11-12T16:05:10.1633333+00:00

I created a user control with a description attribute for these properties.

When I test the user control, these descriptions appear correctly in the property grid.

mYhOck04q7

c3hamrEBas

The description attribute is in French.

I would like to be able to change it dynamically depending on a language choice, for example in English.

How can we change this?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,054 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,743 questions
0 comments No comments
{count} votes

Accepted answer
  1. KOZ6.0 6,590 Reputation points
    2024-11-12T17:36:17.15+00:00

    The string to be displayed are changed by referring to CultureInfo.CurrentUICulture.Name.

    using System.ComponentModel;
    using System.Globalization;
    
    class SRDescriptionAttribute : DescriptionAttribute
    {
        public string EnString { get; }
        public string JpString { get; }
    
        public SRDescriptionAttribute(string enString, string jpString) {
            EnString = enString;
            JpString = jpString;
        }
    
        public override string Description {
            get {
                switch (CultureInfo.CurrentUICulture.Name) {
                    case "en-US":
                        return EnString;
                    case "ja-JP":
                        return JpString;
                }
                return EnString;
            }
        }
    }
    

    It is used like this.

    class TestUserControl : UserControl
    {
        [SRDescription("English", "日本語")]
        public int Prop1 { get; set; }
    }
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. jacky Perpète 81 Reputation points
    2024-11-13T06:02:23.86+00:00

    Many thanks to KOZ6.0 for his answer.

    This solved my problem.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.