| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ValidationLanguageManager.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Globalization; |
| | | 9 | | using FluentValidation.Resources; |
| | | 10 | | using MyNet.Globalization.Culture; |
| | | 11 | | using MyNet.Observable.Localization; |
| | | 12 | | |
| | | 13 | | namespace MyNet.Observable.Validation; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// FluentValidation <see cref="LanguageManager"/> backed by <see cref="ValidationResources"/>. |
| | | 17 | | /// </summary> |
| | | 18 | | public sealed class ValidationLanguageManager : LanguageManager |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// Initializes a new instance of the <see cref="ValidationLanguageManager"/> class. |
| | | 22 | | /// </summary> |
| | 9 | 23 | | public ValidationLanguageManager() |
| | | 24 | | { |
| | 9 | 25 | | RegisterCulture(SupportedCultures.English.TwoLetterISOLanguageName, SupportedCultures.English); |
| | 9 | 26 | | RegisterCulture(SupportedCultures.French.TwoLetterISOLanguageName, SupportedCultures.French); |
| | 9 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Gets the localized string for the specified resource name and culture. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="resourceName">The name of the resource.</param> |
| | | 33 | | /// <param name="culture">The culture for which to retrieve the resource.</param> |
| | | 34 | | /// <returns>The localized string.</returns> |
| | 198 | 35 | | private static new string GetString(string resourceName, CultureInfo culture) => ValidationResources.ResourceManager |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Adapts a resource template by replacing the standard FluentValidation placeholders with the ones expected by the |
| | | 39 | | /// </summary> |
| | | 40 | | /// <param name="resourceTemplate">The resource template to adapt.</param> |
| | | 41 | | /// <param name="placeholderReplacements">An array of placeholder replacements, where each pair of elements represen |
| | | 42 | | /// <returns>The adapted resource template.</returns> |
| | | 43 | | private static string AdaptTemplate(string resourceTemplate, params string[] placeholderReplacements) |
| | | 44 | | { |
| | 198 | 45 | | var message = resourceTemplate.Replace("{0}", "{PropertyName}", StringComparison.Ordinal); |
| | | 46 | | |
| | 612 | 47 | | for (var i = 0; i < placeholderReplacements.Length; i += 2) |
| | | 48 | | { |
| | 108 | 49 | | message = message.Replace(placeholderReplacements[i], placeholderReplacements[i + 1], StringComparison.Ordin |
| | | 50 | | } |
| | | 51 | | |
| | 198 | 52 | | return message; |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Registers the validation messages for a specific language code and resource culture. This method maps FluentVali |
| | | 57 | | /// </summary> |
| | | 58 | | /// <param name="languageCode">The language code for which to register the validation messages.</param> |
| | | 59 | | /// <param name="resourceCulture">The culture of the resource from which to retrieve the validation messages.</param |
| | | 60 | | private void RegisterCulture(string languageCode, CultureInfo resourceCulture) |
| | | 61 | | { |
| | 18 | 62 | | Register(languageCode, "NotNullValidator", GetString(nameof(ValidationResources.FieldIsRequired), resourceCultur |
| | 18 | 63 | | Register(languageCode, "NotEmptyValidator", GetString(nameof(ValidationResources.FieldIsRequired), resourceCultu |
| | 18 | 64 | | Register(languageCode, "EmailValidator", GetString(nameof(ValidationResources.FieldInvalidEmail), resourceCultur |
| | 18 | 65 | | Register(languageCode, "MaximumLengthValidator", GetString(nameof(ValidationResources.FieldExceedsMaxLength), re |
| | 18 | 66 | | Register(languageCode, "MaximumLength_Simple", GetString(nameof(ValidationResources.FieldExceedsMaxLength), reso |
| | 18 | 67 | | Register(languageCode, "InclusiveBetweenValidator", GetString(nameof(ValidationResources.FieldMustBeBetween), re |
| | 18 | 68 | | Register(languageCode, "InclusiveBetween_Simple", GetString(nameof(ValidationResources.FieldMustBeBetween), reso |
| | 18 | 69 | | Register(languageCode, "GreaterThanOrEqualValidator", GetString(nameof(ValidationResources.FieldMustBeGreaterTha |
| | 18 | 70 | | Register(languageCode, "LessThanOrEqualValidator", GetString(nameof(ValidationResources.FieldMustBeLessThanOrEqu |
| | 18 | 71 | | Register(languageCode, "GreaterThanValidator", GetString(nameof(ValidationResources.FieldMustBeGreaterThan), res |
| | 18 | 72 | | Register(languageCode, "LessThanValidator", GetString(nameof(ValidationResources.FieldMustBeLessThan), resourceC |
| | 18 | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Registers a validation message for a specific language code and validator key, using the provided resource templ |
| | | 77 | | /// </summary> |
| | | 78 | | /// <param name="languageCode">The language code for which to register the validation message.</param> |
| | | 79 | | /// <param name="validatorKey">The key of the validator for which to register the message.</param> |
| | | 80 | | /// <param name="resourceTemplate">The resource template to use for the validation message.</param> |
| | | 81 | | /// <param name="placeholderReplacements">An array of placeholder replacements, where each pair of elements represen |
| | | 82 | | private void Register(string languageCode, string validatorKey, string resourceTemplate, params string[] placeholder |
| | | 83 | | { |
| | 198 | 84 | | var message = AdaptTemplate(resourceTemplate, placeholderReplacements); |
| | 198 | 85 | | AddTranslation(languageCode, validatorKey, message); |
| | 198 | 86 | | } |
| | | 87 | | } |
| | | 88 | | |