< Summary

Information
Class: MyNet.Globalization.Localization.Translation.TranslationOptionsBuilder
Assembly: MyNet.Globalization
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Globalization/Localization/Translation/TranslationOptionsBuilder.cs
Tag: 323_28699572109
Line coverage
78%
Covered lines: 36
Uncovered lines: 10
Coverable lines: 46
Total lines: 177
Line coverage: 78.2%
Branch coverage
100%
Covered branches: 2
Total branches: 2
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
WithStyle(...)100%11100%
WithQuantity(...)100%11100%
PrefixWithQuantity(...)100%210%
SuffixWithQuantity(...)100%210%
WithArgument(...)100%11100%
WithArguments(...)100%22100%
UseInflectionFallback()100%11100%
WithoutInflectionFallback()100%11100%
UseKeyFallback()100%11100%
WithoutKeyFallback()100%11100%
Build()100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Globalization/Localization/Translation/TranslationOptionsBuilder.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="TranslationOptionsBuilder.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Generic;
 9using System.Collections.ObjectModel;
 10using MyNet.Text.Templating;
 11
 12namespace MyNet.Globalization.Localization.Translation;
 13
 14/// <summary>
 15/// Fluent builder used to create immutable <see cref="TranslationOptions"/> instances.
 16/// </summary>
 17public sealed class TranslationOptionsBuilder
 18{
 114919    private readonly Dictionary<string, object?> _arguments = new(StringComparer.OrdinalIgnoreCase);
 20    private DisplayStyle _style = DisplayStyle.Default;
 21    private decimal? _quantity;
 114922    private bool _useInflectionFallback = true;
 114923    private bool _useKeyAsFallback = true;
 114924    private QuantityRenderingMode _quantityRenderingMode = QuantityRenderingMode.PlaceholderOnly;
 114925    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    {
 53135        _style = style;
 53136        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    {
 70248        _quantity = count;
 70249        _quantityRenderingMode = quantityRenderingMode;
 50
 70251        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    {
 063        _quantity = count;
 064        _quantityRenderingMode = QuantityRenderingMode.Prefix;
 065        _quantitySeparator = quantitySeparator;
 066        _quantityFormat = quantityFormat;
 67
 068        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    {
 080        _quantity = count;
 081        _quantityRenderingMode = QuantityRenderingMode.Suffix;
 082        _quantitySeparator = quantitySeparator;
 083        _quantityFormat = quantityFormat;
 84
 085        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    {
 58896        ArgumentException.ThrowIfNullOrWhiteSpace(name);
 97
 57098        _arguments[name] = value;
 99
 570100        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    {
 15110        ArgumentNullException.ThrowIfNull(arguments);
 111
 42112        foreach (var argument in arguments)
 113        {
 12114            _arguments[argument.Key] = argument.Value;
 115        }
 116
 9117        return this;
 118    }
 119
 120    /// <summary>
 121    /// Enables inflection fallback.
 122    /// </summary>
 123    /// <returns>The current builder.</returns>
 124    public TranslationOptionsBuilder UseInflectionFallback()
 125    {
 6126        _useInflectionFallback = true;
 6127        return this;
 128    }
 129
 130    /// <summary>
 131    /// Disables inflection fallback.
 132    /// </summary>
 133    /// <returns>The current builder.</returns>
 134    public TranslationOptionsBuilder WithoutInflectionFallback()
 135    {
 12136        _useInflectionFallback = false;
 12137        return this;
 138    }
 139
 140    /// <summary>
 141    /// Enables key fallback.
 142    /// </summary>
 143    /// <returns>The current builder.</returns>
 144    public TranslationOptionsBuilder UseKeyFallback()
 145    {
 3146        _useKeyAsFallback = true;
 3147        return this;
 148    }
 149
 150    /// <summary>
 151    /// Disables key fallback.
 152    /// </summary>
 153    /// <returns>The current builder.</returns>
 154    public TranslationOptionsBuilder WithoutKeyFallback()
 155    {
 105156        _useKeyAsFallback = false;
 105157        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()
 1134165        => new()
 1134166        {
 1134167            Style = _style,
 1134168            Quantity = _quantity,
 1134169            QuantityFormat = _quantityFormat,
 1134170            QuantityRenderingMode = _quantityRenderingMode,
 1134171            QuantitySeparator = _quantitySeparator,
 1134172            Arguments = new ReadOnlyDictionary<string, object?>(new Dictionary<string, object?>(_arguments, StringCompar
 1134173            UseInflectionFallback = _useInflectionFallback,
 1134174            UseKeyAsFallback = _useKeyAsFallback
 1134175        };
 176}
 177