0

I want to localize my application for Russian.

I have this model (simplified):

public class MyModel
{
    [Range(1, 100, ErrorMessage = "RangeError")] // - this translates fine
    public int? PremiseArea { get; set; }
}

I have .resx file with my custom localization strings and when I enter 200 in input, the error shows in Russian, as I want.
My problem is when I'm entering letters in the input. I get this error:

Please enter a valid number.

How to handle these errors? Where to find the list of all the DataAnnotations errors by default?

svstnv
  • 205
  • 3
  • 11

2 Answers2

1

There are different type of validation error messages.

  • Data annotations validation errors
  • Model binding validation errors
  • Identity describer validation errors
  • Custom validation error messages

Additionally, there are browser form validation errors that depends on the field type, and you can override those errors by configuring client side validation.

Each of them requires different approach to be localized.

You can read these articles to learn more about localization in depth:

Last but not least:

Configuring all localization settings takes a lot of time and effort, if you want to do it faster I suggest using XLocalizer, it does all settings in simple setup in the startup, additionlly it can do online translation and auto adding for the localizable strings, so you don't fill resources manually, see the docs for more details.

LazZiya
  • 5,286
  • 2
  • 24
  • 37
  • 1
    Thank you for your answer! I found some interesting things from the links that you attached – svstnv May 25 '21 at 07:06
1

'Please enter a valid number.' this format looks like a default jquery validation message.

How did you apply the validation?

You can also directly define the validation message in javascript,and then localize the validation

message, as my following demo. And this is jquery validation documentation.

<script>
    $(document).ready(function () {
        $('#form1').validate({ 
            rules: {
                PremiseArea: {    //rule the number and range
                    number: true,
                    range: (1, 100)
                }
            },
            messages: {
                PremiseArea: {
                    number:"@Localizer["Please enter a valid number."]",  //localize the message
                    range: "@Localizer["Range Error"]"
                }
            }
        });
    });
</script>

enter image description here

Jerry Cai
  • 869
  • 1
  • 4
  • 4
  • Yes, thank you! Indeed, the problem was on the client-side. I solved it by including this code in my project https://cdn.jsdelivr.net/npm/jquery-validation@1.17.0/dist/localization/messages_ru.js – svstnv May 25 '21 at 07:08