How to embed links in localized text

Viewed 6424

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.

6 Answers

I know this is an old question but I figured this might help.

In ASP.net Core 2.2, you can have a resource file containing:

AllText: There are 2 links and one string. Links are {0} and {1}, the string is {2}
Link1Text: link1
Link2Text: link2
String1: something special

Then you have the view where you inject the localizer:

@using Microsoft.AspNetCore.Mvc.Localization

@inject IViewLocalizer Localizer

And finally in your HTML:

<p>@Localizer["AllText",
       @Html.ActionLink(@Localizer["Link1Text"].Value,"Action1","Controller1"),
       @Html.ActionLink(@Localizer["Link2Text"].Value,"Action2","Controller2"),
       @Localizer["String1"]]</p>

No need to encode or decode anything, just provide the parameters to the Localizer.

Related