I have a TextBlock in a WPF application where I'd like to show the user a message similar to:
Retry or go back.
I can accomplish this by doing:
<TextBlock>
<Hyperlink Command="{Binding RetryCommand}">
<Run Text="Retry" />
</Hyperlink>
<Run Text=" or " />
<Hyperlink Command="{Binding GoBackCommand}">
<Run Text="go back." />
</Hyperlink>
</TextBlock>
However, this solution is not very friendly to localization; it is particularly problematic in the case where sentences require rearranging in the process of localization. I would much prefer to be able to specify something like "[Retry]({0}) or [go back.]({1})." in a resx file and dynamically insert the hyperlinks into the localized string. Is there a clean and simple way to go about doing this?
In another application, we used a surprisingly complex solution involving extending the TextBlock class and dynamically constructing the Inlines property, but it seemed like a lot of code for what should be a common problem.
Related questions: Is adding strings with placeholders (`{0}`) into resources a good idea?, How to embed links in localized text
So I guess one solution would be a custom control. However, I was able to use a IMultiValueConverter to get the job done.
public class TextBlockFormatToHyperlinkConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (values.Length >= 3)
{
var tblk = values[0] as TextBlock;
var format = values[1] as string;
var tokens = Tokenizer.ParseTokens(format);
int hyperLinkIndex = 0;
for (int i = 2; i < values.Length; i++)
{
var token = tokens.FirstOrDefault((p) => string.Equals(p.Value as string, "{" + hyperLinkIndex + "}"));
if (token != null)
{
token.Value = values[i];
}
hyperLinkIndex++;
}
tblk.Inlines.Clear();
foreach (var token in tokens)
{
if (token.Value is Hyperlink)
tblk.Inlines.Add((Hyperlink)token.Value);
else
tblk.Inlines.Add(new Run(token.Value as string));
}
return tblk;
}
return null;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
private class Token
{
public object Value { get; set; }
public Token(object value)
{
Value = value;
}
}
private class HyperlinkToken : Token
{
public HyperlinkToken(object value) : base(value) { }
}
private static class Tokenizer
{
public static List<Token> ParseTokens(string format)
{
var tokens = new List<Token>();
var strings = Regex.Split(format, @"({\d+})");
foreach (var str in strings)
{
if (Regex.IsMatch(str, @"({\d+})"))
{
tokens.Add(new HyperlinkToken(str));
}
else
{
tokens.Add(new Token(str));
}
}
return tokens;
}
}
}
And then in the XAML I used it like this:
<TextBlock x:Name="tblk_LogFileLink" TextWrapping="Wrap">
<TextBlock.Tag>
<MultiBinding Converter="{StaticResource TextBlockFormatToHyperlinkConverter}">
<Binding x:Name="textblock" ElementName="tblk_LogFileLink"/>
<Binding x:Name="formatString" Source="Click to open the {0}."/>
<Binding x:Name="firstHyperlink">
<Binding.Source>
<Hyperlink NavigateUri="{Binding LogPath}">
<Run Text="{x:Static localization:Resources.LBL_LOG_FILE}"/>
</Hyperlink>
</Binding.Source>
</Binding>
</MultiBinding>
</TextBlock.Tag>
</TextBlock>
You could add several hyperlinks to the MultiBinding if your string format required more.
This allowed me to localize the entire string while still providing the hyperlink text.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With