I want the Validation string to be in Japanese.

Asked 2 years ago, Updated 2 years ago, 51 views

If ASP.NET Core v1.1 is numeric, there is a validation string that is automatically added. Is there a way to change it to Japanese?

[Example]
■ View model (excerpt)

public class SampleViewModel
{
    DisplayName ("Value")
    [RegularExpression("^[-.0-9]*$", ErrorMessage=" value must be numeric.")]
    public float?Value {get;set;}
}

■ View (excerpt)

<td>
    @ Html.DisplayFor (model.Value)
</td>

■ HTML code for display results

<div class="form-group">
    <label class="col-md-2 control-label" for="Value">Value</label>
    <div class="col-md-10">
        <input class="form-control" type="text" data-val="true" data-val-number="The field value must be a number.""data-val-regex=" value must be a number." data-val-regex-pattern="^[-.0-9]*$"id="Value" name="Value" value="">
        <span class="text-danger field-validation-valid" data-valmsg-for="Value" data-valmsg-replace="true"></span>
    </div>
</div>


in the above display results data-val-number="The field value must be a number."
There are parts written in English like this.This will be printed on the screen...

Also, although RegularExpression can be prevented to some extent, as in the view model, I would like to automatically overwrite the error message of "data-val-number" without adding unnecessary validation to the view model.

I have looked into various versions of ASP.NET, and there are many incompatibilities between versions, so I couldn't find a "smart way" that could be realized with the latest Core.
(I think it would be a smart way to override .NET Core resources.resx…)

asp.net

2022-09-29 21:20

1 Answers

I'm sorry.As I solved myself while trying and making errors, I will post it as an answer.
I would appreciate it if you could tell me how to do it better. m(__)m
How to DependencyInjection IModelBindingMessageProvider or something like that?

Startup.cs (excerpt)

public void ConfigureServices (IServiceCollection services)
{
    // Add framework services.
    services.AddMvc(setup=>
    {
        varmodelBindingMessageProvider=newModelBindingMessageProviderLocalization();
        setup.ModelBindingMessageProvider.ValueMustBeANumberAccessor=modelBindingMessageProvider.ValueMustBeANumberAccessor;
    });
}

ModelBindingMessageProviderLocalization.cs (excerpt)

public class ModelBindingMessageProviderLocalization
{
    private Func<string, string>_valueMustBeANumberAccessor;

    public ModelBindingMessageProviderLocalization()
    {
        ValueMustBeANumberAccessor=Resources.FormatHtmlGeneration_ValueMustBeNumber;
    }

    publicFunc<string, string>ValueMustBeANumberAccessor
    {
        get
        {
            return_valueMustBeANumberAccessor;
        }
        set
        {
            if(value==null)
            {
                through new ArgumentNullException (nameof(value));
            }

            _valueMustBeANumberAccessor=value;
        }
    }
}

Resources.resx (excerpt)

<data name="HtmlGeneration_ValueMustBeNumber" xml:space="preserve">
    <value>{0} must be a number.</value>
  </data>

Resources.Designer.cs (excerpt)

//<summary>
    /// The field {0} must be a number.
    /// </summary>
    internal static string HtmlGeneration_ValueMustBeNumber
    {
        get=>ResourceManager.GetString("HtmlGeneration_ValueMustBeNumber");
    }

    /// <summary>
    /// The field {0} must be a number.
    /// </summary>
    internal static string FormatHtmlGeneration_ValueMustBeNumber(object p0)
        =>string.Format(CultureInfo.CurrentCulture, ResourceManager.GetString("HtmlGeneration_ValueMustBeNumber"), p0);

■ Results

Run Results

今回 This time, the purpose was to rewrite the message "The field value must be a number." However, ModelBindingMessageProvider manages the following messages, so if you want to rewrite them, you should write the code as well.

top:property and resource names
bottom:messages

MissingBindRequiredValueAccessor=Resources.FormatModelBinding_MissingBindRequiredMember;
Value for the '{0}' property was not provided.

MissingKeyOrValueAccessor=Resources.FormatKeyValuePair_BothKeyAndValueMustBePresent;
Value is required.

MissingRequestBodyRequiredValueAccessor=Resources.FormatModelBinding_MissingRequestBodyRequiredMember;
An-empty request body is required.

ValueMustNotBeNullAccessor=Resources.FormatModelBinding_NullValueNotValid;
The value '{0}' is invalid.

AttemptedValueIsInvalidAccessor=Resources.FormatModelState_AttemptedValueIsInvalid;
The value '{0}' is not valid for {1}.

UnknownValueIsInvalidAccessor=Resources.FormatModelState_UnknownValueIsInvalid;
The supplied value is invalid for {0}

ValueIsInvalidAccessor=Resources.FormatHtmlGeneration_ValueIsInvalid;
The value '{0}' is invalid.

ValueMustBeANumberAccessor=Resources.FormatHtmlGeneration_ValueMustBeNumber;
The field {0} must be a number.

https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Metadata/ModelBindingMessageProvider.cs
https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNetCore.Mvc.Core/Resources.resx
Quoted from


2022-09-29 21:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.