Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you format localized strings to have single words of a sentence be a link?

I am working on a globalized web-app in ASP.NET MVC3. The project contains the I18N resources files and I normally access the resources inside my Razor views like...

@I18N.MyResourceString

I have a tricky situation which I have not been able to figure out a elegant solution to yet. I need to be able to localized the sentence "Click here to donate." where the word 'here' should be a link to our donation system.

Most links in the site are internal so to create link I simply write...

@Html.ActionLink("Some link text", "MyAction", "MyController")

This donation link is external. What I have so far (which is not working) is...

@String.Format(I18N.ClickHereToDonate, "<a href=\"http://paypal.com\">" + I18N.Here + "</a>")

where the I18N.ClickHereToDonate resource's text is "Click {0} to donate.".

What I see on the screen is...

Click <a href="http://paypal.com">here</a> to donate.

Furthermore, I would also like to add a 'title' attribute to the 'a' tag. It gets even uglier when I try that...

@String.Format(I18N.ClickHereToDonate, "<a href=\"http://paypal.com\" title=\"" + I18N.PayPal + "\">" + I18N.Here + "</a>")

There has to be a better way to form complex strings with embedded tags without concatenating things together in such a hackish manner. Not only does it not work (the intended markup got encoded) but it makes the HTML inside a string literal in my razor template which makes me loose any awesome IDE support/intergation/refactoring capabilities.

How can markup be injected into localized strings?

UPDATE
Adam Tuliper mentioned the @Html.Raw helper method in his answer so I added it to my already ugly markup...

@Html.Raw(String.Format(I18N.ClickHereToDonate, "<a href=\"http://paypal.com\" title=\"" + I18N.PayPal + "\">" + I18N.Here + "</a>"))

This at least got me a click-able link in the outputted markup.

Click here to donate.

It is still a far-less-than-elegant solution though so I am still looking for better ways of doing this.

like image 309
Jesse Webb Avatar asked Oct 23 '22 14:10

Jesse Webb


2 Answers

Maybe try

<a href="http://paypal.com" title="@I18N.PayPal">@I18N.ClickHereToDonate</a>

Overall, you don't need the String format - you can just inject the Razor things inside normal html elements.

Edit: Incorporating the below:

@Html.Raw(String.Format(@I18N.ClickHereToDonate,String.Format("<a href='http://paypal.com' title='{0}'>{1}</a>", I18N.PayPal,I18N.Here)))
like image 192
Chris Carew Avatar answered Oct 31 '22 10:10

Chris Carew


Your choices are limited without built in support for this scenario (and there isn't in the helpers) The cleaner way is to form your urls in a viewmodel and pass that view model to the view so you have minimal html. Your ViewModel contains public class WhateverIndexViewModel { public string Key {get;set;} public string URI {get;set;} public string Title {get;set;} }

Set the info in your controller, pass it to your view and use


<a href="@Html.Raw(Links["YourKey"].URI)">@Links["YourKey"].Title"</a>

As a basic idea. Note if you dont want to use Html.Raw here then your URI in the class would be of type MvcString not String this way @Links["YourKey"].URI won't be html encoded.

like image 44
Adam Tuliper Avatar answered Oct 31 '22 09:10

Adam Tuliper