In this post I wondered about cleaner code when internationalising an app. that leads to this second query… supposing I wanted to call a function like this:
@Html.RenderWithTags("Help",
new Dictionary<string, string>() { "HelpPage", "@Html.ActionLink(...)" }
)
such that I look up a string in my local resource file containing embedded “tags” e.g. resource name “Help” contains:
We suggest you read our [HelpPage]
before proceeding
and then my .RenderWithTags() method will expand the tags but dynamically executing the code in the dictionary passed e.g. replace [HelpPage] with whatever @Html.ActionLink(...) produces.
I know I can use Microsoft.CSharp.CSharpCodeProvider().CreateCompiler() to compile C# code on the fly, but what about Razor code?
in the end, the solution turned out pretty slick. would not have been possible without SLaks, I’m much obliged for the help (though I didn’t end up using inline helpers (but thanks for the intro (they’re very cool))).
Now my page contains this:
and somewhere below I have:
To accomplish this simplicity:
…and:
…with a little extra help:
comments or feedback on how it could have been better most welcome!