I have the following situation: assume I have to display the data in the following format.
I am 20 years old
. I need the number 20 to be in bold.
I'm fetching this string from a resource file like this
string.Format(HttpContext.GetGlobalResourceObject("ResourceFile","Key"),age);
Should I consider adding the tags <b>
and </b>
within the resource file? Is that considered a "best practice"? Could anyone provide useful links on localization as well?
For code/data/presentation separation purposes I suggest you don't store tags in your resource file. It will make it harder to maintain (by having tags in aspx/ascx files as well as in resources and maybe even in the DB)
You should follow separation of concerns pattern and keep things separated.
Key Value
"UserAge" "It seems you are {0} year(s) old."
Some loose restrictions may help
But when using some nested markup the most safe thing to do is having tags that don't provide any styling per se. In your case I'd use <span>
tag at most (because it's an inline style and that's exactly what you need). CSS would define it's visual representaion in the end.
Key Value
"UserAge" "It seems you are <span>{0}</span> year(s) old."
But you should understand the implications. Doing it this way may still be worse than having no tags at all. Imagine what happens when you change your presentation layer. Let's say to a Service or a Windows desktop app. tags like <span>
present no meaning in this context. They can be omitted, but why would you put them in in the first place then.
If you want to have certain portions bolded or decorated in some other way, you could store them as Markdown strings in your resource file and then apply Markdown to them when rendering the page. In fact, this very site uses the markdown library to great success doing exactly this. That way you wouldn't have to worry about storing HTML in your resource files, and your strings would still be readable if you ever have to use them outside of HTML.
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