| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ValidationMessages.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 MyNet.Globalization.Facade; |
| | | 10 | | using MyNet.Observable.Localization; |
| | | 11 | | using MyNet.Primitives; |
| | | 12 | | |
| | | 13 | | namespace MyNet.Observable.Validation; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Formats localized validation messages from <see cref="ValidationResources"/>. |
| | | 17 | | /// </summary> |
| | | 18 | | public static class ValidationMessages |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// Formats a validation template using an already resolved property display name. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="template">The localized message template ({0} = field name).</param> |
| | | 24 | | /// <param name="propertyDisplayName">The resolved display name inserted at {0}.</param> |
| | | 25 | | /// <param name="args">Additional format arguments starting at {1}.</param> |
| | | 26 | | public static string Format(string template, string propertyDisplayName, params object?[] args) |
| | | 27 | | { |
| | 21 | 28 | | ArgumentException.ThrowIfNullOrEmpty(template); |
| | 21 | 29 | | ArgumentException.ThrowIfNullOrEmpty(propertyDisplayName); |
| | | 30 | | |
| | 21 | 31 | | var formatArgs = new object?[args.Length + 1]; |
| | 21 | 32 | | formatArgs[0] = propertyDisplayName; |
| | 21 | 33 | | args.CopyTo(formatArgs, 1); |
| | | 34 | | |
| | 21 | 35 | | return template.FormatWith(CultureInfo.CurrentCulture, formatArgs); |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Formats a validation template by translating the property key with the current culture. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <param name="template">The localized message template ({0} = field name).</param> |
| | | 42 | | /// <param name="propertyDisplayKey">The translation key for the property display name.</param> |
| | | 43 | | /// <param name="args">Additional format arguments starting at {1}.</param> |
| | | 44 | | public static string FormatForPropertyKey(string template, string propertyDisplayKey, params object?[] args) |
| | | 45 | | { |
| | 15 | 46 | | ArgumentException.ThrowIfNullOrEmpty(propertyDisplayKey); |
| | | 47 | | |
| | 15 | 48 | | var displayName = propertyDisplayKey.Translate(); |
| | 15 | 49 | | if (string.IsNullOrWhiteSpace(displayName)) |
| | 0 | 50 | | displayName = propertyDisplayKey; |
| | | 51 | | |
| | 15 | 52 | | return Format(template, displayName, args); |
| | | 53 | | } |
| | | 54 | | } |
| | | 55 | | |