Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
# Changelog

## `Dmnk.Blazor.Mvvm`: `0.0.9` - *unreleased*
## `Dmnk.LocalizedDataAnnotations`: `0.0.1` - *unreleased*

Initial release.

## `Dmnk.Blazor.Mvvm`: `0.0.9` - *2026-06-12*

**Added:**

- `IServiceCollection.AddBlazorMvvm()`, `IServiceCollection.AddViewModelRegistration()`
and `IServiceCollection.AddViewModelRegistrationOpenGeneric()` extension methods for
less verbose DI setup.

## `Dmnk.Blazor.Dialogs.Fluent`: `0.0.4` - *unreleased*
## `Dmnk.Blazor.Dialogs.Fluent`: `0.0.4` - *2026-06-12*

**Fixed**:

Expand Down
1 change: 1 addition & 0 deletions docfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"Dmnk.Icons.Blazor/bin/Release/net10.0/Dmnk.Icons.Blazor.dll",
"Dmnk.Icons.Blazor.Fluent/bin/Release/net10.0/Dmnk.Icons.Blazor.Fluent.dll",
"Dmnk.Icons.Core/bin/Release/netstandard2.0/Dmnk.Icons.Core.dll",
"Dmnk.LocalizedDataAnnotations/bin/Release/netstandard2.0/Dmnk.LocalizedDataAnnotations.dll",
],
"exclude": [
"**/obj/**",
Expand Down
2 changes: 2 additions & 0 deletions index.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Mostly small Blazor-related C# libraries. Documentation and Code are Work-In-Pro
- <xref:Dmnk.Blazor.Focus> - Blazor components and extensions to manage keyboard focus
- <xref:Dmnk.Blazor.Cookies> - Utility methods to read generic cookies as well as ASP.NET CultureInfo cookies in Blazor
WASM applications.
- <xref:Dmnk.LocalizedDataAnnotations> - Localization for `System.ComponentModel.DataAnnotations`
in modern .NET and netstandard2.0

### Icons

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="NUnit" />
<PackageReference Include="NUnit.Analyzers" />
<PackageReference Include="NUnit3TestAdapter" />
</ItemGroup>

<ItemGroup>
<Using Include="NUnit.Framework"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Dmnk.LocalizedDataAnnotations.Tests\Dmnk.LocalizedDataAnnotations.Tests.csproj" />
<ProjectReference Include="..\Dmnk.LocalizedDataAnnotations\Dmnk.LocalizedDataAnnotations.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Resources;
using Dmnk.LocalizedDataAnnotations.Tests;

namespace Dmnk.LocalizedDataAnnotations.Hack.Tests;

/// <summary>
/// This needs to live in a seperate assembly so that nunit can run it as a seperate
/// *process*. This is required because the built-in ValidationAttributes sometimes
/// cache their localized messages after the first validation, thereby preventing us from
/// switching culture at runtime.
/// </summary>
[TestFixture, Parallelizable(ParallelScope.None), SetCulture("en-US")]
public class LocalizedValidatorReflectionHackTests
{
[TearDown]
public void TearDown()
{
if (LocalizedValidatorReflectionHack.IsHacked)
LocalizedValidatorReflectionHack.UnHack();
SetCulture("en-US");
}

private static void SetCulture(string cultureName)
{
var culture = new CultureInfo(cultureName);
CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
}

private static List<ValidationResult> Validate(object model)
{
var context = new ValidationContext(model);
var results = new List<ValidationResult>();
Validator.TryValidateObject(model, context, results, true);
return results;
}

// -- Shared localization cases ---------------------------------------------

[Test, TestCaseSource(typeof(ValidatorTestCases), nameof(ValidatorTestCases.All))]
public void Localizes(ValidatorTestCase tc)
{
LocalizedValidatorReflectionHack.Hack(doThrow: true);

SetCulture("de-DE");
var deResults = Validate(tc.Model);
foreach (var s in tc.MessagesGermanContaining)
Assert.That(
deResults.Any(r => r.ErrorMessage?.Contains(s) == true), Is.True,
$"No German message contained: \"{s}\"");

if (!tc.CanSwitchAtRuntimeWithHack) return;

SetCulture("en-US");
var enResults = Validate(tc.Model);
foreach (var s in tc.MessagesEnglishContaining)
Assert.That(
enResults.Any(r => r.ErrorMessage?.Contains(s) == true), Is.True,
$"No English message contained: \"{s}\"");
}

// -- Hack-specific behavior ------------------------------------------------

[Test]
public void Hack_LocalizesRequiredAttribute()
{
Assert.That(
Validate(new ValidatorTestCases.RequiredModel())[0].ErrorMessage, Contains.Substring("is required"),
"Sanity check: English before hack");

LocalizedValidatorReflectionHack.Hack(doThrow: true);

SetCulture("de-DE");
Assert.That(
Validate(new ValidatorTestCases.RequiredModel())[0].ErrorMessage, Contains.Substring("ist erforderlich"),
"After hack, de-DE");

SetCulture("en-US");
Assert.That(
Validate(new ValidatorTestCases.RequiredModel())[0].ErrorMessage, Contains.Substring("is required"),
"After hack, en-US");
}

[Test]
public void UnHack_RestoresOriginalBehavior()
{
LocalizedValidatorReflectionHack.Hack(doThrow: true);
LocalizedValidatorReflectionHack.UnHack(doThrow: true);

SetCulture("de-DE");
Assert.That(
Validate(new ValidatorTestCases.RequiredModel())[0].ErrorMessage,
Contains.Substring("is required"),
"After un-hack, de-DE should still return English");
}

[Test]
public void Hack_IsIdempotent()
{
LocalizedValidatorReflectionHack.Hack(doThrow: true);
Assert.DoesNotThrow(() => LocalizedValidatorReflectionHack.Hack(doThrow: true));

SetCulture("de-DE");
Assert.That(
Validate(new ValidatorTestCases.RequiredModel())[0].ErrorMessage,
Contains.Substring("ist erforderlich"));
}

[Test]
public void UnHack_WhenNotHacked_IsNoOp()
{
Assert.DoesNotThrow(() => LocalizedValidatorReflectionHack.UnHack(doThrow: true));

SetCulture("de-DE");
Assert.That(
Validate(new ValidatorTestCases.RequiredModel())[0].ErrorMessage,
Contains.Substring("is required"),
"Should remain unlocalized after no-op un-hack");
}

[Test]
public void Hack_WithCustomResourceManager_UsesCustomMessages()
{
LocalizedValidatorReflectionHack.Hack(
doThrow: true,
resourceManager: new CustomResourceManager());

var results = Validate(new ValidatorTestCases.RequiredModel());
Assert.That(results[0].ErrorMessage, Contains.Substring("CUSTOM_REQUIRED"));
}

private class CustomResourceManager : ResourceManager
{
public override string? GetString(string name, CultureInfo? culture) =>
name == "RequiredAttribute_ValidationError" ? "CUSTOM_REQUIRED: {0}" : null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System.Collections;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Resources;
using Dmnk.LocalizedDataAnnotations.Hack;
using Dmnk.LocalizedDataAnnotations.MessageProvider;
using Dmnk.LocalizedDataAnnotations.Properties;
using RangeAttribute = System.ComponentModel.DataAnnotations.RangeAttribute;

namespace Dmnk.LocalizedDataAnnotations.Tests;

[TestFixture]
public class CurrentVersionCompatibilityTests
{
private static readonly List<Type> KnownValidationAttributes =
[
typeof(AllowedValuesAttribute),
typeof(DeniedValuesAttribute),
typeof(Base64StringAttribute),
typeof(LengthAttribute),

// netfx:
typeof(CompareAttribute),
typeof(CustomValidationAttribute),

typeof(DataTypeAttribute),
typeof(EnumDataTypeAttribute),
typeof(CreditCardAttribute),
typeof(EmailAddressAttribute),
typeof(FileExtensionsAttribute),
typeof(PhoneAttribute),
typeof(UrlAttribute),

typeof(MaxLengthAttribute),
typeof(MinLengthAttribute),

typeof(RangeAttribute),
typeof(RegularExpressionAttribute),
typeof(RequiredAttribute),

typeof(StringLengthAttribute)
];

/// <summary>
/// If we update to a new version of .net, we should check if any new validation attributes were
/// added to the framework and add deal with their localization accordingly.
/// </summary>
[Test]
public void No_New_Attributes_Were_Added()
{
var validationAttributeType = typeof(ValidationAttribute);
var systemComponentModelDataAnnotations = validationAttributeType.Assembly;
var allValidationAttributes = systemComponentModelDataAnnotations
.GetTypes()
.Where(t => validationAttributeType.IsAssignableFrom(t) && !t.IsAbstract)
.ToList();

var unknownAttributes = allValidationAttributes
.Where(a => !KnownValidationAttributes.Contains(a))
.ToList();

using (Assert.EnterMultipleScope())
{
Assert.That(allValidationAttributes, Has.Count.GreaterThan(0));
Assert.That(KnownValidationAttributes, Has.Count.GreaterThan(0));
Assert.That(KnownValidationAttributes,
Has.Count.LessThanOrEqualTo(allValidationAttributes.Count));
}

if (unknownAttributes.Count != 0)
{
Assert.Fail(
$"The following ValidationAttribute subclasses were not included in " +
$"the list of known attributes: " +
$"{string.Join(", ", unknownAttributes.Select(a => a.FullName))}");
}
}

[Test]
public void ResourceManager_Keys_Match()
{
var localizedRm = I18nLocalizedDataAnnotations.ResourceManager;
var defaultRm = LocalizedValidatorReflectionHack.GetCurrentResourceManager();

var localizedRs = localizedRm.GetResourceSet(CultureInfo.CurrentCulture, true, true)!;
var defaultRs = defaultRm.GetResourceSet(CultureInfo.CurrentCulture, true, true)!;

List<string> localizedKeys =
localizedRs.Cast<DictionaryEntry>().Select(e => (string)e.Key!).ToList();
List<string> defaultKeys =
defaultRs.Cast<DictionaryEntry>().Select(e => (string)e.Key!).ToList();

using (Assert.EnterMultipleScope())
{
Assert.That(localizedKeys, Has.Count.GreaterThan(0));
Assert.That(defaultKeys, Has.Count.GreaterThan(0));
Assert.That(defaultKeys, Has.Count.LessThanOrEqualTo(localizedKeys.Count),
"The default ResourceManager should not have more keys than the localized one.");

foreach (string key in defaultKeys)
{
Assert.That(localizedKeys, Contains.Item(key),
$"Key '{key}' is present in the default ResourceManager " +
$"but not in the localized one.");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="NUnit" />
<PackageReference Include="NUnit.Analyzers" />
<PackageReference Include="NUnit3TestAdapter" />
</ItemGroup>

<ItemGroup>
<Using Include="NUnit.Framework"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Dmnk.LocalizedDataAnnotations\Dmnk.LocalizedDataAnnotations.csproj" />
</ItemGroup>

</Project>
Loading
Loading