stefann
stefann

Reputation: 1075

ASP.NET: localize content with mixed HTML formatting

In my application I have paragraphs with mixed static text and HTML formatting and links. I'm looking for a good localization solution that keeps resources decoupled from markup. Let's say we have the following paragraph:

<p>Let's have a <a href="someURL">cup of coffee</a> and get <b>energized</b>.</p>

Using the standard resx solution forces me to embed the HTML markup and the link destinations in the resx string. This is bad because it couples markup/CSS/app structure with resources.

The next best thing is to split the paragraph such that localized content never contains markup. In the above example I would have 4 para fragments: 1) "Let's have a" as plain text 2) "cup of coffee" as a link 3) "and get" as plain text 4) "energized" as bold text

The problem with this solution is that fragmentation makes maintenace of resources a complete nightmare plus it forces a certain order of the paragraph fragments which might not fit the grammar of all cultures. For instance, in the translated language the proper translation might be

<p>Let's get <b>energized</b> with a <a href="someURL">cup of coffee</a>.</p>

I don't think I can quite get away with not embedding markup into resources and that might not be a huge deal. Using proper markup/CSS (span, div, id, class) I can create abstractions that would lessen the impact of coupling.

What do I do about the link URLs though? Thanks,

Stefan

Upvotes: 16

Views: 6062

Answers (6)

MatthiasS
MatthiasS

Reputation: 483

I was having the same problem. If I'm reading your question correctly this is what I was able to do to solve it:

In my resources, I added the following text:

<p>Let's get <b>energized</b> with a <a href="{0}">cup of coffee</a>.</p>

And in my ASP.NET view (using a Razor view), I added the following:

@Html.Raw(String.Format(ShopSavvy.Retailers.Web.Resources.Home.Index.perksReachShoppersBody1, "someURL"))

Doing this gave me all the word-specific formatting I needed for the proper effect.

Upvotes: 2

Julian
Julian

Reputation: 1002

Do it like this:

<%= String.Format("Resources.MyResource.mydata","someURL") %>

and use this as your resource string:

<p>Let's have a <a href="{0}">cup of coffee</a> and get <b>energized</b>.</p> 

Upvotes: 1

Ian Boyd
Ian Boyd

Reputation: 256631

The default Visual Studio sample web-site contains:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" 
       AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>Welcome to ASP.NET!</h2>
    <p>To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET
          Website">www.asp.net</a>.
    </p>
    <p>
        You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&amp;clcid=0x409"
            title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
    </p>
</asp:Content>

How would you localize these:

  1. <h2>Welcome to ASP.NET!</h2>

  2. <p>To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.</p>

  3. <p>You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&amp;clcid=0x409" title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.</p>

Upvotes: -1

pp6
pp6

Reputation:

I came across this googling the exact same problem. It is intentional, since if you send the resources file to a translator he/she might not understand HTML and could damage your code. I don't like this either.

Upvotes: 0

Scott Schulthess
Scott Schulthess

Reputation: 2923

I'm on Rahuls side - I would consider the html/css of content...just that...content.

The main reason is because when you want to do bulk content updates to a website, you will just have to copy paste from provided html/css into the resx files. It's also a lot quicker to do the orginal templating from html/css into asp.net.

There are also some refractor tools that have "export to resx file" options when content is selected that may speed things up.

Thanks!

Upvotes: 0

Rahul
Rahul

Reputation: 12231

Don't view the HTML as "formatting", but as structure, and save all of it to a resource data store (such as resx, or a database, or xml files or something). Then you can stop worrying about little bits of text inside a paragraph. Instead, you'll have some reference to a resource called "paragraph_energized_with_coffee" or something, per locale, and whatever software you use to edit the resources will determine what flexibility editors have in structuring the html inside each resource.

Upvotes: 3

Related Questions