views:

240

answers:

3

I have the following situation assume i have to display the data in the follwing format.

I am 20 years old . i need the number 20 to be in bold.

I'm fetching this string from resoucre file like this

string.Format(HttpContext.GetGlobalResourceObject("ResourceFile","Key"),age);

should i consider adding the tags <b> and </b>

in the resoucre file , is that the best practice.?

could anyone provide useful links on localization.?

thanks,

vijay

A: 

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.

Alex Marshall
Could u suggest a small sample.?
vijaysylvester
+2  A: 

Don't store tags that change visual style in resources

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.

Robert Koritnik
Example please :)
vijaysylvester
Example of what?
Robert Koritnik
kinda a solution for the above instance. ?
vijaysylvester
Would this satisfy your requirements?
Robert Koritnik
Even i thought of that . but imagine the text is in different language , then we dont know where to have the number and text . so how do we apply styles for number alone.? that is the catch here.
vijaysylvester
A: 

In general, you want to keep your viewing logic independent of your resources, and this is no exception.

I would break the string into 3 components: the substring before the bold part, the bold part itself, and the substring after the bold part. Put the first and third items into resources, and format the second on your page as appropriate.

Jon Seigel
That is a bad idea . in some other language the number might come in the front and text might follow it .
vijaysylvester
That's exactly why it gets split. In another language, your strings before/after the number are different. In some languages, either of the strings could be empty.
Jon Seigel
That will be extremely difficult to translate by external translators. Its much easier to have a complete sentence with placeholders in it.
Malcolm Frexner
@Mathias: Agreed, but I wouldn't say it's "extremely difficult." Translating an application with no sense of context is rarely going to work out well anyways.
Jon Seigel