views:

478

answers:

5

I am developing a standard small ASP.Net MVC website which will be made multilingual using ASP.Net resource files.

My question is about the resource files. See if you have a block of text which should be shown in paragraphes, is it appropriate to add <p> tags inside your resource file text?

If not, what would be the best way to deal with it?

A: 

We do.

You could use a text to html converter, but as text contains so much less info than html you'd be restricted.

Anthony Johnston
+1  A: 

I would say that this should be avoided, but sometimes it is practically unavoidable. Ideally it would be best to store your resources at a level of granularity that allows you to add the presentation markup in common for all languages in the appropriate area of the app.

Sometimes gramatical differences between languages make this difficult. Sometimes you want to store large chunks of text based resources (large enough that they would have to encompass things like p tags). It depends a little what you are doing.

UpTheCreek
+4  A: 

When I faced the same question I decided against having paragraphs directly in resources. Separate text block by simple new lines (Shift+Enter if I'm not mistaken). Then create a helper method to wrap blocks into paragraphs.

namespace MyHtmlExtensions
{
    public static class ResourceHelpers
    {
        public static string WrapTextBlockIntoParagraphs (this string s)
        {
            if (s == null) return string.Empty;

            var blocks = s.Split (new string[] { "/r/n", "/n" },
                                  StringSplitOptions.RemoveEmptyEntries);

            StringBuilder htmlParagraphs = new StringBuilder ();
            foreach (string block in blocks)
            {
                htmlParagraphs.Append ("<p>" + block + "</p>");
            }

            return htmlParagraphs.ToString ();
        }
    }
}

Then in your view you import your extension method namespace:

<%@ Import Namespace="MyHtmlExtensions" %>

And apply it:

<%= Resources.Texts.HelloWorld.WrapTextBlockIntoParagraphs () %>

I think it is a more cleaner way than putting markup tags into text resources where they in principle do not belong.

Developer Art
I like this solution. But, I wonder what kind of affect this has on performance?
UpTheCreek
I doubt it can become a bottleneck. Anyway I vote for clarity and maintainability over error-prone micro-optimizations.
Developer Art
It's not only about paragraphs, what about making a single word in a text bold. Would you write a special markup format just for resource files?
Peter
@Peter, that's outside the scope of your question. Your question was specifically about paragraphs!
Dan Atkinson
ok you're right, maybe I should pose a new one :p
Peter
+2  A: 

Yes and no. Resources are a good place to store localized text and basically this will allow you to send the whole resource to some translation agency who will do all translations for you. So, that's a plus. However, it also increases the chance of bugs simply because you might forget to encode or decode the HTML. The .resx file is an xml file so it needs to be stored encoded in the resource. (Fortunately, it will encode it automatically.) When you retrieve it again, it will be decoded automatically again. However, if you pass the text to other functions, there is a risk that it will be encoded again, resulting in <b>bold</b> instead of bold... So you will need some additional testing. Which is something you can manage.

But the big problem could be for the person who does the translations. If they just translate the .resx file, they'll see encoded strings. This could be solved if they use something to make the resources more readable. Just as long as they won't translate the tags themselves. They could confuse the persons who will be doing the translations.

Workshop Alex
A: 

Use URLEncoder to store the HTML string within the resource; then Decode the String and use webview rather than converting all the HTML tags.

" URLDecoder decodes the argument which is assumed to be encoded in the x-www-form-urlencoded MIME content type. '+' will be converted to space, '%' and two following hex digit characters are converted to the equivalent byte value. All other characters are passed through unmodified.

For example "A+B+C %24%25" -> "A B C $%". "

Blantant