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 <noreply@anthropic.com>
This commit is contained in:
@@ -94,15 +94,19 @@ public sealed partial class TextCatalog : ITextCatalog
|
||||
/// </summary>
|
||||
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<TextCatalog>.Instance);
|
||||
|
||||
private static readonly Lazy<TextCatalog> DefaultInstance = new(() => CreateDefault());
|
||||
private static readonly Lazy<TextCatalog> DefaultInstance = new(() => CreateDefault(
|
||||
AppDomain.CurrentDomain.GetAssemblies()
|
||||
.Where(a => a.GetName().Name?.StartsWith("SlpModularCms.", StringComparison.Ordinal) == true)
|
||||
.ToArray()));
|
||||
|
||||
/// <summary>
|
||||
/// 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 <c>SlpModularCms.*</c> 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.
|
||||
/// </summary>
|
||||
public static TextCatalog Default => DefaultInstance.Value;
|
||||
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using FluentAssertions;
|
||||
using SlpModularCms.Core.Texts;
|
||||
using SlpModularCms.Modules.Offerings.Models;
|
||||
|
||||
namespace SlpModularCms.Modules.Offerings.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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<ValidationResult>();
|
||||
|
||||
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<string> { 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.");
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -19,5 +19,5 @@ public record CreateOfferingRequest(
|
||||
) : IValidatableObject
|
||||
{
|
||||
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
||||
=> OfferingSchedule.Validate(StartsAt, EndsAt);
|
||||
=> OfferingSchedule.Validate(StartsAt, EndsAt, validationContext);
|
||||
}
|
||||
|
||||
@@ -16,16 +16,16 @@ public class FeaturesValidationAttribute : ValidationAttribute
|
||||
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
|
||||
{
|
||||
if (value is not List<string> 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;
|
||||
|
||||
@@ -35,26 +35,27 @@ public record OfferingBannerRequest(
|
||||
{
|
||||
public IEnumerable<ValidationResult> 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)]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,10 +6,10 @@ namespace SlpModularCms.Modules.Offerings.Models;
|
||||
/// <summary>The one rule for "is this offering shown right now", shared by the public list and the SEO injection.</summary>
|
||||
public static class OfferingSchedule
|
||||
{
|
||||
public static IEnumerable<ValidationResult> Validate(DateTimeOffset? startsAt, DateTimeOffset? endsAt)
|
||||
public static IEnumerable<ValidationResult> 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)]);
|
||||
}
|
||||
|
||||
/// <summary>Switched on, started (inclusive) and not yet ended (exclusive).</summary>
|
||||
|
||||
@@ -19,5 +19,5 @@ public record UpdateOfferingRequest(
|
||||
) : IValidatableObject
|
||||
{
|
||||
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
||||
=> OfferingSchedule.Validate(StartsAt, EndsAt);
|
||||
=> OfferingSchedule.Validate(StartsAt, EndsAt, validationContext);
|
||||
}
|
||||
|
||||
@@ -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<IHistoryRepository, NonLockingMySQLHistoryRepository>();
|
||||
});
|
||||
|
||||
services.AddCmsTextsFrom(typeof(OfferingsModule).Assembly);
|
||||
services.AddScoped<IOfferingRepository, OfferingRepository>();
|
||||
services.AddScoped<IOfferingsService, OfferingsService>();
|
||||
services.AddScoped<IIndexHtmlMarkerProvider, OfferingsIndexMarkers>();
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using SlpModularCms.Core.Texts;
|
||||
|
||||
namespace SlpModularCms.Modules.Offerings;
|
||||
|
||||
/// <summary>The keys of the Offerings module's texts (<c>Texts/nl.json</c>, <c>Texts/en.json</c>, key path <c>offerings.*</c>).</summary>
|
||||
public static class OfferingsTexts
|
||||
{
|
||||
/// <summary>Messages of request validation, shown to the administrator.</summary>
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The catalogue of the request being validated, or the shared default one outside a request (unit tests).</summary>
|
||||
public static ITextCatalog From(ValidationContext? context)
|
||||
=> context?.GetService(typeof(ITextCatalog)) as ITextCatalog ?? TextCatalog.Default;
|
||||
}
|
||||
@@ -16,4 +16,8 @@
|
||||
<ProjectReference Include="..\SlpModularCms.Core\SlpModularCms.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="Texts\*.json" WithCulture="false" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -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."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user