Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed links in localized text

I am internationalizing an ASP.NET MVC application, but I'm unsure how to handle linked text. Take the following as an example:

  • English: "Please login to continue."
  • Português: "Entre por favor para continuar."

Since "login" is hyperlinked, I must have the translator mark which corresponding word or phrase should be hyperlinked when the text is localized, e.g. Entre.

What is the best strategy-solution?

Is there a standard way to mark special text (and a standard way of using the translated results) ... or have I gone down the wrong path in expecting my content and presentation information to be so tightly coupled (although I can't think of any way to remove the coupling in this case).

Current Implementation:

I am currently using local resource files for Views and an extension method on HtmlHelper to get the localized sting:

<%= Html.Resource("LoginMessage")%>

Update: Please see Keith's answer.

I found it most helpful, but the system auto selected another one.

like image 555
Robert Claypool Avatar asked Nov 17 '09 04:11

Robert Claypool


People also ask

How do you embed a link in text?

Press Ctrl+K. You can also right-click the text or picture and click Link on the shortcut menu. In the Insert Hyperlink box, type or paste your link in the Address box. Note: If you don't see the Address box, make sure Existing File or Web Page is selected under Link to.

What do you call to an external links embedded inside a text?

Hyperlink is embedded into a text or an image and takes visitors to another part of a web page.

What is text within the link?

(hyperLINK TEXT) Also called "anchor text," it is a word or phrase on a Web page that the user clicks on to jump to another page on the same site or to a page on an external site.


2 Answers

I think this comes down to 4 choices:

  1. Put a link in your localisation: "Please <a href="#">login</a> to continue"
  2. Multiple localisations - either:
    1. "Please {0} to continue" and "login", or
    2. "Please", "login" and " to continue"
  3. Markup inside your localisations, for instance:
    1. "Please {0}login{1} to continue"
    2. "Please {start-login}login{end-login} to continue"
    3. "Please <a href="{0}">login</a> to continue"
  4. Just don't support it - make the whole sentence a link

I think there is a major reason to avoid 1 - you mix up localisations and application navigation.

Option 2 ends up with multiple resources for each block of text, but as long as you have good ways of managing all your localisations it shouldn't be an issue. It can be a pain for 3rd-party translators though - you need some way to tell them the context or you get some very weird translations of the individual words.

Option 3 is my preferred solution. You still create issues for your translators though - most will not understand your tokens/HTML/markup in the text. Ours already do some HTML, so 3.3 has worked for us.

Option 4 might be worth considering - do you gain enough in having the link embedded in order to make it worth the extra work and maintenance? It's an important question specific to your application: if the whole sentence is the link (rather than just the active verb - which is link best practice) do you really lose enough to make option 2 or 3 worth the additional effort?

I think this might be why there aren't more standardised ways of doing this as for most projects (maybe 9 times out of 10) option 4 is sufficient, so this only ends up as a problem for some special cases. We have a complex application with around 11,000 pieces of localised text and go for 4 the vast majority of the time, and have only 4 or 5 places where we've had to go with 3.3

Our technical implementation is similar to yours:

<%= Html.Localise("Controller/Action/KeyOfTextOnPage") %>

For links we have a specific helper:

<%= Html.LocaliseLink("Controller/Action/KeyOfTextOnPage", "~/link.aspx") %>
<%= Html.LocaliseAction("Controller/Action/KeyOfTextOnPage", "action", "controller") %>
like image 187
Keith Avatar answered Oct 11 '22 00:10

Keith


What I'm currently using is he following setup:

I have a global resource file containing my primary texts, named Strings.resx (Strings.NL-nl.resx etc). Next to this I have my global file containing all localizations of my actionlinks. That is: ActionLinks.resx and locals.

Now what I do is, in my Strings.resx I have something like:

Strings.resx

Please {0} to continue

Local language Strings.NL-nl.resx

{0} om verder te gaan

Now the trick is to do something like:

<%= Html.Encode(string.Format(Resources.Strings.ControllernameViewnameKey, 
Html.ActionLink(Resources.ActionLinks.login, "Account", "LogOn")))

If you need more than one variable in your link you can give an array of objects to

 string.Format()

The keynaming is my own convention to understand where the maintext is used (what page). Since my maintexts are very specific. Of course keys can be made yourself.

For more information on how I do it you can have a look at my answer on my own question: here


Edit

Of course you can use local files instead of global files but I like my strongly typed resources too much for that :).

like image 37
bastijn Avatar answered Oct 10 '22 23:10

bastijn