I have Razor Views with localizated resources using IViewLocalizer like this:
@inject Microsoft.AspNetCore.Mvc.Localization.IViewLocalizer Localizer
...
@Localizer.GetString("Click here to Log in")
I'm trying to extract all those strings from my precompiled views (to build a PO file dictionary), so I think I'll have to find all references of IViewLocalizer injected into my views, something like this:
var findAll = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.Static;
var asm = Assembly.LoadFile("MySite.Web.Views.dll"));
var view = asm.GetType("AspNetCore.Views_Home_Index");
var props = view.GetProperties(findAll).Where(p=>p.PropertyType.FullName == "Microsoft.AspNetCore.Mvc.Localization.IViewLocalizer").ToList();
// I want to find all references that use this this IViewLocalizer Localizer.get()
var getter = props.First().GetGetMethod().MetadataToken;
// How can I do it?
var allMethods = view.GetMethods(findAll);
foreach(var method in allMethods)
{
// How can I check my methods for usages of that property getter, and in case get the strings?
}
I tried using the code from this answer which uses MethodBase.GetMethodBody().GetILAsByteArray() but it didn't work - looks like the method public override Task ExecuteAsync()
is very short, so it looks like my view is rendered somewhere else.
And additionally, I'm not sure if I should look for the MetadataToken of the getter (IViewLocalizer injected into my views) or if I should look for usages of any concrete method which implements IViewLocalizer.GetString(string)
and IViewLocalizer.GetString(string, params object[])
.
PS: I'm aware that I can use Regex over my cshtml views, as long as the injected localizer follows some naming standard. That's Plan B in case I can't figure out the reflection solution.
Aucun commentaire:
Enregistrer un commentaire