You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
How shound be format for the ResourceKey field for use inside View (injecting IViewLocalizer) ? I try for example: Views.Home.Index .. not works
Or, in other words – Can use IViewLocalizer
The text was updated successfully, but these errors were encountered:
In configuration:
services.AddSqlLocalization(options => options.UseTypeFullNames = false);
That is, use false, so the class name is the same ResourcesKey in database (like HomeController or SharedResources)
Explanation:
In class SqlStringLocalizerFactory:
// classes go this way
public IStringLocalizer Create(Type resourceSource) {...}
...
// Views got this way
public IStringLocalizer Create(string baseName, string location) {...}
I think that is a bug in second approach, the resouces key includes the location, and the database not exists that entry , I do this workaround:
public IStringLocalizer Create(string baseName, string location)
{
// fix
var resourceSource = baseName.Replace(location, string.Empty).Substring(1);
if (_resourceLocalizations.Keys.Contains(resourceSource)) // goodbye: baseName + location
{
return _resourceLocalizations[resourceSource];
}
var sqlStringLocalizer = new SqlStringLocalizer(GetAllFromDatabaseForResource(resourceSource), resourceSource, false);
return _resourceLocalizations.GetOrAdd(resourceSource, sqlStringLocalizer);
}
Then, in my database i can use ResourcesKey like: Views.Home.Index
And works fine!
Perhaps Damien Bod should consider making this change in a future publication.
Hello! I am struggling the same issue with IViewLocalizer usage. The fix for correcting the resourceSource is not in the latest release. When will it be released? I cannot use IViewLocalizer in my Razor pages, unless I get the source code for SqlLocalizer and apply the above fix.
How shound be format for the ResourceKey field for use inside View (injecting IViewLocalizer) ? I try for example: Views.Home.Index .. not works
Or, in other words – Can use IViewLocalizer
The text was updated successfully, but these errors were encountered: