RSS

Inlines and Run in TextBlock does not follow Language override

25 Sep

If you want to localize your application you generally override the LanguageProperty with your desired language:

FrameworkElement.LanguageProperty.OverrideMetadata(
    typeof(FrameworkElement), 
    new FrameworkPropertyMetadata(XmlLanguage.GetLanguage("de-DE")));

As stated in the documentation every control should now use the – in this case – German culture. Well this is not the case. If you have a TextBlock and set the Text property then this works, but if you have a TextBlock with Inline elements, then the en-us culture is used.
This is very stange. Why is that the case? TextBlock inherits from FrameworkElement and because you have overridden the FrameworkElement.LanguageProperty this works. But if you use Inlines, such as Run, Italic, Underline, … those controls inherits from FrameworkContentElement and not from FrameworkElement for which you do not have overridden the corresponding DependencyProperty. This is the only reason why Inlines do not follow the application wide culture. As solution for that you have to override the LanguageProperty for FrameworkContentElements.

FrameworkContentElement.LanguageProperty.OverrideMetadata(
    typeof(FrameworkElement),
    new FrameworkPropertyMetadata(XmlLanguage.GetLanguage("de-DE")))

This does not work because it says that is is already registered. So a workaround is to override if for every class that is one inheritance hierarchy lower than FrameworkContentElement:

 FrameworkContentElement.LanguageProperty.OverrideMetadata(
    typeof(DefinitionBase),
    new FrameworkPropertyMetadata(XmlLanguage.GetLanguage("de-DE")));

            FrameworkContentElement.LanguageProperty.OverrideMetadata(
    typeof(FixedDocument),
    new FrameworkPropertyMetadata(XmlLanguage.GetLanguage("de-DE")));

            FrameworkContentElement.LanguageProperty.OverrideMetadata(
    typeof(FixedDocumentSequence),
    new FrameworkPropertyMetadata(XmlLanguage.GetLanguage("de-DE")));

            FrameworkContentElement.LanguageProperty.OverrideMetadata(
    typeof(FlowDocument),
    new FrameworkPropertyMetadata(XmlLanguage.GetLanguage("de-DE")));

            FrameworkContentElement.LanguageProperty.OverrideMetadata(
    typeof(TableColumn),
    new FrameworkPropertyMetadata(XmlLanguage.GetLanguage("de-DE")));

            FrameworkContentElement.LanguageProperty.OverrideMetadata(
    typeof(TextElement),
    new FrameworkPropertyMetadata(XmlLanguage.GetLanguage("de-DE")));

Now the Inlines in TextBlock follows the culture that you have specified

Advertisements
 
Leave a comment

Posted by on September 25, 2014 in C-Sharp

 

Tags:

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: