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?
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.
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.
I don't think Microsoft would classify this as a bug. It's working as designed; but it definitely is a pain in the neck.
There's not an easy solution that I'm aware of short of sub-classing the Label control and adding properties - yuck.
As a workaround, could you simply set the Width or Text properties instead of the Visibility?
I still believe that the Visible property is not localizable. It is always reset to its setting for the default language when I switch to the default laguage and then back to the specific language, does not matter whether it is a default setting or not.
精彩评论