如何检测 WinForms 中的可本地化属性?

发布于 2024-11-08 16:40:13 字数 1154 浏览 0 评论 0原文

在其他地方,有人指出“控件的“Visible”属性无法本地化。这句话只对了一半。但是,“Visible”属性的 LocalizedAttribute 设置为 true。但这仅仅意味着该属性被序列化到资源文件中。我编写了一个测试程序,将不变文化的标签可见属性设置为“false”。使表单可本地化,我将德国文化的可见属性更改为“true”。现在,如果我在系统设置为德语区域设置的情况下启动程序,会发生什么情况?标签保持不可见。 检查资源文件 Form1.de.resx 可以看到可见属性尚未序列化。但如果我手动将其添加到资源文件中:

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

就会出现标签。我承认有点困惑。 两个问题:

  • 如何使用内置序列化器检测属性是否“真正”可本地化?
  • 覆盖默认行为的推荐方法是什么?

编辑: 也许我需要澄清我的问题。 我的示例程序是一个简单的形式,其默认语言设置为不变。我已通过表单设计器手动添加了德语资源。程序在区域设置设置为德语的系统上运行。

案例 1

添加到表单的面板的“可见”属性:
1.) 在不变文化中设置为 false,在德国文化中设置为 true(默认)。 =>面板不可见
2.) 在不变文化中设置为 true,在德国文化中设置为 false。 =>面板不可见(按预期工作)
显然,如果该值不是默认值,则仅写入特定于语言的资源文件。

案例 2

添加到表单的标签的“字体”属性:
1.) 在固定文化中设置为粗体,在德国文化中属性重置为默认值。 =>标签不是粗体
2.) 在固定文化中设置为默认值,在德国文化中设置为粗体。 =>标签为粗体
现在这里的属性已按预期序列化。

这是一个错误还是我错过了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

挽手叙旧 2024-11-15 16:40:13

我们已收到微软的回复:

我发现这可能是个问题
对于这个本地化场景,但是在
一般情况,这是方法
大大减小 resx 的大小
文件,所以我们不想改变
这个实施。这个设计缺陷
不符合我们当前的标准,所以这个
不会在下一个版本中修复。

所以答案是:目前没有解决办法。

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.

野稚 2024-11-15 16:40:13

当我尝试时,效果很好。测试步骤:

  • 在表单上放置一个 TextBox
  • 将表单的 Localized 属性设置为 True
  • 将表单的 Language 设置为南非荷兰语
  • 将 TextBox 的 Visible 属性设置为 True
  • 像这样修改构造函数:

    公共 Form1() {
        System.Threading.Thread.CurrentThread.CurrentUICulture =
            System.Globalization.CultureInfo.GetCultureInfo("af");
        初始化组件();
    }
    

当我运行表单时,文本框不可见。我注释掉 CurrentUICulture 作业以切换回英语:文本框可见。

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.

南街女流氓 2024-11-15 16:40:13

我认为微软不会将此归类为错误。它按照设计工作;但这绝对是一个令人头痛的事情。

据我所知,没有一个简单的解决方案可以对 Label 控件进行子类化并添加属性 - 恶心。

作为解决方法,您可以简单地设置宽度或文本属性而不是可见性吗?

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?

风筝在阴天搁浅。 2024-11-15 16:40:13

我仍然相信 Visible 属性不可本地化。当我切换到默认语言然后返回到特定语言时,它总是重置为默认语言的设置,无论它是否是默认设置。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文