Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect localizable properties in WinForms?

Somewhere else, someone states that the "Visible" Property of a control cannot be localized. This is half-true. However, the "Visible" Property has the LocalizableAttribute set to true. But this just means that the property is serialized to the resource file. I wrote a test program that has the visible property of a label set to "false" for the Invariant Culture. Making the form localizable I changed the visible property to "true" for the German culture. Now what happens if I start the program with the system set to German locale? The label stays invisible. Checking the resource file Form1.de.resx I can see that the visible property has not been serialized. But if I manually add this to the resource file:

<data name="label1.Visible" type="System.Boolean, mscorlib">
  <value>True</value>
</data>

the label appears. I confess to be slightly confused. Two questions:

  • How can I detect if a property is "truly" localizable using the build-in serializer?
  • What is the recommended way to override the default behaviour?

Edit: Maybe I need to clarify my question. My sample program is a simple form which has the default language set to invariant. I have manually added german resources through the forms designer. Program runs on a system with regional settings set to german.

Case 1

"Visible" Property of a panel added to the form:
1.) set to false in Invariant culture, true (default) in german culture. => panel is invisible
2.) set to true in Invariant culture, false in german culture. => panel is invisible (works as expected)
Apparently the value is only written to the language specific resource file if it's not the default.

Case 2

"Font" Property of a label added to the form:
1.) set to bold in Invariant culture, Property is reset to default in german culture. => label is not bold
2.) set to default in Invariant culture, bold in german culture. => label is bold
Now here the properties are serialized as expected.

Is it a bug or am I missing something?

like image 704
puls200 Avatar asked May 18 '11 13:05

puls200


2 Answers

We have received a response from Microsoft:

I can see that this can be a problem for this localization scenario, but in general case, this is the way to greatly reduce the size of a resx file, so we would not want to change this implementation. This design flaw does not meet our current bar, so this will not be fixed in the next release.

So the answer is: There is currently no solution.

like image 144
puls200 Avatar answered Oct 19 '22 09:10

puls200


This works just fine when I try it. Steps taken to test:

  • drop a TextBox on the form
  • set the form's Localizable property to True
  • set the form's Language to Afrikaans
  • set the TextBox' Visible property to True
  • modified the constructor like this:

    public Form1() {
        System.Threading.Thread.CurrentThread.CurrentUICulture =
            System.Globalization.CultureInfo.GetCultureInfo("af");
        InitializeComponent();
    }
    

The text box isn't visible when I run the form. I comment out the CurrentUICulture assignment to switch back to English: text box is visible.

like image 45
Hans Passant Avatar answered Oct 19 '22 07:10

Hans Passant