From d12e44350e6124d380b4a144ed6ccd6680f7c3a6 Mon Sep 17 00:00:00 2001 From: Sluijsens Date: Sun, 15 Nov 2026 20:44:38 +0100 Subject: [PATCH] Vertaalbestanden 4/6: Offerings-validatiemeldingen naar Texts/nl.json en en.json (offerings.validation.*) GEDRAGSWIJZIGING (goedgekeurd): de validatiemeldingen waren Engels en zijn nu Nederlands bij Texts:DefaultLanguage=nl (alleen zichtbaar voor beheerders). De Engelse teksten zijn de oude meldingen, letterlijk. TextCatalog.Default omvat nu alle geladen SlpModularCms.*-assemblies (fallback buiten DI). Co-Authored-By: Claude Sonnet 5.5 --- src/SlpModularCms.Core/Texts/TextCatalog.cs | 12 ++-- .../OfferingsTextsTests.cs | 66 +++++++++++++++++++ .../Controllers/OfferingBannersController.cs | 3 +- .../Models/CreateOfferingRequest.cs | 2 +- .../Models/FeaturesValidationAttribute.cs | 6 +- .../Models/OfferingBannerModels.cs | 13 ++-- .../Models/OfferingSchedule.cs | 4 +- .../Models/UpdateOfferingRequest.cs | 2 +- .../OfferingsModule.cs | 2 + .../OfferingsTexts.cs | 37 +++++++++++ .../SlpModularCms.Modules.Offerings.csproj | 4 ++ .../Texts/en.json | 21 ++++++ .../Texts/nl.json | 21 ++++++ 13 files changed, 175 insertions(+), 18 deletions(-) create mode 100644 src/SlpModularCms.Modules.Offerings.Tests/OfferingsTextsTests.cs create mode 100644 src/SlpModularCms.Modules.Offerings/OfferingsTexts.cs create mode 100644 src/SlpModularCms.Modules.Offerings/Texts/en.json create mode 100644 src/SlpModularCms.Modules.Offerings/Texts/nl.json diff --git a/src/SlpModularCms.Core/Texts/TextCatalog.cs b/src/SlpModularCms.Core/Texts/TextCatalog.cs index 492b141..681be5b 100644 --- a/src/SlpModularCms.Core/Texts/TextCatalog.cs +++ b/src/SlpModularCms.Core/Texts/TextCatalog.cs @@ -94,15 +94,19 @@ public sealed partial class TextCatalog : ITextCatalog /// public static TextCatalog CreateDefault(params Assembly[] extraAssemblies) => new( Options.Create(new TextsOptions()), - new[] { new TextAssembly(typeof(TextCatalog).Assembly) }.Concat(extraAssemblies.Select(a => new TextAssembly(a))), + new[] { typeof(TextCatalog).Assembly }.Concat(extraAssemblies).Distinct().Select(a => new TextAssembly(a)), [], Microsoft.Extensions.Logging.Abstractions.NullLogger.Instance); - private static readonly Lazy DefaultInstance = new(() => CreateDefault()); + private static readonly Lazy DefaultInstance = new(() => CreateDefault( + AppDomain.CurrentDomain.GetAssemblies() + .Where(a => a.GetName().Name?.StartsWith("SlpModularCms.", StringComparison.Ordinal) == true) + .ToArray())); /// - /// A shared catalogue over Core's own texts in Dutch, for code that is not given the real one (unit tests, a service - /// constructed by hand). The hosts always inject the configured catalogue. + /// A shared catalogue in Dutch over the texts of Core and of every SlpModularCms.* assembly loaded when it is + /// first used, for code that is not given the real one (unit tests, a service constructed by hand). The hosts always + /// inject the configured catalogue. /// public static TextCatalog Default => DefaultInstance.Value; diff --git a/src/SlpModularCms.Modules.Offerings.Tests/OfferingsTextsTests.cs b/src/SlpModularCms.Modules.Offerings.Tests/OfferingsTextsTests.cs new file mode 100644 index 0000000..bbb5152 --- /dev/null +++ b/src/SlpModularCms.Modules.Offerings.Tests/OfferingsTextsTests.cs @@ -0,0 +1,66 @@ +using System.ComponentModel.DataAnnotations; +using FluentAssertions; +using SlpModularCms.Core.Texts; +using SlpModularCms.Modules.Offerings.Models; + +namespace SlpModularCms.Modules.Offerings.Tests; + +/// +/// The validation messages moved into the text files. The English texts are exactly the messages that were in the code; +/// the Dutch ones are NEW (the messages used to be English only): with the site language Dutch an administrator now gets +/// Dutch validation messages. That is the one deliberate change of behaviour of the translation work. +/// +public class OfferingsTextsTests +{ + private static readonly TextCatalog Catalog = TextCatalog.CreateDefault(typeof(OfferingsTexts).Assembly); + + [Fact] + public void TheTextFiles_MatchTheDeclaredKeys_InDutchAndEnglish() + => TextAudit.Verify(typeof(OfferingsTexts).Assembly, TextAudit.KeysOf(typeof(OfferingsTexts)), "nl", "en").Should().BeEmpty(); + + [Theory] + [InlineData("features.notAList", "Features must be a list of strings.")] + [InlineData("features.count", "Features must contain between {min} and {max} items.")] + [InlineData("features.item", "Each feature must be non-empty and at most {max} characters.")] + [InlineData("banner.position", "Position must be 'top' or 'bottom'.")] + [InlineData("banner.topTextTooLong", "A top banner text can be at most {max} characters.")] + [InlineData("banner.bottomNeedsTitle", "A bottom banner needs a title.")] + [InlineData("banner.linkTogether", "Link label and link URL must be given together.")] + [InlineData("banner.linkNotAllowed", "Link URL must be an https:// address or a site path starting with '/'.")] + [InlineData("schedule.endAfterStart", "EndsAt must be after StartsAt.")] + public void TheEnglishTexts_AreTheMessagesThatWereInTheCode(string key, string expected) + => Catalog.Get(new TextKey("offerings.validation." + key, "min", "max"), "en", ("min", "{min}"), ("max", "{max}")).Should().Be(expected); + + [Fact] + public void WithTheSiteLanguageDutch_TheValidationMessagesAreDutch() + { + var banner = new OfferingBannerRequest("", "Tekst", Position: "bottom"); + var results = new List(); + + Validator.TryValidateObject(banner, new ValidationContext(banner), results, validateAllProperties: false); + + results.Should().ContainSingle().Which.ErrorMessage.Should().Be("Een banner onderaan heeft een titel nodig."); + } + + [Fact] + public void TheFeaturesMessages_CarryTheirLimits() + { + var attribute = new FeaturesValidationAttribute(); + + var tooMany = attribute.GetValidationResult(Enumerable.Repeat("x", 11).ToList(), new ValidationContext(new object()) { MemberName = "Features" }); + var tooLong = attribute.GetValidationResult(new List { new('x', 201) }, new ValidationContext(new object()) { MemberName = "Features" }); + + tooMany!.ErrorMessage.Should().Be("Kenmerken moeten tussen 1 en 10 items bevatten."); + tooLong!.ErrorMessage.Should().Be("Elk kenmerk mag niet leeg zijn en maximaal 200 tekens bevatten."); + } + + [Fact] + public void TheScheduleMessage_IsShared_ByOfferingsAndBanners() + { + var starts = DateTimeOffset.UtcNow; + var offering = new CreateOfferingRequest("T", null, "p", null, ["f"], "c", StartsAt: starts, EndsAt: starts); + + offering.Validate(new ValidationContext(offering)).Should().ContainSingle() + .Which.ErrorMessage.Should().Be("Het einde moet na het begin liggen."); + } +} diff --git a/src/SlpModularCms.Modules.Offerings/Controllers/OfferingBannersController.cs b/src/SlpModularCms.Modules.Offerings/Controllers/OfferingBannersController.cs index e3067a2..0e6085d 100644 --- a/src/SlpModularCms.Modules.Offerings/Controllers/OfferingBannersController.cs +++ b/src/SlpModularCms.Modules.Offerings/Controllers/OfferingBannersController.cs @@ -1,3 +1,4 @@ +using SlpModularCms.Core.Texts; using System.Security.Claims; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -59,7 +60,7 @@ public class PublicOfferingBannerController(IOfferingBannersService service) : C { position = string.IsNullOrEmpty(position) ? OfferingBannerPositions.Bottom : position; if (!OfferingBannerPositions.IsValid(position)) - return BadRequest(new { error = "Position must be 'top' or 'bottom'." }); + return BadRequest(new { error = HttpTexts.From(HttpContext).Get(OfferingsTexts.Validation.Banner.Position) }); Response.Headers.CacheControl = "public, max-age=60"; var current = await service.GetActiveAsync(position); diff --git a/src/SlpModularCms.Modules.Offerings/Models/CreateOfferingRequest.cs b/src/SlpModularCms.Modules.Offerings/Models/CreateOfferingRequest.cs index 1069950..7c4561e 100644 --- a/src/SlpModularCms.Modules.Offerings/Models/CreateOfferingRequest.cs +++ b/src/SlpModularCms.Modules.Offerings/Models/CreateOfferingRequest.cs @@ -19,5 +19,5 @@ public record CreateOfferingRequest( ) : IValidatableObject { public IEnumerable Validate(ValidationContext validationContext) - => OfferingSchedule.Validate(StartsAt, EndsAt); + => OfferingSchedule.Validate(StartsAt, EndsAt, validationContext); } diff --git a/src/SlpModularCms.Modules.Offerings/Models/FeaturesValidationAttribute.cs b/src/SlpModularCms.Modules.Offerings/Models/FeaturesValidationAttribute.cs index 8d5380d..efdb7d4 100644 --- a/src/SlpModularCms.Modules.Offerings/Models/FeaturesValidationAttribute.cs +++ b/src/SlpModularCms.Modules.Offerings/Models/FeaturesValidationAttribute.cs @@ -16,16 +16,16 @@ public class FeaturesValidationAttribute : ValidationAttribute protected override ValidationResult? IsValid(object? value, ValidationContext validationContext) { if (value is not List features) - return new ValidationResult("Features must be a list of strings.", [validationContext.MemberName!]); + return new ValidationResult(OfferingsTexts.From(validationContext).Get(OfferingsTexts.Validation.Features.NotAList), [validationContext.MemberName!]); if (features.Count is < MinItems or > MaxItems) return new ValidationResult( - $"Features must contain between {MinItems} and {MaxItems} items.", + OfferingsTexts.From(validationContext).Get(OfferingsTexts.Validation.Features.Count, args: [("min", MinItems), ("max", MaxItems)]), [validationContext.MemberName!]); if (features.Any(f => string.IsNullOrWhiteSpace(f) || f.Length > MaxItemLength)) return new ValidationResult( - $"Each feature must be non-empty and at most {MaxItemLength} characters.", + OfferingsTexts.From(validationContext).Get(OfferingsTexts.Validation.Features.Item, args: ("max", MaxItemLength)), [validationContext.MemberName!]); return ValidationResult.Success; diff --git a/src/SlpModularCms.Modules.Offerings/Models/OfferingBannerModels.cs b/src/SlpModularCms.Modules.Offerings/Models/OfferingBannerModels.cs index d98d499..9f4ceba 100644 --- a/src/SlpModularCms.Modules.Offerings/Models/OfferingBannerModels.cs +++ b/src/SlpModularCms.Modules.Offerings/Models/OfferingBannerModels.cs @@ -35,26 +35,27 @@ public record OfferingBannerRequest( { public IEnumerable Validate(ValidationContext validationContext) { + var texts = OfferingsTexts.From(validationContext); if (!OfferingBannerPositions.IsValid(Position)) - yield return new ValidationResult("Position must be 'top' or 'bottom'.", [nameof(Position)]); + yield return new ValidationResult(texts.Get(OfferingsTexts.Validation.Banner.Position), [nameof(Position)]); else if (Position == OfferingBannerPositions.Top) { if (Text?.Trim().Length > OfferingBannerPositions.TopTextMaxLength) - yield return new ValidationResult($"A top banner text can be at most {OfferingBannerPositions.TopTextMaxLength} characters.", [nameof(Text)]); + yield return new ValidationResult(texts.Get(OfferingsTexts.Validation.Banner.TopTextTooLong, args: ("max", OfferingBannerPositions.TopTextMaxLength)), [nameof(Text)]); } else if (string.IsNullOrWhiteSpace(Title)) - yield return new ValidationResult("A bottom banner needs a title.", [nameof(Title)]); + yield return new ValidationResult(texts.Get(OfferingsTexts.Validation.Banner.BottomNeedsTitle), [nameof(Title)]); var hasLabel = !string.IsNullOrWhiteSpace(LinkLabel); var hasUrl = !string.IsNullOrWhiteSpace(LinkUrl); if (hasLabel != hasUrl) - yield return new ValidationResult("Link label and link URL must be given together.", [nameof(LinkLabel), nameof(LinkUrl)]); + yield return new ValidationResult(texts.Get(OfferingsTexts.Validation.Banner.LinkTogether), [nameof(LinkLabel), nameof(LinkUrl)]); else if (hasUrl && !OfferingBannerLinks.IsAllowed(LinkUrl!.Trim())) - yield return new ValidationResult("Link URL must be an https:// address or a site path starting with '/'.", [nameof(LinkUrl)]); + yield return new ValidationResult(texts.Get(OfferingsTexts.Validation.Banner.LinkNotAllowed), [nameof(LinkUrl)]); if (StartsAt is not null && EndsAt is not null && EndsAt <= StartsAt) - yield return new ValidationResult("EndsAt must be after StartsAt.", [nameof(EndsAt)]); + yield return new ValidationResult(texts.Get(OfferingsTexts.Validation.Schedule.EndAfterStart), [nameof(EndsAt)]); } } diff --git a/src/SlpModularCms.Modules.Offerings/Models/OfferingSchedule.cs b/src/SlpModularCms.Modules.Offerings/Models/OfferingSchedule.cs index ebe6969..99b9af8 100644 --- a/src/SlpModularCms.Modules.Offerings/Models/OfferingSchedule.cs +++ b/src/SlpModularCms.Modules.Offerings/Models/OfferingSchedule.cs @@ -6,10 +6,10 @@ namespace SlpModularCms.Modules.Offerings.Models; /// The one rule for "is this offering shown right now", shared by the public list and the SEO injection. public static class OfferingSchedule { - public static IEnumerable Validate(DateTimeOffset? startsAt, DateTimeOffset? endsAt) + public static IEnumerable Validate(DateTimeOffset? startsAt, DateTimeOffset? endsAt, ValidationContext? context = null) { if (startsAt is not null && endsAt is not null && endsAt <= startsAt) - yield return new ValidationResult("EndsAt must be after StartsAt.", [nameof(OfferingBanner.EndsAt)]); + yield return new ValidationResult(OfferingsTexts.From(context).Get(OfferingsTexts.Validation.Schedule.EndAfterStart), [nameof(OfferingBanner.EndsAt)]); } /// Switched on, started (inclusive) and not yet ended (exclusive). diff --git a/src/SlpModularCms.Modules.Offerings/Models/UpdateOfferingRequest.cs b/src/SlpModularCms.Modules.Offerings/Models/UpdateOfferingRequest.cs index ec5e645..ba6c87c 100644 --- a/src/SlpModularCms.Modules.Offerings/Models/UpdateOfferingRequest.cs +++ b/src/SlpModularCms.Modules.Offerings/Models/UpdateOfferingRequest.cs @@ -19,5 +19,5 @@ public record UpdateOfferingRequest( ) : IValidatableObject { public IEnumerable Validate(ValidationContext validationContext) - => OfferingSchedule.Validate(StartsAt, EndsAt); + => OfferingSchedule.Validate(StartsAt, EndsAt, validationContext); } diff --git a/src/SlpModularCms.Modules.Offerings/OfferingsModule.cs b/src/SlpModularCms.Modules.Offerings/OfferingsModule.cs index faa9a26..f665dd3 100644 --- a/src/SlpModularCms.Modules.Offerings/OfferingsModule.cs +++ b/src/SlpModularCms.Modules.Offerings/OfferingsModule.cs @@ -1,3 +1,4 @@ +using SlpModularCms.Core.Texts; using System.Diagnostics.CodeAnalysis; using Microsoft.AspNetCore.Builder; using Microsoft.EntityFrameworkCore; @@ -28,6 +29,7 @@ public class OfferingsModule : IModule options.ReplaceService(); }); + services.AddCmsTextsFrom(typeof(OfferingsModule).Assembly); services.AddScoped(); services.AddScoped(); services.AddScoped(); diff --git a/src/SlpModularCms.Modules.Offerings/OfferingsTexts.cs b/src/SlpModularCms.Modules.Offerings/OfferingsTexts.cs new file mode 100644 index 0000000..05207e0 --- /dev/null +++ b/src/SlpModularCms.Modules.Offerings/OfferingsTexts.cs @@ -0,0 +1,37 @@ +using System.ComponentModel.DataAnnotations; +using SlpModularCms.Core.Texts; + +namespace SlpModularCms.Modules.Offerings; + +/// The keys of the Offerings module's texts (Texts/nl.json, Texts/en.json, key path offerings.*). +public static class OfferingsTexts +{ + /// Messages of request validation, shown to the administrator. + public static class Validation + { + public static class Features + { + public static readonly TextKey NotAList = new("offerings.validation.features.notAList"); + public static readonly TextKey Count = new("offerings.validation.features.count", "min", "max"); + public static readonly TextKey Item = new("offerings.validation.features.item", "max"); + } + + public static class Banner + { + public static readonly TextKey Position = new("offerings.validation.banner.position"); + public static readonly TextKey TopTextTooLong = new("offerings.validation.banner.topTextTooLong", "max"); + public static readonly TextKey BottomNeedsTitle = new("offerings.validation.banner.bottomNeedsTitle"); + public static readonly TextKey LinkTogether = new("offerings.validation.banner.linkTogether"); + public static readonly TextKey LinkNotAllowed = new("offerings.validation.banner.linkNotAllowed"); + } + + public static class Schedule + { + public static readonly TextKey EndAfterStart = new("offerings.validation.schedule.endAfterStart"); + } + } + + /// The catalogue of the request being validated, or the shared default one outside a request (unit tests). + public static ITextCatalog From(ValidationContext? context) + => context?.GetService(typeof(ITextCatalog)) as ITextCatalog ?? TextCatalog.Default; +} diff --git a/src/SlpModularCms.Modules.Offerings/SlpModularCms.Modules.Offerings.csproj b/src/SlpModularCms.Modules.Offerings/SlpModularCms.Modules.Offerings.csproj index 0ec8c20..a91d84b 100644 --- a/src/SlpModularCms.Modules.Offerings/SlpModularCms.Modules.Offerings.csproj +++ b/src/SlpModularCms.Modules.Offerings/SlpModularCms.Modules.Offerings.csproj @@ -16,4 +16,8 @@ + + + + diff --git a/src/SlpModularCms.Modules.Offerings/Texts/en.json b/src/SlpModularCms.Modules.Offerings/Texts/en.json new file mode 100644 index 0000000..0b937ac --- /dev/null +++ b/src/SlpModularCms.Modules.Offerings/Texts/en.json @@ -0,0 +1,21 @@ +{ + "offerings": { + "validation": { + "features": { + "notAList": "Features must be a list of strings.", + "count": "Features must contain between {min} and {max} items.", + "item": "Each feature must be non-empty and at most {max} characters." + }, + "banner": { + "position": "Position must be 'top' or 'bottom'.", + "topTextTooLong": "A top banner text can be at most {max} characters.", + "bottomNeedsTitle": "A bottom banner needs a title.", + "linkTogether": "Link label and link URL must be given together.", + "linkNotAllowed": "Link URL must be an https:// address or a site path starting with '/'." + }, + "schedule": { + "endAfterStart": "EndsAt must be after StartsAt." + } + } + } +} diff --git a/src/SlpModularCms.Modules.Offerings/Texts/nl.json b/src/SlpModularCms.Modules.Offerings/Texts/nl.json new file mode 100644 index 0000000..82f241b --- /dev/null +++ b/src/SlpModularCms.Modules.Offerings/Texts/nl.json @@ -0,0 +1,21 @@ +{ + "offerings": { + "validation": { + "features": { + "notAList": "Kenmerken moeten een lijst met teksten zijn.", + "count": "Kenmerken moeten tussen {min} en {max} items bevatten.", + "item": "Elk kenmerk mag niet leeg zijn en maximaal {max} tekens bevatten." + }, + "banner": { + "position": "De locatie moet 'top' of 'bottom' zijn.", + "topTextTooLong": "De tekst van een bovenbalk mag maximaal {max} tekens bevatten.", + "bottomNeedsTitle": "Een banner onderaan heeft een titel nodig.", + "linkTogether": "Knoptekst en link moeten samen worden opgegeven.", + "linkNotAllowed": "De link moet een https://-adres zijn of een pad op de site dat met '/' begint." + }, + "schedule": { + "endAfterStart": "Het einde moet na het begin liggen." + } + } + } +}