dotnet-api-docs icon indicating copy to clipboard operation
dotnet-api-docs copied to clipboard

Examples pleeeeeease?

Open timmi4sa opened this issue 7 years ago • 3 comments

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.

timmi4sa avatar Nov 14 '18 16:11 timmi4sa

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!

tonyawad88 avatar Dec 31 '18 04:12 tonyawad88

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)

timmi4sa avatar Dec 31 '18 05:12 timmi4sa

@tonyawad88 and @timmi4sa : After close to 7 years, you probably moved on or found the solution, but if anybody has the same issue, what you should put in ErrorMessageResourceType is the type of the class containing the error message, that is typeof(MyController) in your first example.
It should be associated with ErrorMessageResourceName such as ErrorMessageResourceName = nameof(MyController.MyPropertyRequired).

Full example:

[Required(AllowEmptyStrings = false, ErrorMessageResourceName = nameof(MyController.MyPropertyRequired), ErrorMessageResourceType = typeof(MyController))]
public string MyProperty { get; set; }

Arkane5 avatar Sep 04 '25 14:09 Arkane5