Converter: Enable translation for error messages (#3313)#10411
Converter: Enable translation for error messages (#3313)#10411ScarletKuro merged 14 commits intoMudBlazor:devfrom
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## dev #10411 +/- ##
==========================================
+ Coverage 91.60% 91.73% +0.13%
==========================================
Files 427 427
Lines 13368 13375 +7
Branches 2580 2581 +1
==========================================
+ Hits 12246 12270 +24
+ Misses 544 531 -13
+ Partials 578 574 -4 ☔ View full report in Codecov by Sentry. |
|
Add these to BindingConverterTests.cs should get you closer to 100% coverage. Mostly just missing Error testing. Which Looks like Converter.cs instead of setting SetErrorMessage and GetErrorMessage directly the error messages should be calling UpdateSetErrorMessage() and UpdateGetErrorMessage() respectively. |
|
Thanks for the PR. I'm not sure I'm a fan of this approach. The design seems off, with attaching the localizer to the Converter from the side. First, converters are something I plan to rewrite sooner or later. I want to make them more like in WPF, where the culture is part of the From/To methods, rather than being just a side property, as we have with the localizer here. Also, I'm not a fan of the universal converter that attempts to handle all conversions. As well I don't like that it has this Second, I would approach the translation differently. I wouldn't translate it at the Converter level, so it wouldn't depend on the localizer. Instead, I would spit out the translation keys -UpdateGetError(Localizer?[LanguageResource.DefaultConverter_InvalidBoolean] ?? "Not a valid boolean");
+UpdateGetError(LanguageResource.DefaultConverter_InvalidBoolean);And have the components that listen for the handle that. These components would then depend on the localizer, catch the key, and only then perform the translation. +Localizer[error] / Localizer[_converter.GetErrorMessage]This would feel more natural. |
|
@ScarletKuro Thank you for the review! Your solution indeed looks cleaner. However, I have a question regarding handling strings with interpolation. For example: I initially considered changing to UpdateGetError(string msg, params object[] arguments) to support interpolation, but this would also require modifying
to something like a Alternatively, I could change the existing behavior in and implement a custom solution, but that also seems strange. |
|
I guess we could do a breaking change and make it ? |
…ub.com/pwasilewski/MudBlazor into feat/localize-converter-error-messages
|
Feedback applied. I hope it's better now. I also made two additional changes since the last update:
|
|
@henon, I’m not sure who else needs to review this. |
|
@ScarletKuro As you can see from the issue number, this issue has existed for a long time. I know this is needed, so I'd say let's go for it. I know that @HClausing is using converters. @HClausing what do you think about merging this in v8? @pwasilewski can you write a sentence for our migration guide that advises users how they should adapt their code to this breaking change? |
|
@henon In Converter.cs, several property signatures have been updated to enhance localization support. These properties no longer return English messages directly. Instead, they now return keys intended for localization, accompanied by additional parameters:
|
|
|
Added to v8.0.0 Migration Guide #9953 |
I really like this approach. Yes, I've written some custom Converters to dynamically apply masks for brazilian social ID's (companies and natural peopla have different masks, but should be placed on the same field on model/binding). Would be nice to have it on v8. |
|
Thanks @pwasilewski ! |



Description
This contribution refactors error message handling in
ConverterandDefaultConverter. The converters now return error message keys and optional arguments. Localization is performed byMudFormComponent, which resolves these keys and arguments into localized error messages using theInternalMudLocalizerservice.Unfortunately, some method signatures and property types have been modified as part of this change, resulting in breaking changes.
Fixes #3313
How Has This Been Tested?
Visually: by changing the
.resxfile and verifying that the appropriate localized error messages appear.Unit Tests: by adding relevant unit tests.
Type of Changes
Checklist
dev).