none
Why (GetLocalizedString(string key)) in a overriding class, which base of CategoryAttribute, cannot be called more than once RRS feed

  • Question

  • Hi

    I fund a problem when I try to changing the properties' language of a form. In order to achieve the multiLanguage function, a customized class was coded as follows:

    //using LocalizedCategoryAttribute and LocalizedCategory to  support multilanguage
    [LocalizedProperty("BackColor")]
    [LocalizedCategory("Background")]
    public new virtual Color BackColor { get; set; } 
    
    [LocalizedProperty("BackgroundImage")]
    [LocalizedCategory("Background")]
    public new virtual Image BackgroundImage { get; set; } 
    
    
    .......
    
    // customized category class
    public class LocalizedCategoryAttribute : CategoryAttribute
    {
         public LocalizedCategoryAttribute(string res ourceKey):base(resourceKey)
         {
      
         }
            protected override string GetLocalizedString(string key)
            {
                CultureInfo ci = new CultureInfo(GlobalizedCategoryAttribute.GlobalSelectedLanguage);
                string value = MainFormLanguageResource.ResourceManager.GetString(key,ci);
                return value;
            }
     }
    
    // customized DisplayName class
     public class LocalizedPropertyAttribute : DisplayNameAttribute
        {
            private string ResourceKey;
            public LocalizedPropertyAttribute (string resourceKey)
            {
                ResourceKey = resourceKey;
            }
            public override string DisplayName
            {
                get
                {
                    string valu = MainFormLanguageResource.ResourceManager.GetString(ResourceKey);
                    Console.WriteLine("Local Struct: " + valu);
                    return valu;
                }
            }
        }

    But when I want to change the language form Englis to Chinese, only the propertyName can chang, the category name cant be change. 

    The overrided function of "GetLocalizedString(string key)" ony called once when generate a new project .  Actually, this function should be called immediately when the cultureInfo changed in order to changing the UI language.

    I realy need help.  I will very appriciate your suggestion. 

    Thanks very much

    Ming ( email: mingisworking@hotmail.com)



    Wednesday, September 11, 2019 9:05 AM

All replies

  • Hi

    This problem that the category of  some properties can not change according to UI cultureInfor changing still have not been resolve. The following is the test of the project that uploaded to the GitHub, some one can download and  test it, and try to find the resolution.

    https://github.com/gentleman-ming/GUIDesigner

    Thanks very much

    Ming ( email: mingisworking@hotmail.com)



    Wednesday, September 11, 2019 9:02 AM
  • In your Category part, you use new CultureInfo(GlobalizedCategoryAttribute.GlobalSelectedLanguage) instead of using the current UI Culture in the Property part, so the category name is always displayed in the same language as the GlobalizedCategoryAttribute.GlobalSelectedLanguage.

    The easiest fix would be just NOT passing the ci to ResourceManager.GetString(key) there.
    • Edited by cheong00 Wednesday, September 11, 2019 9:30 AM
    Wednesday, September 11, 2019 9:28 AM
  • Hi,

    What is "the category name" you mentioned? Can you elaborate on it? Besides, how did you set multiple languages? Via resx files? To handle it more efficiently, it would be better if you can provide a simple demo project?

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 12, 2019 8:50 AM
    Moderator
  • Hi

    "Category" name like as follow  shows "Bsic","Setting“,

     ^ Bsic

        Name

        Size

    ^ Setting

       BackColor

       BackgroundImage

    All property of a class are descriptes as:

            [LocalizedCategory("Background")]
            [LocalizedProperty("BackColor")]
            [LocalizedDescriptions("BackColor")]
            public new static  Color BackColor { get; set; }

    In my project,  resource file, like "test.en-US.resx", "text.zh-CN.resx" is used to surpport muliLanguage opration. 

    Can I have your Email? I will send my project to you through Email.

    Regards

    Ming


    Thursday, September 12, 2019 9:14 AM
  • Hi

    Actually, the function as follow showing will not called when thread cultrulInfo changed even using current UI cultrue. This function only initialized once when project producting.  The problem is how to call this function timely when current thread UI culture changed.

          protected override string GetLocalizedString(string key)
            {
    
                string value = MainFormLanguageResource.ResourceManager.GetString(key,Thread.CurrentThread.CurrentUICulture);
                return value;
            }

    I also tryed tried to not use current UI cultureInfo in this function, as following shows, but it did not work. In the MainForm(main UI class), the thread cultureInfo is changed timely. I don't know why this function not called more than once according to the changing of the cultureInfo . 

         protected override string GetLocalizedString(string key)
            {
    
                string value = MainFormLanguageResource.ResourceManager.GetString(key);
                return value;
            }

    regards

    Ming



    Monday, September 16, 2019 8:19 AM
  • Okay, I see what your problem is.

    Try call CurrentCulture.ClearCachedData() whenever your Culture setting changes and see if it helps.

    My current job don't use C# so I don't have environment to test before I go home.

    Monday, September 16, 2019 9:31 AM
  • Hi

    "CurrentCulture.ClearCachedData() " still can not work.

    This test project have been upload to the GitHub:

    https://github.com/gentleman-ming/GUIDesigner

    Thanks


    Tuesday, September 17, 2019 1:28 AM