Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I localize an asp-action using Localizer

I am building a webstie in multiple languages and would like the asp-action to be customised depending on the language. The idea is that the URL in the English version is something like /en/feedback whilst in German it should be /de/gaestestimmen.

I have tried the following:

<a asp-controller="Home" 
   asp-action="@Localizer["navKontakt"]" 
   asp-route-lang="@CultureInfo.CurrentCulture.TwoLetterISOLanguageName">
     @Localizer["navFeedback"]
</a>

The Localizer returns the correct text in the second instance, but not for the asp-action - there it returns:

"Microsoft.AspNetCore.Mvc.Localization.LocalizedHtmlString" instead of the expected string.

If I just enter asp-action="Feedback"it works fine.

Anyone any idea how I can make this work?

like image 621
rBalzer Avatar asked Sep 07 '25 15:09

rBalzer


2 Answers

Found the answer. As the localizer retuns a LocalizedHtmlString which is an object but not a string, I have to add the .Value to get value as a string.

asp-action="@Localizer["navKontakt"].Value"
like image 104
rBalzer Avatar answered Sep 10 '25 22:09

rBalzer


Looks like a quotes issue. Try switching out the double quotes for the attribute value with single quotes:

<a asp-controller="Home"
    asp-action='@Localizer["navKontakt"]'
    asp-route-lang="@CultureInfo.CurrentCulture.TwoLetterISOLanguageName">
    @Localizer["navFeedback"]
</a>
like image 40
Joel Waymack Avatar answered Sep 10 '25 23:09

Joel Waymack