Localize validation messages with user lang files (#15820)

* Localize validation messages with user lang files

* Fixed unit tests
This commit is contained in:
Kenn Jacobsen
2024-03-04 12:50:24 +01:00
committed by GitHub
parent f17f5522c0
commit 21126b7ba6
5 changed files with 27 additions and 5 deletions

View File

@@ -86,5 +86,7 @@ internal class ContentPropertyDisplayMapper : ContentPropertyBasicMapper<Content
// Translate // Translate
dest.Label = _textService.UmbracoDictionaryTranslate(_cultureDictionary, dest.Label); dest.Label = _textService.UmbracoDictionaryTranslate(_cultureDictionary, dest.Label);
dest.Description = _textService.UmbracoDictionaryTranslate(_cultureDictionary, dest.Description); dest.Description = _textService.UmbracoDictionaryTranslate(_cultureDictionary, dest.Description);
dest.Validation.MandatoryMessage = _textService.UmbracoDictionaryTranslate(_cultureDictionary, dest.Validation.MandatoryMessage);
dest.Validation.PatternMessage = _textService.UmbracoDictionaryTranslate(_cultureDictionary, dest.Validation.PatternMessage);
} }
} }

View File

@@ -1,5 +1,8 @@
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Cms.Core.Cache; using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Dictionary;
using Umbraco.Cms.Core.Models; using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.PropertyEditors; using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Extensions; using Umbraco.Extensions;
@@ -12,17 +15,30 @@ public class PropertyValidationService : IPropertyValidationService
private readonly PropertyEditorCollection _propertyEditors; private readonly PropertyEditorCollection _propertyEditors;
private readonly ILocalizedTextService _textService; private readonly ILocalizedTextService _textService;
private readonly IValueEditorCache _valueEditorCache; private readonly IValueEditorCache _valueEditorCache;
private readonly ICultureDictionary _cultureDictionary;
[Obsolete("Use the constructor that accepts ICultureDictionary. Will be removed in V15.")]
public PropertyValidationService( public PropertyValidationService(
PropertyEditorCollection propertyEditors, PropertyEditorCollection propertyEditors,
IDataTypeService dataTypeService, IDataTypeService dataTypeService,
ILocalizedTextService textService, ILocalizedTextService textService,
IValueEditorCache valueEditorCache) IValueEditorCache valueEditorCache)
: this(propertyEditors, dataTypeService, textService, valueEditorCache, StaticServiceProvider.Instance.GetRequiredService<ICultureDictionary>())
{
}
public PropertyValidationService(
PropertyEditorCollection propertyEditors,
IDataTypeService dataTypeService,
ILocalizedTextService textService,
IValueEditorCache valueEditorCache,
ICultureDictionary cultureDictionary)
{ {
_propertyEditors = propertyEditors; _propertyEditors = propertyEditors;
_dataTypeService = dataTypeService; _dataTypeService = dataTypeService;
_textService = textService; _textService = textService;
_valueEditorCache = valueEditorCache; _valueEditorCache = valueEditorCache;
_cultureDictionary = cultureDictionary;
} }
/// <inheritdoc /> /// <inheritdoc />
@@ -76,13 +92,13 @@ public class PropertyValidationService : IPropertyValidationService
if (isRequired && !string.IsNullOrWhiteSpace(isRequiredMessage) && if (isRequired && !string.IsNullOrWhiteSpace(isRequiredMessage) &&
requiredDefaultMessages.Contains(validationResult.ErrorMessage, StringComparer.OrdinalIgnoreCase)) requiredDefaultMessages.Contains(validationResult.ErrorMessage, StringComparer.OrdinalIgnoreCase))
{ {
validationResult.ErrorMessage = isRequiredMessage; validationResult.ErrorMessage = _textService.UmbracoDictionaryTranslate(_cultureDictionary, isRequiredMessage);
} }
if (!string.IsNullOrWhiteSpace(validationRegExp) && !string.IsNullOrWhiteSpace(validationRegExpMessage) && if (!string.IsNullOrWhiteSpace(validationRegExp) && !string.IsNullOrWhiteSpace(validationRegExpMessage) &&
formatDefaultMessages.Contains(validationResult.ErrorMessage, StringComparer.OrdinalIgnoreCase)) formatDefaultMessages.Contains(validationResult.ErrorMessage, StringComparer.OrdinalIgnoreCase))
{ {
validationResult.ErrorMessage = validationRegExpMessage; validationResult.ErrorMessage = _textService.UmbracoDictionaryTranslate(_cultureDictionary, validationRegExpMessage);
} }
yield return validationResult; yield return validationResult;

View File

@@ -7,6 +7,7 @@ using Moq;
using NUnit.Framework; using NUnit.Framework;
using Umbraco.Cms.Core; using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Cache; using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Dictionary;
using Umbraco.Cms.Core.Events; using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Models; using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Notifications; using Umbraco.Cms.Core.Notifications;
@@ -1195,7 +1196,7 @@ public class ContentServiceTests : UmbracoIntegrationTestWithContent
// content cannot publish values because they are invalid // content cannot publish values because they are invalid
var propertyValidationService = new PropertyValidationService(PropertyEditorCollection, DataTypeService, var propertyValidationService = new PropertyValidationService(PropertyEditorCollection, DataTypeService,
TextService, ValueEditorCache); TextService, ValueEditorCache, Mock.Of<ICultureDictionary>());
var isValid = propertyValidationService.IsPropertyDataValid(content, out var invalidProperties, var isValid = propertyValidationService.IsPropertyDataValid(content, out var invalidProperties,
CultureImpact.Invariant); CultureImpact.Invariant);
Assert.IsFalse(isValid); Assert.IsFalse(isValid);

View File

@@ -5,6 +5,7 @@ using Moq;
using NUnit.Framework; using NUnit.Framework;
using Umbraco.Cms.Core; using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Cache; using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Dictionary;
using Umbraco.Cms.Core.IO; using Umbraco.Cms.Core.IO;
using Umbraco.Cms.Core.Models; using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.PropertyEditors; using Umbraco.Cms.Core.PropertyEditors;
@@ -615,6 +616,7 @@ public class VariationTests
propertyEditorCollection, propertyEditorCollection,
dataTypeService, dataTypeService,
localizedTextService, localizedTextService,
new ValueEditorCache()); new ValueEditorCache(),
Mock.Of<ICultureDictionary>());
} }
} }

View File

@@ -6,6 +6,7 @@ using Moq;
using NUnit.Framework; using NUnit.Framework;
using Umbraco.Cms.Core; using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Cache; using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Dictionary;
using Umbraco.Cms.Core.IO; using Umbraco.Cms.Core.IO;
using Umbraco.Cms.Core.Models; using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.PropertyEditors; using Umbraco.Cms.Core.PropertyEditors;
@@ -51,7 +52,7 @@ public class PropertyValidationServiceTests
var propEditors = new PropertyEditorCollection(new DataEditorCollection(() => new[] { dataEditor })); var propEditors = new PropertyEditorCollection(new DataEditorCollection(() => new[] { dataEditor }));
validationService = new PropertyValidationService(propEditors, dataTypeService.Object, Mock.Of<ILocalizedTextService>(), new ValueEditorCache()); validationService = new PropertyValidationService(propEditors, dataTypeService.Object, Mock.Of<ILocalizedTextService>(), new ValueEditorCache(), Mock.Of<ICultureDictionary>());
} }
[Test] [Test]