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 Answers2

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.

poke
  • 369,085
  • 72
  • 557
  • 602
  • 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") ]) – Najeeb Anwari Oct 28 '22 at 14:52
  • @NajeebAnwari There is an extra page on [localization support in Blazor](https://learn.microsoft.com/en-us/aspnet/core/blazor/globalization-localization?view=aspnetcore-6.0&pivots=server) in the documentation. – poke Oct 29 '22 at 10:34
  • 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? – Najeeb Anwari Oct 29 '22 at 16:42
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.

Slack Groverglow
  • 846
  • 7
  • 25
  • 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. – Najeeb Anwari Sep 12 '22 at 06:23
  • Maybe I'm still not getting your point, or maybe i didnt read the spec (https://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/PO-Files.html) 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? – Slack Groverglow Sep 12 '22 at 06:53