| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="TranslationOptionsBuilder.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.Generic; |
| | | 9 | | using System.Collections.ObjectModel; |
| | | 10 | | using MyNet.Text.Templating; |
| | | 11 | | |
| | | 12 | | namespace MyNet.Globalization.Localization.Translation; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Fluent builder used to create immutable <see cref="TranslationOptions"/> instances. |
| | | 16 | | /// </summary> |
| | | 17 | | public sealed class TranslationOptionsBuilder |
| | | 18 | | { |
| | 1149 | 19 | | private readonly Dictionary<string, object?> _arguments = new(StringComparer.OrdinalIgnoreCase); |
| | | 20 | | private DisplayStyle _style = DisplayStyle.Default; |
| | | 21 | | private decimal? _quantity; |
| | 1149 | 22 | | private bool _useInflectionFallback = true; |
| | 1149 | 23 | | private bool _useKeyAsFallback = true; |
| | 1149 | 24 | | private QuantityRenderingMode _quantityRenderingMode = QuantityRenderingMode.PlaceholderOnly; |
| | 1149 | 25 | | private string _quantitySeparator = " "; |
| | | 26 | | private string? _quantityFormat; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Sets the display style. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="style">The display style.</param> |
| | | 32 | | /// <returns>The current builder.</returns> |
| | | 33 | | public TranslationOptionsBuilder WithStyle(DisplayStyle style) |
| | | 34 | | { |
| | 531 | 35 | | _style = style; |
| | 531 | 36 | | return this; |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Sets the count used for pluralization and rendering. |
| | | 41 | | /// Automatically exposes the "count" rendering argument. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="count">The count value.</param> |
| | | 44 | | /// <param name="quantityRenderingMode">The mode for rendering quantity information.</param> |
| | | 45 | | /// <returns>The current builder.</returns> |
| | | 46 | | public TranslationOptionsBuilder WithQuantity(decimal count, QuantityRenderingMode quantityRenderingMode = QuantityR |
| | | 47 | | { |
| | 702 | 48 | | _quantity = count; |
| | 702 | 49 | | _quantityRenderingMode = quantityRenderingMode; |
| | | 50 | | |
| | 702 | 51 | | return this; |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Sets the count used for pluralization and rendering, with the quantity value rendered as a prefix to the transla |
| | | 56 | | /// </summary> |
| | | 57 | | /// <param name="count">The count value.</param> |
| | | 58 | | /// <param name="quantityFormat">The format string to use when rendering the quantity value.</param> |
| | | 59 | | /// <param name="quantitySeparator">The separator to use between the quantity value and its unit.</param> |
| | | 60 | | /// <returns>The current builder.</returns> |
| | | 61 | | public TranslationOptionsBuilder PrefixWithQuantity(decimal count, string? quantityFormat = null, string quantitySep |
| | | 62 | | { |
| | 0 | 63 | | _quantity = count; |
| | 0 | 64 | | _quantityRenderingMode = QuantityRenderingMode.Prefix; |
| | 0 | 65 | | _quantitySeparator = quantitySeparator; |
| | 0 | 66 | | _quantityFormat = quantityFormat; |
| | | 67 | | |
| | 0 | 68 | | return this; |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Sets the count used for pluralization and rendering, with the quantity value rendered as a suffix to the transla |
| | | 73 | | /// </summary> |
| | | 74 | | /// <param name="count">The count value.</param> |
| | | 75 | | /// <param name="quantityFormat">The format string to use when rendering the quantity value.</param> |
| | | 76 | | /// <param name="quantitySeparator">The separator to use between the quantity value and its unit.</param> |
| | | 77 | | /// <returns>The current builder.</returns> |
| | | 78 | | public TranslationOptionsBuilder SuffixWithQuantity(decimal count, string? quantityFormat = null, string quantitySep |
| | | 79 | | { |
| | 0 | 80 | | _quantity = count; |
| | 0 | 81 | | _quantityRenderingMode = QuantityRenderingMode.Suffix; |
| | 0 | 82 | | _quantitySeparator = quantitySeparator; |
| | 0 | 83 | | _quantityFormat = quantityFormat; |
| | | 84 | | |
| | 0 | 85 | | return this; |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Adds or replaces a rendering argument. |
| | | 90 | | /// </summary> |
| | | 91 | | /// <param name="name">The argument name.</param> |
| | | 92 | | /// <param name="value">The argument value.</param> |
| | | 93 | | /// <returns>The current builder.</returns> |
| | | 94 | | public TranslationOptionsBuilder WithArgument(string name, object? value) |
| | | 95 | | { |
| | 588 | 96 | | ArgumentException.ThrowIfNullOrWhiteSpace(name); |
| | | 97 | | |
| | 570 | 98 | | _arguments[name] = value; |
| | | 99 | | |
| | 570 | 100 | | return this; |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Adds multiple rendering arguments. |
| | | 105 | | /// </summary> |
| | | 106 | | /// <param name="arguments">The arguments to add.</param> |
| | | 107 | | /// <returns>The current builder.</returns> |
| | | 108 | | public TranslationOptionsBuilder WithArguments(IEnumerable<KeyValuePair<string, object?>> arguments) |
| | | 109 | | { |
| | 15 | 110 | | ArgumentNullException.ThrowIfNull(arguments); |
| | | 111 | | |
| | 42 | 112 | | foreach (var argument in arguments) |
| | | 113 | | { |
| | 12 | 114 | | _arguments[argument.Key] = argument.Value; |
| | | 115 | | } |
| | | 116 | | |
| | 9 | 117 | | return this; |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Enables inflection fallback. |
| | | 122 | | /// </summary> |
| | | 123 | | /// <returns>The current builder.</returns> |
| | | 124 | | public TranslationOptionsBuilder UseInflectionFallback() |
| | | 125 | | { |
| | 6 | 126 | | _useInflectionFallback = true; |
| | 6 | 127 | | return this; |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// Disables inflection fallback. |
| | | 132 | | /// </summary> |
| | | 133 | | /// <returns>The current builder.</returns> |
| | | 134 | | public TranslationOptionsBuilder WithoutInflectionFallback() |
| | | 135 | | { |
| | 12 | 136 | | _useInflectionFallback = false; |
| | 12 | 137 | | return this; |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Enables key fallback. |
| | | 142 | | /// </summary> |
| | | 143 | | /// <returns>The current builder.</returns> |
| | | 144 | | public TranslationOptionsBuilder UseKeyFallback() |
| | | 145 | | { |
| | 3 | 146 | | _useKeyAsFallback = true; |
| | 3 | 147 | | return this; |
| | | 148 | | } |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Disables key fallback. |
| | | 152 | | /// </summary> |
| | | 153 | | /// <returns>The current builder.</returns> |
| | | 154 | | public TranslationOptionsBuilder WithoutKeyFallback() |
| | | 155 | | { |
| | 105 | 156 | | _useKeyAsFallback = false; |
| | 105 | 157 | | return this; |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | /// <summary> |
| | | 161 | | /// Builds an immutable <see cref="TranslationOptions"/> instance. |
| | | 162 | | /// </summary> |
| | | 163 | | /// <returns>The created translation options.</returns> |
| | | 164 | | public TranslationOptions Build() |
| | 1134 | 165 | | => new() |
| | 1134 | 166 | | { |
| | 1134 | 167 | | Style = _style, |
| | 1134 | 168 | | Quantity = _quantity, |
| | 1134 | 169 | | QuantityFormat = _quantityFormat, |
| | 1134 | 170 | | QuantityRenderingMode = _quantityRenderingMode, |
| | 1134 | 171 | | QuantitySeparator = _quantitySeparator, |
| | 1134 | 172 | | Arguments = new ReadOnlyDictionary<string, object?>(new Dictionary<string, object?>(_arguments, StringCompar |
| | 1134 | 173 | | UseInflectionFallback = _useInflectionFallback, |
| | 1134 | 174 | | UseKeyAsFallback = _useKeyAsFallback |
| | 1134 | 175 | | }; |
| | | 176 | | } |
| | | 177 | | |