mrpmorris.bsky.social's Avatar

mrpmorris.bsky.social

@mrpmorris

12
Followers
17
Following
20
Posts
03.07.2023
Joined
Posts Following

Latest posts by mrpmorris.bsky.social @mrpmorris

I hope you don't mind, I just added this as a comment to your blog along with a link to my solution.

12.03.2026 10:28 👍 0 🔁 0 💬 0 📌 0

So it is bad news for those people who use validation summaries, and a lot of labour (and possible mistakes) in setting a string on every attribute.

My library gives the best of both. 1 line of manual work, no mistakes, and supports field names + any attribute property placeholders (max length).

12.03.2026 10:14 👍 0 🔁 0 💬 1 📌 0

It's also not a feasible approach when the format string has values in it, such as

"{0} has a maximum length of {1}."

You'd either have to just output "String is too long" or have a unique string for each unique string length in your app...

12.03.2026 10:14 👍 0 🔁 0 💬 1 📌 0

The default error messages come from a pre-defined format string that has a placeholder for the member name.

"The {0} field is required".

This is why in your example you have to set the ErrorMessage on every validator - but that then becomes unusable when you use a validation summary...

12.03.2026 10:14 👍 0 🔁 0 💬 1 📌 0

Well, if you do want to use ValidatorAttribute and have localization. Now it's easy :)

12.03.2026 10:07 👍 0 🔁 0 💬 0 📌 0

In that case, your scenario can obviously use FluentValidation to localize and not need to worry about Swagger schemas not being updated :)

12.03.2026 10:06 👍 1 🔁 0 💬 0 📌 0

Ah I see, so you have a shared Contracts project used by both client and server that had data objects and FV validators in - and then you use FV to validate on the client before talking to the API and on the server to validate the request?

11.03.2026 16:42 👍 0 🔁 0 💬 1 📌 0

I did exactly that, but it was pointless because injecting TranslationService and grabbing TS.Person_FamilyName gave the exact same output as TypeGeneratedFromResxFile.Person_Family_Name

And it didn't work with ValidatorAttribute et al.

So there was just no need for it.

11.03.2026 16:40 👍 1 🔁 0 💬 1 📌 0

How did you remove the need to get FV restrictions? Did you stop including meta information in the API schema related to validation?

11.03.2026 16:37 👍 0 🔁 0 💬 1 📌 0

So you don't use ValidationAttribute at all?

Do you generate Swagger API endpoints for your app? If so, how do you get the FV restrictions to show in the API schema?

11.03.2026 13:22 👍 0 🔁 0 💬 1 📌 0

I removed that as soon as I spotted the fact that using the code-generated static properties still localized to the current user's culture.

Why MS recommend IStringLocalizer<T>, I just don't know.

11.03.2026 13:21 👍 0 🔁 0 💬 2 📌 0

I wrote an extension to allow LocalizeErrors.GetText(() => Errors.Required) with an Analyzer to check it was on the same type.

I didn't like that so I wrote Roslyn code to automatically create a class for each resx along with properties, and it used StringLocalizer to resolve the text, but...

11.03.2026 13:21 👍 0 🔁 0 💬 1 📌 0

The problem is you can pass anything in as the string key, so even with nameof() you could use the wrong resource

LocalizeApp[nameof(Resources.Errors.Required)]

You can't add an extension x => x.Required because expressions can't point to static members...

11.03.2026 13:21 👍 0 🔁 0 💬 1 📌 0

Yes, and that's just a few lines of code in Program.cs too.

You can still use magic strings for keys using MyRes.ResourceManager

I suspect IStringLocalizer<T> was created just so MS could offer blanket advice "don't use statics" for their "pit of success" advice.

11.03.2026 10:35 👍 0 🔁 0 💬 2 📌 0
Preview
GitHub - mrpmorris/AutoLocalize: Automatically localize DataAnnotations validation attributes in .NET Automatically localize DataAnnotations validation attributes in .NET - mrpmorris/AutoLocalize

This looks like a nice clean solution to a wart on .NET's localization functionality - using Fody to set a default ErrorMessageResourceType and ErrorMessageResourceName (based on the attribute type).

github.com/mrpmorris/Au...

Seems crazy that this is necessary though - am I missing something?!

10.03.2026 22:43 👍 5 🔁 2 💬 3 📌 0

I removed IStringLocalizer and just used the static members on the generated classes for resx files. In our Blazor Server app it uses the correct language. No idea how, but it does.

11.03.2026 10:26 👍 0 🔁 0 💬 1 📌 0
Preview
Roslynject/Source/Lib/Morris.Roslynject/Extensions/AttributeDataGetArgumentsExtension.cs at 1532d760be598e237bb6cc181a6772bd9750b56c · mrpmorris/Roslynject Roslyn based dependency discovery. Contribute to mrpmorris/Roslynject development by creating an account on GitHub.

@meziantou.net Hi
I wonder if you can help with a Roslyn unit test problem?

Branch = rewrite

Unit test = github.com/mrpmorris/Ro...

When debugging, attributeData.AttributeClass is always `ErrorTypeSymbol`
github.com/mrpmorris/Ro...

But it actually generates code perfectly.

Any ideas?

22.12.2024 23:32 👍 2 🔁 0 💬 1 📌 0
The periodic table of elements game

Have you played mine?
theperiodictableofelementsgame.com

18.12.2024 18:55 👍 0 🔁 0 💬 0 📌 0

What kind of download size do you get for a simple Hello World?

03.07.2023 08:14 👍 0 🔁 0 💬 0 📌 0

Enable long paths in Windows

03.07.2023 08:13 👍 1 🔁 0 💬 0 📌 0

It means that you don't know, not that you cannot know.

03.07.2023 08:12 👍 0 🔁 0 💬 0 📌 0