Add carriage return to string resource in WPF
Asked Answered
L

3

15

My applications store all localized text in a string resource dictionary as suggested here http://msdn.microsoft.com/en-us/library/bb295251(VS.85).aspx

        <ResourceDictionary 
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:system="clr-namespace:System;assembly=mscorlib">

  <!-- String resource that can be localized -->
  <system:String x:Key="localizedMessage">en-US Message</system:String>

</ResourceDictionary>

My question is, how can I add a new line to a string resource and have it render correctly when it's used in a TextBlock.

Doing this inline works:

<TextBlock Name="test" Text="Line 1&#13;Line 2"> </TextBlock>

However, using &#13; in a string resource does not work. What's the best way to add a new line to a string resource?

Labonte answered 20/10, 2010 at 0:50 Comment(0)
H
20

UPDATE: updated answer - better option

The XAML parser normalized whitespace according to the following rules.

http://msdn.microsoft.com/en-us/library/cc189036(VS.95).aspx#whitespace

To instruct your sys:String to preserve whitespace, apply xml:space="preserved to it:

<sys:String x:Key="MyLocalizedString" xml:space="preserve">this&#13;&#10;is&#13;&#10;my&#13;&#10;resource</sys:String>
Hunt answered 20/10, 2010 at 1:9 Comment(4)
The localized string option does work, but the binding expression is a bit ugly.Labonte
Yep. But that's what you're stuck with - it would be nice if you could provide an explicit or implicit conversion operator on the LocalizedString type and have the parser pick it up, but it doesn't. I can show a much more complicated option involving an attached property, but I'm not sure that's any better in the long run.Hunt
Updated the answer above with the attached property option.Hunt
Note: updated answer to be more XAML specific (with actual non workaround solution)Hunt
H
14

When you are entering a string resource in the resource view you can add CR/LF by hitting Shift + Enter. It will add the newline and you'll be able to see it. Retrieving the resource string and setting it as text on the text block will then yield the desired effect of reproducing the newline (or multiple newlines). In my case I wanted to simulate two paragraphs in a single text block. I didn't like the other approaches because it required the translation into another language to deal with two strings. I wanted to treat this as a single string resource so that the translator had the full context of what they were translating.

Helmand answered 13/4, 2012 at 14:54 Comment(0)
N
0

So, it's obviously been a while but for those in my situation perhaps this will help. In my case, I had a stand-alone XML resource file where I was already using ...

var strVal = (string)Application.Current.FindResource(key);
return strVal;

... within my own helper class to load the resource string. So for me the simplest solution was to just insert a call to Replace as follows:

var strVal = (string)Application.Current.FindResource(key);
strVal = strVal.Replace(@"\n", Environment.NewLine);
return strVal;

My example from my StringResources.xaml allows me to use the escape character \n as follows....

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:system="clr-namespace:System;assembly=mscorlib">

    <system:String x:Key="resKeyValue1">The source field "{0}" is already mapped to the destination field "{1}".\n\n
    Do you want to additionally map the source field "{0}" to the destination field "{2}"?</system:String>

    ...
</ResourceDictionary>
Neither answered 26/2, 2015 at 1:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.