dotnet / dotnet-api-docs

.NET API reference documentation (.NET 5+, .NET Core, .NET Framework)
https://docs.microsoft.com/dotnet/api/
Other
739 stars 1.57k forks source link

Examples pleeeeeease? #1097

Open timmi4sa opened 6 years ago

timmi4sa commented 6 years ago

Please throw us a bone: give us some code snippets. This is as fluid as water in a raging river, to the point of being discouraging!


Document Details

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

tonyawad88 commented 5 years ago

Hi @timmi4sa , did you figure this out? I am looking for ways on how to use the "ErrorMessageResourceType" in dotnet core to reference the type of a controller who's resx files are under: projectroot/Resources/Controllers/MyController.resx projectroot/Resources/Controllers/MyController.fr.resx

[StringLength(255)]
[Required(AllowEmptyStrings = false, ErrorMessageResourceName ="MyPropertyRequired", ErrorMessageResourceType = typeof(IStringLocalizer<MyController>????) )]
public string MyProperty { get; set; }

I have it working with the shared resource files under: projectroot/Resources/SharedResource.resx projectroot/Resources/SharedResource.fr.resx

and using the following in startup.cs:

services.AddMvc()
.AddDataAnnotationsLocalization(options => {
                        options.DataAnnotationLocalizerProvider = (type, factory) =>
                            factory.Create(typeof(SharedResource));
                    });
[StringLength(255)]
        [Required(AllowEmptyStrings = false, ErrorMessage ="MyPropertyRequired" )]
        public string MyProperty { get; set; }

But it would be neat to be able to have those translations picked up from their respective controller rather than from the shared.

Thoughts? Any help is greatly appreciated. Thank you!

timmi4sa commented 5 years ago

No, a complete dead-end! The annoying thing is the "ErrorMessage" property being repurposed, which not only sounds wrong but also breaks libraries (such as "Foolproof" validation library, I have stumbled upon this while porting the library from .NET 4.0 framework for my personal needs).

I am thinking of switching to Python.. (kidding)