< Summary

Information
Class: MyNet.Globalization.Facade.CultureExtensions
Assembly: MyNet.Globalization
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Globalization/Facade/Extensions/CultureExtensions.cs
Tag: 323_28699572109
Line coverage
0%
Covered lines: 0
Uncovered lines: 13
Coverable lines: 13
Total lines: 125
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
OrContext(...)100%210%
Translate(...)100%210%
Translate(...)100%210%
Translate(...)100%210%
Translate(...)100%210%
Translate(...)100%210%
Translate(...)100%210%

File(s)

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

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="CultureExtensions.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Globalization;
 9using MyNet.Globalization.Localization.Translation;
 10using MyNet.Primitives;
 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 CultureExtensions
 17{
 18    extension(CultureInfo? culture)
 19    {
 20        /// <summary>
 21        /// Returns the current culture from the globalization services if <paramref name="culture"/> is null, otherwise
 22        /// </summary>
 23        /// <remarks>
 24        /// This overload is a convenience facade that uses <see cref="GlobalizationServices"/>.
 25        /// Prefer explicit DI overloads when services are available.
 26        /// </remarks>
 27        /// <returns>The current culture from the globalization services if <paramref name="culture"/> is null, otherwis
 028        public CultureInfo OrContext() => culture.Or(GlobalizationServices.Current.CurrentCulture);
 29    }
 30
 31    extension(CultureInfo culture)
 32    {
 33        /// <summary>
 34        /// Translates the key using the current culture.
 35        /// </summary>
 36        /// <remarks>
 37        /// This overload is a convenience facade that uses <see cref="Localizer"/>.
 38        /// Prefer explicit DI overloads when services are available.
 39        /// </remarks>
 40        /// <param name="key">The key to translate.</param>
 41        /// <returns>The translated string.</returns>
 042        public string Translate(string key) => Localizer.Translation.Translate(key, TranslationOptionsPresets.Default, c
 43
 44        /// <summary>
 45        /// Translates the key from a specific resource using the current culture. When null, the current culture is use
 46        /// </summary>
 47        /// <remarks>
 48        /// This overload is a convenience facade that uses <see cref="Localizer"/>.
 49        /// Prefer explicit DI overloads when services are available.
 50        /// </remarks>
 51        /// <param name="key">The key to translate.</param>
 52        /// <param name="resourceKey">The resource key to use for translation.</param>
 53        /// <returns>The translated string.</returns>
 054        public string Translate(string key, string resourceKey) => Localizer.Translation.Translate(key, TranslationOptio
 55
 56        /// <summary>
 57        /// Translates the key using the current culture with specified translation options. When null, the current cult
 58        /// </summary>
 59        /// <remarks>
 60        /// This overload is a convenience facade that uses <see cref="Localizer"/>.
 61        /// Prefer explicit DI overloads when services are available.
 62        /// </remarks>
 63        /// <param name="key">The key to translate.</param>
 64        /// <param name="configure">A delegate to configure the translation options.</param>
 65        /// <returns>The translated string.</returns>
 66        public string Translate(string key, Action<TranslationOptionsBuilder> configure)
 67        {
 068            ArgumentNullException.ThrowIfNull(configure);
 69
 070            var builder = new TranslationOptionsBuilder();
 71
 072            configure(builder);
 73
 074            return Localizer.Translation.Translate(key, builder.Build(), culture);
 75        }
 76
 77        /// <summary>
 78        /// Translates the key from a specific resource using the current culture. When null, the current culture is use
 79        /// </summary>
 80        /// <remarks>
 81        /// This overload is a convenience facade that uses <see cref="Localizer"/>.
 82        /// Prefer explicit DI overloads when services are available.
 83        /// </remarks>
 84        /// <param name="key">The key to translate.</param>
 85        /// <param name="configure">A delegate to configure the translation options.</param>
 86        /// <param name="resourceKey">The resource key to use for translation.</param>
 87        /// <returns>The translated string.</returns>
 88        public string Translate(string key, Action<TranslationOptionsBuilder> configure, string resourceKey)
 89        {
 090            ArgumentNullException.ThrowIfNull(configure);
 91
 092            var builder = new TranslationOptionsBuilder();
 93
 094            configure(builder);
 95
 096            return Localizer.Translation.Translate(key, builder.Build(), resourceKey, culture);
 97        }
 98
 99        /// <summary>
 100        /// Translates the key using the current culture with specified translation options. When null, the current cult
 101        /// </summary>
 102        /// <remarks>
 103        /// This overload is a convenience facade that uses <see cref="Localizer"/>.
 104        /// Prefer explicit DI overloads when services are available.
 105        /// </remarks>
 106        /// <param name="key">The key to translate.</param>
 107        /// <param name="options">The translation options to use.</param>
 108        /// <returns>The translated string.</returns>
 0109        public string Translate(string key, TranslationOptions options) => Localizer.Translation.Translate(key, options,
 110
 111        /// <summary>
 112        /// Translates the key from a specific resource using the current culture. When null, the current culture is use
 113        /// </summary>
 114        /// <remarks>
 115        /// This overload is a convenience facade that uses <see cref="Localizer"/>.
 116        /// Prefer explicit DI overloads when services are available.
 117        /// </remarks>
 118        /// <param name="key">The key to translate.</param>
 119        /// <param name="options">The translation options to use.</param>
 120        /// <param name="resourceKey">The resource key to use for translation.</param>
 121        /// <returns>The translated string.</returns>
 0122        public string Translate(string key, TranslationOptions options, string resourceKey) => Localizer.Translation.Tra
 123    }
 124}
 125