I used to have something like this:
We suggest you read our @Html.ActionLink("help page", "Help", "Home") before
proceeding.
nice and clean. then I decided we needed to internationalise the app. I couldn't figure out a better way to deal with the above than to store the following string in the resource file:
We suggest you read our [HelpPage] before proceeding.
and then on the view I have to do:
@MvcHtmlString.Create(this.Resource("Help").ToString()
.Replace("[HelpPage]",
@Html.ActionLink("help page", "Help", "Home").ToString()
)
)
What other strategies can you use to internationalize using Razor?
this.Resource()
is a page extension that calls .GetLocalResourceObject()
and returns an MvcHtmlString
so here's what I ended up writing:
where the tag replacements gets done like this:
so in my code I can now call it like this:
and elsewhere:
any suggestions for improvement most welcome
You should create a separate code-behind method that replaces any
[placeholder]
s with actual links, then call that helper in Razor views.This will give you a single place to change the code that fills in the links.
I was having the same problem. Instead of using placeholders, I use the same formatting in my resource strings as if I were using
String.Format()
. An example of using this; my resource stringsand my view (razor):
I think it's a bit cleaner