views:

100

answers:

2

I am working on globalizing/localizing an asp.net application with C# as the backend. We are in the process of extracting strings to a resource file, and have run into a problem. We are trying to keep sentences together so that they are translatable, but this is not possible with links. For Example:

<%= Strings.BeginningOfSentence %>
<asp:HyperLink id="exampleLink" runat="server"><%= Strings.MiddleOfSentence %></asp:HyperLink>
<%= Strings.EndOfSentence %>

Strings is the resource file. If this were normal html for the link, I could use String.Format and keep the sentence together, adding in the html as two parameters, but that breaks it here. Any ideas in how to make this work?

+4  A: 

You don't have to use a HyperLink control for this do you? If you need a dynamic link then you can store the anchor tag in a parameterised string and add the necessary attribute values using string.Format, like you suggested. Something like this:

Code:

myLiteral.Text = string.Format("{0} <a href=\"{1}\">{2}</a> {3}", Strings.BeginningOfSentence, myUrl, Strings.MiddleOfSentence, Strings.EndOfSentence);

ASPX:

<asp:Literal id="myLiteral" runat="server" />
Charlie
of course! the code-behind!
antimatteraustin
+1  A: 

I have found that parameterized strings greatly simplifies translations mixed with dynamic content. For instance, you can have placeholders in the translated string into which link-html can be inserted. This may rule out the use of server-side hyperlink controls though. Example strings:

English:

"The <a href=\"http://images.google.se/images?q=house&amp;tab=wi\"&gt;house&lt;/a&gt; in which we lived"

Swedish:

"<a href=\"http://images.google.se/images?q=hus&amp;tab=wi\"&gt;huset&lt;/a&gt; som vi bodde i"

Note how the link has moved in the sentence in relation to the link (there is no text before the link in the Swedish version).

If you do not want to include the markup in the translation, I guess that may be used as a parameterized template in itself:

string googleSearchTemplate = "<a href=\"http://images.google.se/images?q={0}&amp;tab=wi\"&gt;{1}&lt;/a&gt;"

Then you can parse translated pieces into the link html, and then insert that piece into the final string:

string.Format("The {0} in which we lived", string.Format(googleSearchTemplate, "house", "house"));

Then you just need to insert the resulting string into the page.

Fredrik Mörk