I would like to display localized Texts/Error Messages in .Net Core Mvc Identity Server4 application. I tried to do the small PoC by following .NetCore Globalization Guide my Microsoft and it works as expected. I can display correct localized texts per requirements.
However, we've got an issue, when we applied it in the actual implementation. We've got in-house built in framework for WebApplication and our .NetCore application is inheriting/implementing the base class & interfaces. So, the built in .Net Core localizer are always trying to get the view translations by using the parent Namespace.
For example.
Current behaviour (Wrong)
application name = ttcg.MyCustomFramework.CustomNameSpace
ResourceBaseName = ttcg.MyCustomFramework.CustomNameSpace.Resources.Views.Account.Login
Expected bahaviour
application name = ttcg.IdentityServer
ResourceBaseName = ttcg.IdentityServer.Resources.Views.Account.Login
Please see the screenshot below:
I wrote AddViewLocaliztion
per following in the StartUp class. But, I don't know how to set the ResourceBaseName in the setup.
services.AddMvcCore().AddViewLocalization(
LanguageViewLocationExpanderFormat.Suffix,
options =>
{
options.ResourcesPath = "Resources";
}
);
I tried to give the ResourceFile name with full namespace in the path (Eg. "ttcg.MyCustomFramework.CustomNameSpace.Resources.Views.Account.Login.en-GB.resx"), but it throws the path too long (over 260 characters) error.
Since I don't know how to overwrite it, I created the custom localizer service to inject in the views and it works. However, the disadvantage is that, I've got a single Huge SharedResource file with multiple texts.
public class CustomLocalizerService
{
private readonly IStringLocalizer _localizer;
public CustomLocalizerService(IStringLocalizerFactory factory)
{
var type = typeof(SharedResource);
var assemblyName = new AssemblyName(type.GetTypeInfo().Assembly.FullName);
_localizer = factory.Create("SharedResource", assemblyName.Name);
}
public LocalizedString GetLocalizedHtmlString(string key)
{
return _localizer[key];
}
public LocalizedString GetLocalizedHtmlString(string key, string parameter)
{
return _localizer[key, parameter];
}
}
So, it would be great if there is a way to make default .Net Core Localization provider work. Is there anyway to provide or force .Net Core default localizer to use the provided namespace in ResourceBaseName?
You can provide the namespace with
IStringLocalizerFactory
:If you still