dotnet / AspNetCore.Docs

Documentation for ASP.NET Core
https://docs.microsoft.com/aspnet/core
Creative Commons Attribution 4.0 International
12.64k stars 25.28k forks source link

Additional Resources guidance on Localization and Generics no longer valid #26138

Open SpudManTwo opened 2 years ago

SpudManTwo commented 2 years ago

[EDIT by guardrex to remove the metadata given that this won't be a Blazor docs issues. I'll place the metadata for the main doc set glob/loc topic.]

First time submitting an issue here, so apologies ahead of time if I mess something up with how to submit this issue but here it goes.

I'm not entirely sure whether this issue is more of a documentation or an issue with AspNetCore 6.0 itself, but the currently attached Additional Resource on "Localization and Generics" is no longer valid or functional. The currently existing way to add String Localization using "IStringLocalizer" in .NET 6 is with a builder.Services.AddLocalization() call. This was previously being fixed by a workaround mentioned in this issue here https://github.com/dotnet/aspnetcore/issues/3422 . However, as .NET has matured, this solution is no longer up to date as it is referencing things from 2018 and that code isn't valid anymore. Attempting to follow the linked additional resource only results in more errors and absolutely no guidance on how to solve said errors. Currently, the only working solution I have found to allow a Generic Templated Component using an IStringLocalizer (something that looks like this in a code behind) public partial class BlazorTemplatedComponent<T> : ComponentBase {} Is to create a second stand-in component that doesn't do or contain anything and serves no purpose outside of being a registerable component that can have ".resx" file that the original "BlazorTemplatedComponent" can then resolve. @inject IStringLocalizer<BlazorTemplatedComponent<T>> stringLocalizer does not work as, (mentioned in the issue I mentioned above), the resource file can't be located since the system will be looking for a resource file with an esoteric and unconventional name.

I'm unsure if there is a more modern and proper solution to this problem currently and the documentation is just lagging behind or if this is a legitimate issue with the AddLocalization() code itself.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

guardrex commented 2 years ago

Hello @SpudManTwo ... This won't be the right spot (topic anyway) for such an issue ... probably ... it looks broader than Blazor. I see the exchange with @hishamco on https://github.com/dotnet/aspnetcore/issues/3422 ... and then https://github.com/dotnet/AspNetCore.Docs/issues/8244 was opened and led to https://github.com/dotnet/AspNetCore.Docs/pull/12718. There was also https://github.com/dotnet/AspNetCore.Docs/pull/8245#issuecomment-415530098 ...

I think we need to file a new issue for resource naming for localized generic classes, or perhaps modify the source instead of using weird names

Also, a mini-section linking to https://github.com/dotnet/aspnetcore/issues/3422 was placed in the main doc set's glob/loc overview on https://github.com/dotnet/AspNetCore.Docs/pull/8245, but it looks like that was removed at some point, so the link to the GH issue 3422 no longer appears in any version of the live topic.

@mkArtakMSFT ... Can you assist?

guardrex commented 2 years ago

Note: I will keep an 👁️ on this because it's true that we have an Additional Resources section link in the Blazor topic for Localization & Generics to ...

http://hishambinateya.com/localization-and-generics

... so if it's decided to remove/change that link, I'll knock it out as an 'addresses' PR against this issue. If this only pertains to Blazor, I'll revert the metadata back to the Blazor glob/loc topic.

SpudManTwo commented 2 years ago

Not sure what exchange you are referring to that I specifically had on https://github.com/dotnet/aspnetcore/issues/3422 . I would note that I was not the one who opened that issue originally but, rather, just someone who just stumbled upon this while working on something for my day job while trying to get a templated component to localize and then found the documentation to be somewhere between confusing and unhelpful specifically towards Templated Components in Blazor.

guardrex commented 2 years ago

Oh, sorry ... yes ... that's correct. I was more looking at the comments than the players.

I will need help from the PU for this subject if it pertains to Blazor. I'm not a glob/loc pro, just a doc jockey. They'll need to respond and let me/us know how to proceed here.

SpudManTwo commented 2 years ago

No worries, just wanted to clear that up before aforementioned glob/loc pro steps in and gets confused. I wasn't entirely expecting you to know the answer as, like I said, I'm not sure if there is a better way that got implemented and it just never made it to documentation or so on. I figured I'd start out by leaving an issue on the documentation in case there was just a link or something that I was missing somewhere (not to to mention that the documentation seems like it should be updated regarding that link anyways). Thanks again for taking a look into this

hishamco commented 1 year ago

@SpudManTwo could you please refresh my mind what's no longer valid?

mkArtakMSFT commented 9 months ago

@MackinnonBuck I wonder if this is an area you can build expertise on and try to help when you get chance. I think it will be quite late (closer to Preview7 / RC1 timeframe) but I think that's still fine.

SpudManTwo commented 9 months ago

I was not expecting to get pinged on this assumedly dead thread but I can chime in my input again from the original issue as the one who opened/reported it.

The issue, as reported within ASP.NET 6 at the time, was regarding how to properly handle Blazor's Generic Template Components as mixed with Localization. The issue at heart was that when the Localizer Service was attempting to look up the .resx resource file, it was doing so with an erroneous lookup name (see the workaround I mentioned in my original post). This issue was made worse by the fact that the .NET documentation was linking to code from 2018 that actually would create more errors than it would solve. As requested upon time of open, I was asking that either the documentation was wrong and there was a newer solution to the problem that I wasn't aware of and that needed added or that this was currently an existing issue with .NET that should see a resolution. It's currently quite late for me local time but I can try to dig up my code from roughly 2 years ago and see if I can still replicate the issue in the .NET 7 and 8 as I have since moved onto those two versions for my daily work. Please let me know if that's necessary or if the information provided at the beginning of the thread as well as re-described here is sufficient.

@hishamco My apologies for never responding last year, I honestly didn't see it until now. As just mentioned above, it's been a long while since I looked at this specific issue in particular. If deemed necessary, I can attempt to replicate the exact issues. That said, this is a very old bug as the original version that this appeared in and required workarounds for was in .NET Core 2.X and we're now on .NET 8. I do remember there being issues though, not just from version differences, but from the fact that I was using Blazor and your solution was using Razor Pages. All that said, I can try to dedicate some hours either tomorrow or the following day if you truly require it.

hishamco commented 9 months ago

No problem, please try to reproduce the issue again, otherwise we will close this