Issue with orchard.root.po translation (LocalizedRequiredAttribute)

Topics: Localization
Sep 5, 2014 at 9:13 AM
Edited Sep 5, 2014 at 9:23 AM
A while ago, I wanted to translate a string in LocalizedRequiredAttribute, namely: "The {0} field is required."

I came to learn that translations in files such as this one, should be peformed in orchard.root.po instead of orchard.core.po/orchard.theme.po/orchard.module.po.

So, I downloaded the default translation for orchard.root.po and I found this string entry in it already. I went on ahead and translated it to Dutch, but this doesn't seem to have any effect at all.

The following entry is present in the aforementioned thread:
#: "Orchard.Mvc.DataAnnotations.LocalizedRequiredAttribute"
#| msgid "The {0} field is required."
msgctxt "Orchard.Mvc.DataAnnotations.LocalizedRequiredAttribute"
msgid "The {0} field is required."
msgstr "Het veld {0} is vereist."
I tried multiple approaches such as removing the quotation marks and the msgctxt, but none of it seemed to change the situation at all.

I placed the orchard.root.po in the App_Data folder of my Orchard.Web solution and I placed it on the server in the respective manner.
Mar 18, 2015 at 10:44 AM
I've noticed the same problem with the [Required] attribute not being translated.

After a bit of digging, it boils down on how the scope and context are created for these particular attributes:
     foreach (var attribute in attributes) {
         Func<ValidationAttribute, Localizer, ValidationAttribute> localizedAttribute;

         // default translations use the attribute's scope, e.g., Orchard.Mvc.DataAnnotations.LocalizedRequiredAttribute
         var localAttribute = attribute;
         var tProvider = new Lazy<Localizer>(() => LocalizationUtilities.Resolve(context, localAttribute.GetType().FullName));

         var validationAttribute = attribute as ValidationAttribute;

         // substitute the attribute to its localized version if available
         if ( _validationAttributes.TryGetValue(attribute.GetType(), out localizedAttribute) ) {
             localizedAttributes.Add(localizedAttribute((ValidationAttribute)attribute, tProvider.Value));
         }                                                                                                                        
the tProvider initialization is creating a problem, given that the scope that is creating is "System.ComponentModel.DataAnnotations.RequiredAttribute" and not the one in the po file.

I'm trying to fix this, but some suggestions may be:
  • Fix the po file to either remove the context or put the correct context.
  • Fix the LocalizedModelValidatorProvider.cs to generate the correct scope for the validation attributes.
Mar 18, 2015 at 10:54 AM
Just to confirm that solution 1 works fine. Simply go into the orchard.root.po file and replace all the

Orchard.Mvc.DataAnnotations.LocalizedXXXAttribute

With the corresponding:

System.ComponentModel.DataAnnotations.XXXAttribute

I'll check if a fix to the LocalizedModelValidatorProvider.cs is something doable (or easier).