1

I am using Portable Object Localization in my ASP.NET MVC app.

I need to pass a parameter to a translation string e.g.

msgid "The {0} field is required"
msgstr[0] "موجودیت {0} لازمی میباشد"

I want to use the above example for every required field for my models.


using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace aaeamis.Models
{
    public class OrganizationModel
    {
        public int OrganizationId { get; set; }
        // I want to pass "Organization Name" to translation
        [Required(ErrorMessage = "The Organization Name field is required")]
        public string OrganizationName { get; set; }
        // I want to pass "Location" to translation
        [Required(ErrorMessage = "The Location field is required")]
        public string Location{ get; set; }
        public int CreatedBy { get; set; }
        public DateTime CreatedAt { get; set; } = DateTime.Now;
        public DateTime CreatedAt { get; set; } = DateTime.Now;
    }
}

How can I pass "Organization Name" to translation as parameter?

2 Answers 2

1

The [Required] attribute already supports parameterized error messages. In fact, the default English error message is:

The {0} field is required.

When the message is being constructed, it will then use the Name of the [Display] attribute to fill in that parameter. If you haven’t specified an explicit display name, the name of the property will be used by default.

So the following property setup will provide an error message "The Location field is required" when the value was not set:

[Display(Name = "Location")]
[Required(ErrorMessage = "The {0} field is required")]
public string Location{ get; set; }

Both the text values for the Display and the Required attribute (along with other validation attributes) can be provided via ressources, so you can also generalize this for multiple languages without having to specify the actual strings in the attributes.

Sign up to request clarification or add additional context in comments.

3 Comments

Hello, Thanks for the solution. I have another question. Now I am using Blazor and I think Blazor does not support data annotations localization so the validation messages are not localized anymore. Do you know any good solution for that? I also want to know how to pass a parameter to a localization string. This is how it is done in Laravel Localization. ":record was :action successfully!" : ":record با موفقیت :action شد!" __(":record was :action successfully!", [ "record"=>__("Employee"), "action" => __("Terminated") ])
@NajeebAnwari There is an extra page on localization support in Blazor in the documentation.
According to documentation --> IHtmlLocalizer, IViewLocalizer, and Data Annotations localization are ASP.NET Core MVC features and not supported in Blazor apps. So is there any other way to solve the problem?
0

First, you need to get the string "OrganizationName" from the variable itself

var fieldName = nameof(OrganizationName)

Then, if you are on C# 10 (.NET 6.0) you can use const string interpolation to set the error message.

[Required(ErrorMessage = $"The {nameof(OrganizationName)} field is require")]
public string OrganizationName { get; set; }

[Required(ErrorMessage = $"The {nameof(Location)} field is require")]
public string Location { get; set; }

Note the $ before the string.

2 Comments

I think you didn't get my point. I want to translate the string and pass a parameter as well. Like var myString = "Organization Name" $"The {myString} field is required". Passing a value to the string is easy. But I want my translation to accept a parameter from the data annotation to translate it. msgid "The {0} field is required" msgstr[0] "موجودیت {0} لازمی میباشد" So if I pass the "Organization Name", the result will be msgid "The {Organization Name} field is required" msgstr[0] "موجودیت () لازمی میباشد" I also have the "Organization Name" translation in my ob file.
Maybe I'm still not getting your point, or maybe i didnt read the spec (gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/…) correctly, but msgstr[0] seems to be related to the plural form and its not going to pass a string in regardless, only and int. Whats your Plural-Forms configured to?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.