forked from OrchardCMS/OrchardCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support data annotations PO localization (OrchardCMS#4675)
- Loading branch information
Showing
6 changed files
with
134 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 4 additions & 18 deletions
22
src/OrchardCore.Modules/OrchardCore.Users/ViewModels/LoginViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,17 @@ | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Localization; | ||
|
||
namespace OrchardCore.Users.ViewModels | ||
{ | ||
public class LoginViewModel : IValidatableObject | ||
public class LoginViewModel | ||
{ | ||
[Required(ErrorMessage = "Username is required.")] | ||
[Display(Name = "Username")] | ||
public string UserName { get; set; } | ||
|
||
[Required(ErrorMessage = "Password is required.")] | ||
[DataType(DataType.Password)] | ||
public string Password { get; set; } | ||
|
||
public bool RememberMe { get; set; } | ||
|
||
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) | ||
{ | ||
var S = validationContext.GetService<IStringLocalizer<LoginViewModel>>(); | ||
if (string.IsNullOrWhiteSpace(UserName)) | ||
{ | ||
yield return new ValidationResult(S["Username is required."], new[] { "UserName" }); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(Password)) | ||
{ | ||
yield return new ValidationResult(S["Password is required."], new[] { "Password" }); | ||
} | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...dCore/OrchardCore.Localization.Core/DataAnnotations/LocalizedDataAnnotationsMvcOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Localization; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace OrchardCore.Localization.DataAnnotations | ||
{ | ||
internal class LocalizedDataAnnotationsMvcOptions : IConfigureOptions<MvcOptions> | ||
{ | ||
private readonly IStringLocalizerFactory _stringLocalizerFactory; | ||
|
||
public LocalizedDataAnnotationsMvcOptions(IStringLocalizerFactory stringLocalizerFactory) | ||
{ | ||
_stringLocalizerFactory = stringLocalizerFactory; | ||
} | ||
|
||
public void Configure(MvcOptions options) | ||
{ | ||
var localizer = _stringLocalizerFactory.Create(typeof(LocalizedDataAnnotationsMvcOptions)); | ||
|
||
options.ModelMetadataDetailsProviders.Add(new LocalizedValidationMetadataProvider(localizer)); | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...Core/OrchardCore.Localization.Core/DataAnnotations/LocalizedValidationMetadataProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata; | ||
using Microsoft.Extensions.Localization; | ||
|
||
namespace OrchardCore.Localization.DataAnnotations | ||
{ | ||
/// <summary> | ||
/// Provides a validation for a <see cref="DefaultModelMetadata"/>. | ||
/// </summary> | ||
public class LocalizedValidationMetadataProvider : IValidationMetadataProvider | ||
{ | ||
private readonly IStringLocalizer _stringLocalizer; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of a <see cref="LocalizedValidationMetadataProvider"/> with string localizer. | ||
/// </summary> | ||
/// <param name="stringLocalizer">The <see cref="IStringLocalizer"/>.</param> | ||
public LocalizedValidationMetadataProvider(IStringLocalizer stringLocalizer) | ||
{ | ||
_stringLocalizer = stringLocalizer; | ||
} | ||
|
||
/// <remarks>This will localize the default data annotations error message if it is exist, otherwise will try to look for a parameterized version.</remarks> | ||
/// <example> | ||
/// A property named 'UserName' that decorated with <see cref="RequiredAttribute"/> will be localized using | ||
/// "The {0} field is required." and "The UserName field is required." error messages. | ||
/// </example> | ||
public void CreateValidationMetadata(ValidationMetadataProviderContext context) | ||
{ | ||
foreach (var metadata in context.ValidationMetadata.ValidatorMetadata) | ||
{ | ||
if (metadata is ValidationAttribute attribute) | ||
{ | ||
var displayName = context.Attributes.OfType<DisplayAttribute>().FirstOrDefault()?.Name; | ||
// Use DisplayName if present | ||
var argument = displayName ?? context.Key.Name; | ||
var errorMessageString = attribute.ErrorMessage == null && attribute.ErrorMessageResourceName == null | ||
? attribute.FormatErrorMessage(argument) | ||
: attribute.ErrorMessage; | ||
|
||
// Localize the parameterized error message | ||
var localizedString = _stringLocalizer[errorMessageString]; | ||
|
||
if (localizedString == errorMessageString) | ||
{ | ||
// Localize the unparameterized error message | ||
var unparameterizedErrorMessage = errorMessageString.Replace(argument, "{0}"); | ||
localizedString = _stringLocalizer[unparameterizedErrorMessage]; | ||
} | ||
|
||
attribute.ErrorMessage = localizedString; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters