< Summary

Information
Class: MyNet.Globalization.Facade.TranslationExtensions
Assembly: MyNet.Globalization
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Globalization/Facade/Extensions/TranslationExtensions.cs
Tag: 323_28699572109
Line coverage
25%
Covered lines: 7
Uncovered lines: 21
Coverable lines: 28
Total lines: 191
Line coverage: 25%
Branch coverage
21%
Covered branches: 3
Total branches: 14
Branch coverage: 21.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Translate(...)100%11100%
Translate(...)100%210%
Translate(...)100%210%
Translate(...)100%210%
Translate(...)100%11100%
Translate(...)0%620%
TranslateDatePattern(...)0%4260%
Translate(...)100%210%
Translate(...)100%210%
Translate(...)100%210%
Translate(...)100%210%
TranslateOr(...)50%66100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Globalization/Facade/Extensions/TranslationExtensions.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="TranslationExtensions.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.Globalization;
 10using MyNet.Globalization.Localization.Translation;
 11
 12#pragma warning disable IDE0130 // Namespace does not match folder structure
 13namespace MyNet.Globalization.Facade;
 14#pragma warning restore IDE0130 // Namespace does not match folder structure
 15
 16public static class TranslationExtensions
 17{
 18    extension(string key)
 19    {
 20        /// <summary>
 21        /// Translates the key using the current culture.
 22        /// </summary>
 23        /// <remarks>
 24        /// This overload is a convenience facade that uses <see cref="Localizer"/>.
 25        /// Prefer explicit DI overloads when services are available.
 26        /// </remarks>
 27        /// <param name="culture">The culture to use for translation. If null, the current culture is used.</param>
 28        /// <returns>The translated string.</returns>
 15329        public string Translate(CultureInfo? culture = null) => key.Translate(TranslationOptionsPresets.Default, culture
 30
 31        /// <summary>
 32        /// Translates the key from a specific resource using the current culture. When null, the current culture is use
 33        /// </summary>
 34        /// <param name="resourceKey">The resource key to use for translation.</param>
 35        /// <param name="culture">The culture to use for translation. If null, the current culture is used.</param>
 36        /// <returns>The translated string.</returns>
 037        public string Translate(string resourceKey, CultureInfo? culture = null) => key.Translate(TranslationOptionsPres
 38
 39        /// <summary>
 40        /// Translates the key using the current culture with specified translation options. When null, the current cult
 41        /// </summary>
 42        /// <remarks>
 43        /// This overload is a convenience facade that uses <see cref="Localizer"/>.
 44        /// Prefer explicit DI overloads when services are available.
 45        /// </remarks>
 46        /// <param name="configure">A delegate to configure the translation options.</param>
 47        /// <param name="culture">The culture to use for translation. If null, the current culture is used.</param>
 48        /// <returns>The translated string.</returns>
 49        public string Translate(Action<TranslationOptionsBuilder> configure, CultureInfo? culture = null)
 50        {
 051            ArgumentNullException.ThrowIfNull(configure);
 52
 053            var builder = new TranslationOptionsBuilder();
 54
 055            configure(builder);
 56
 057            return key.Translate(builder.Build(), culture);
 58        }
 59
 60        /// <summary>
 61        /// Translates the key from a specific resource using the current culture. When null, the current culture is use
 62        /// </summary>
 63        /// <remarks>
 64        /// This overload is a convenience facade that uses <see cref="Localizer"/>.
 65        /// Prefer explicit DI overloads when services are available.
 66        /// </remarks>
 67        /// <param name="configure">A delegate to configure the translation options.</param>
 68        /// <param name="resourceKey">The resource key to use for translation.</param>
 69        /// <param name="culture">The culture to use for translation. If null, the current culture is used.</param>
 70        /// <returns>The translated string.</returns>
 71        public string Translate(Action<TranslationOptionsBuilder> configure, string resourceKey, CultureInfo? culture = 
 72        {
 073            ArgumentNullException.ThrowIfNull(configure);
 74
 075            var builder = new TranslationOptionsBuilder();
 76
 077            configure(builder);
 78
 079            return key.Translate(builder.Build(), resourceKey, culture);
 80        }
 81
 82        /// <summary>
 83        /// Translates the key using the current culture with specified translation options. When null, the current cult
 84        /// </summary>
 85        /// <remarks>
 86        /// This overload is a convenience facade that uses <see cref="Localizer"/>.
 87        /// Prefer explicit DI overloads when services are available.
 88        /// </remarks>
 89        /// <param name="options">The translation options to use.</param>
 90        /// <param name="culture">The culture to use for translation. If null, the current culture is used.</param>
 91        /// <returns>The translated string.</returns>
 15392        public string Translate(TranslationOptions options, CultureInfo? culture = null) => Localizer.Translation.Transl
 93
 94        /// <summary>
 95        /// Translates the key from a specific resource using the current culture. When null, the current culture is use
 96        /// </summary>
 97        /// <remarks>
 98        /// This overload is a convenience facade that uses <see cref="Localizer"/>.
 99        /// Prefer explicit DI overloads when services are available.
 100        /// </remarks>
 101        /// <param name="options">The translation options to use.</param>
 102        /// <param name="resourceKey">The resource key to use for translation.</param>
 103        /// <param name="culture">The culture to use for translation. If null, the current culture is used.</param>
 104        /// <returns>The translated string.</returns>
 105        public string Translate(TranslationOptions options, string resourceKey, CultureInfo? culture = null)
 0106            => culture == null
 0107                ? Localizer.Translation.Translate(key, options, resourceKey)
 0108                : Localizer.Translation.Translate(key, options, resourceKey, culture);
 109
 110        /// <summary>
 111        /// Translates the key as a date pattern for the specified culture. It first attempts to retrieve the date patte
 112        /// </summary>
 113        /// <remarks>
 114        /// This overload is a convenience facade that uses <see cref="Localizer"/>.
 115        /// Prefer explicit DI overloads when services are available.
 116        /// </remarks>
 117        /// <param name="culture">The culture to use for translation. If null, the current culture is used.</param>
 118        /// <returns>The translated date pattern.</returns>
 119        public string TranslateDatePattern(CultureInfo? culture = null)
 120        {
 0121            ArgumentException.ThrowIfNullOrEmpty(key);
 122
 0123            var effectiveCulture = culture.OrContext();
 0124            var format = effectiveCulture.DateTimeFormat;
 0125            var prop = format.GetType().GetProperty(key);
 0126            return prop is not null ? prop.GetValue(format)?.ToString() ?? string.Empty : key.Translate(culture);
 127        }
 128
 129        /// <summary>
 130        /// Translates using the specified display style and culture.
 131        /// </summary>
 132        /// <remarks>
 133        /// This overload is a convenience facade that uses <see cref="Localizer"/>.
 134        /// Prefer explicit DI overloads when services are available.
 135        /// </remarks>
 136        /// <param name="style">The display style.</param>
 137        /// <param name="culture">The target culture.</param>
 138        /// <returns>The translated string.</returns>
 0139        public string Translate(DisplayStyle style, CultureInfo? culture = null) => key.Translate(x => x.WithStyle(style
 140
 141        /// <summary>
 142        /// Translates using pluralization count and culture.
 143        /// </summary>
 144        /// <remarks>
 145        /// This overload is a convenience facade that uses <see cref="Localizer"/>.
 146        /// Prefer explicit DI overloads when services are available.
 147        /// </remarks>
 148        /// <param name="count">The pluralization count.</param>
 149        /// <param name="culture">The target culture.</param>
 150        /// <returns>The translated string.</returns>
 0151        public string Translate(decimal count, CultureInfo culture) => key.Translate(x => x.WithQuantity(count), culture
 152
 153        /// <summary>
 154        /// Translates using pluralization count, display style and culture.
 155        /// </summary>
 156        /// <remarks>
 157        /// This overload is a convenience facade that uses <see cref="Localizer"/>.
 158        /// Prefer explicit DI overloads when services are available.
 159        /// </remarks>
 160        /// <param name="count">The pluralization count.</param>
 161        /// <param name="style">The display style.</param>
 162        /// <param name="culture">The target culture.</param>
 163        /// <returns>The translated string.</returns>
 0164        public string Translate(decimal count, DisplayStyle style, CultureInfo culture) => key.Translate(x => x.WithStyl
 165
 166        /// <summary>
 167        /// Translates using custom rendering arguments.
 168        /// </summary>
 169        /// <remarks>
 170        /// This overload is a convenience facade that uses <see cref="Localizer"/>.
 171        /// Prefer explicit DI overloads when services are available.
 172        /// </remarks>
 173        /// <param name="arguments">The rendering arguments.</param>
 174        /// <returns>The translated string.</returns>
 0175        public string Translate(params KeyValuePair<string, object?>[] arguments) => key.Translate(x => x.WithArguments(
 176
 177        /// <summary>
 178        /// Translates when <paramref name="key"/> is set; otherwise returns <paramref name="fallbackText"/> or empty.
 179        /// </summary>
 180        public string TranslateOr(
 181            string? fallbackText = null,
 182            string? resourceFilename = null,
 183            CultureInfo? culture = null)
 6184            => !string.IsNullOrEmpty(key)
 6185                ? string.IsNullOrEmpty(resourceFilename)
 6186                    ? key.Translate(culture)
 6187                    : key.Translate(resourceFilename, culture)
 6188                : fallbackText ?? string.Empty;
 189    }
 190}
 191