locked
Winform UserControl Property Description Localization By ResourceManager In PropertyGrid RRS feed

  • Question

  • I create a windows usercontrol try to localization the property description in PropertyGrid at design time. I commit the source code here https://github.com/oudiorg/WinControlLocalization ,I create a usercontrol named UserControl1 has one property named TextLocalization and add SRDescription attribute  in the project WinControlLocalization.

    SRDescriptionAttribute inherid DescriptionAttribute and override Description. in class SR there is a ResourceManager to get the project Resources, the method GetString to get the resource by current culture. I create Resources and the orther Culture Resources Resources.en-US and Resources.zh-CN, in the Resources there is TextLocalization with different cluture value.

    When i drag the user control to Form1 and select the control to see the control property in PropertyGrid, it always return default resource value. I forced add culture info in SR and Debug.Write it's value but it return the default value(not culture specific).

        public sealed class SR
        {
            private static SR loader;
            private ResourceManager resources;
            public const string ControlTextLocalization = "TextLocalization";
    
    
            public SR()
            {
                resources = new ResourceManager("WinControlLocalization.Properties.Resources", typeof(SR).Assembly);
            }
    
            private static SR GetLoader()
            {
                if (SR.loader == null) SR.loader = new SR();
                return SR.loader;
            }
    
            public static ResourceManager Resources
            {
                get
                {
                    return SR.GetLoader().resources;
                }
            }
    
            public static string GetString(string name)
            {
                var culture=System.Globalization.CultureInfo.GetCultureInfo("zh-CN");
                System.Diagnostics.Debug.WriteLine(string.Format("CultureName {0} {1}={2}", culture.Name, name, SR.Resources.GetString(name, culture)));
                culture = System.Globalization.CultureInfo.GetCultureInfo("en-US");
                System.Diagnostics.Debug.WriteLine(string.Format("CultureName {0} {1}={2}", culture.Name, name, SR.Resources.GetString(name, culture)));
                return SR.Resources.GetString(name, System.Threading.Thread.CurrentThread.CurrentCulture);
            }
        }

    I download the ResX Resource Manager show the right value.


    Am i missing something?


    • Edited by oudiyue Thursday, August 6, 2020 2:05 AM
    Thursday, August 6, 2020 2:02 AM

All replies


  • Hi oudiyue,
    Based on your description, I have a suggestion you can refer to.
    You can try to follow the technique used by .NET itself, instead of creating a custom type descriptor.
    Microsoft created custom attributes derived from basic attributes that allow them to specify the name of the resource string and load it dynamically.

    public class SRDescriptionAttribute : DescriptionAttribute
    {
       public SRDescriptionAttribute ( string resourceName )
       {
    
       }
       public override string Description
       {
          get 
          {
           //  load string from resource
           }
        }
    }

    Best Regards,
    Daniel Zhang


    "Windows Forms General" forum will be migrating to a new home on Microsoft Q&A (Preview)!
    We invite you to post new questions in the "Windows Forms General" forum’s new home on Microsoft Q&A (Preview)!
    For more information, please refer to the sticky post.

    Thursday, August 6, 2020 9:21 AM
  • Hi Daniel Zhang

      Thanks for your reply, I did your suggestion in my code that you can review the source code from https://github.com/oudiorg/WinControlLocalization/blob/master/WinControlLocalization/SRDescriptionAttribute.cs

    namespace WinControlLocalization
    {
        [AttributeUsage(AttributeTargets.Property)]
        public class SRDescriptionAttribute : System.ComponentModel.DescriptionAttribute
        {
            private bool replaced = false;
    
            public SRDescriptionAttribute(string description) : base(description)
            {
    
            }
    
            public override string Description
            {
                get
                {
                    if (this.replaced == false)
                    {
                        this.replaced = true;
                        this.DescriptionValue = SR.GetString(base.Description);
                    }
                    return base.Description;
                }
            }
        }
    }

    The problem is that when i call SR.GetString(base.Description) and set CultureInfo to ResourceManager.GetString it did not return the correct culture specfic value. You can clone my project to see the issue.

    Friday, August 7, 2020 5:34 AM
  • Hi oudiyue, 
    I suggest you use the DefaultValue property assignment, and then initialize in the constructor.
    Here is a similar thread you can refer to.
    Best Regards,
    Daniel Zhang


    "Windows Forms General" forum will be migrating to a new home on Microsoft Q&A (Preview)!
    We invite you to post new questions in the "Windows Forms General" forum’s new home on Microsoft Q&A (Preview)!
    For more information, please refer to the sticky post.


    Monday, August 10, 2020 9:03 AM