How to get localized string from ASP.NET Core Controller Work Around


Question: #IStringLocalizer not working, when I try to get a String Localization from the Asp.Net Core #Controller it does not work. However, am able to Globalization String from the Views utilizing Resource Files.



Login to See the Rest of the Answer

Answer: If you can't get the String #Localization from the Asp.Net #Core Controller, this might be a long way to troubleshooting instead here is the work around:

1. Return the String from the Controller, based on the String returned then create a .RESX file or a Resource file with language specified. This way you could rely on the IVeiwLocalizer instead of IStringLocalizer which is treaky to have it working.


2. This means all the String returned from the Controller would have their conterpart translation Strings. Take a look at the ViewBag Massage returned from the Controller:

 ViewBag.Notification = new Notifications { SuccessMessage = null, ErrorMessage = null, NotificationType = "Information", NotificationHeader = "NOTE", NotificationMessage = "PLEASE ADD ITEMS TO THE CART." };

In the View where the Notification is shown, handle the Localization as below:

 @Html.Raw(Localizer[ViewBag.Notification].Value)

Keep in mind that the hierarchy should be followed when creating a Resouce.resx file. The naming conversion has to be the same as the File that is consuming the Notification.







© 2024 - ErnesTech - Privacy
E-Commerce Return Policy