| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="TemplateTransform.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Concurrent; |
| | | 9 | | using System.Globalization; |
| | | 10 | | using System.Text.RegularExpressions; |
| | | 11 | | |
| | | 12 | | namespace MyNet.Text.Templating; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Current implementation of <see cref="ITemplateTransform"/> that supports named placeholders with optional format spe |
| | | 16 | | /// <example> |
| | | 17 | | /// {count} |
| | | 18 | | /// {count:N2} |
| | | 19 | | /// {price:C} |
| | | 20 | | /// {date:yyyy-MM-dd} |
| | | 21 | | /// </example> |
| | | 22 | | /// </summary> |
| | | 23 | | public sealed partial class TemplateTransform(TextTemplateOptions options) : ITemplateTransform |
| | | 24 | | { |
| | 9 | 25 | | private static readonly ConcurrentDictionary<string, Func<object?, CultureInfo, string?, string>> CachedFormatters = |
| | | 26 | | |
| | | 27 | | /// <inheritdoc /> |
| | | 28 | | public string Apply(string input, CultureInfo culture) |
| | | 29 | | { |
| | 4890 | 30 | | if (string.IsNullOrWhiteSpace(input)) |
| | 3 | 31 | | return string.Empty; |
| | | 32 | | |
| | 4887 | 33 | | ArgumentNullException.ThrowIfNull(options); |
| | 4884 | 34 | | ArgumentNullException.ThrowIfNull(culture); |
| | | 35 | | |
| | 4881 | 36 | | var rendered = PlaceholderRegex().Replace(input, match => |
| | 4881 | 37 | | { |
| | 4881 | 38 | | var argumentName = match.Groups["name"].Value; |
| | 4881 | 39 | | var format = match.Groups["format"].Success ? match.Groups["format"].Value : null; |
| | 4881 | 40 | | |
| | 4881 | 41 | | return !TryResolveArgument(argumentName, options, out var value) ? match.Value : FormatValue(value, culture, |
| | 4881 | 42 | | }); |
| | | 43 | | |
| | 4881 | 44 | | return ApplyQuantityRendering(rendered, options, culture); |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Tries to resolve an argument value by name from the provided translation options. |
| | | 49 | | /// </summary> |
| | | 50 | | /// <param name="argumentName">The name of the argument to resolve.</param> |
| | | 51 | | /// <param name="options">The translation options containing the arguments.</param> |
| | | 52 | | /// <param name="value">The resolved value, if found.</param> |
| | | 53 | | /// <returns>True if the argument was successfully resolved; otherwise, false.</returns> |
| | | 54 | | private static bool TryResolveArgument(string argumentName, TextTemplateOptions options, out object? value) |
| | | 55 | | { |
| | | 56 | | // Built-in count support |
| | 411 | 57 | | if (argumentName.Equals(nameof(TextTemplateOptions.Quantity), StringComparison.OrdinalIgnoreCase)) |
| | | 58 | | { |
| | 321 | 59 | | value = options.Quantity; |
| | 321 | 60 | | return options.Quantity.HasValue; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | // Custom arguments |
| | 90 | 64 | | if (options.Arguments.TryGetValue(argumentName, out value)) |
| | | 65 | | { |
| | 75 | 66 | | return true; |
| | | 67 | | } |
| | | 68 | | |
| | 15 | 69 | | value = null; |
| | 15 | 70 | | return false; |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Formats a value using the provided culture and optional format string. |
| | | 75 | | /// </summary> |
| | | 76 | | private static string FormatValue(object? value, CultureInfo culture, string? format) |
| | | 77 | | { |
| | 1293 | 78 | | if (value is null) |
| | | 79 | | { |
| | 3 | 80 | | return string.Empty; |
| | | 81 | | } |
| | | 82 | | |
| | 1290 | 83 | | var formatter = CachedFormatters.GetOrAdd( |
| | 1290 | 84 | | value.GetType().FullName + "|" + format, |
| | 1290 | 85 | | static _ => static (v, c, f) => v is null |
| | 1290 | 86 | | ? string.Empty |
| | 1290 | 87 | | : v switch |
| | 1290 | 88 | | { |
| | 1290 | 89 | | IFormattable formattable => formattable.ToString(f, c), |
| | 1290 | 90 | | _ => v.ToString() ?? string.Empty |
| | 1290 | 91 | | }); |
| | | 92 | | |
| | 1290 | 93 | | return formatter(value, culture, format); |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Applies quantity rendering to the rendered translation string based on the provided options and culture. This me |
| | | 98 | | /// </summary> |
| | | 99 | | /// <param name="rendered">The rendered translation string.</param> |
| | | 100 | | /// <param name="options">The translation options containing the quantity and rendering settings.</param> |
| | | 101 | | /// <param name="culture">The culture to use for formatting the quantity.</param> |
| | | 102 | | /// <returns>The translation string with quantity rendering applied, if applicable.</returns> |
| | | 103 | | private static string ApplyQuantityRendering(string rendered, TextTemplateOptions options, CultureInfo culture) |
| | | 104 | | { |
| | 4881 | 105 | | if (!options.Quantity.HasValue) |
| | 3984 | 106 | | return rendered; |
| | | 107 | | |
| | | 108 | | // Already handled by placeholder |
| | 897 | 109 | | if (rendered.Contains($"{{{{{nameof(TextTemplateOptions.Quantity)}", StringComparison.OrdinalIgnoreCase)) |
| | 0 | 110 | | return rendered; |
| | | 111 | | |
| | 897 | 112 | | var quantity = FormatValue(options.Quantity.Value, culture, options.QuantityFormat); |
| | | 113 | | |
| | 897 | 114 | | return options.QuantityRenderingMode switch |
| | 897 | 115 | | { |
| | 39 | 116 | | QuantityRenderingMode.Prefix => $"{quantity}{options.QuantitySeparator}{rendered}", |
| | 3 | 117 | | QuantityRenderingMode.Suffix => $"{rendered}{options.QuantitySeparator}{quantity}", |
| | 855 | 118 | | _ => rendered |
| | 897 | 119 | | }; |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Matches named placeholders with optional format specifier. |
| | | 124 | | /// Examples: |
| | | 125 | | /// {count} |
| | | 126 | | /// {count:N2} |
| | | 127 | | /// {price:C} |
| | | 128 | | /// </summary> |
| | | 129 | | [GeneratedRegex(@"\{(?<name>[a-zA-Z0-9_]+)(:(?<format>[^}]+))?\}", RegexOptions.Compiled | RegexOptions.CultureInvar |
| | | 130 | | private static partial Regex PlaceholderRegex(); |
| | | 131 | | } |
| | | 132 | | |