| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="TranslationExtensions.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.Globalization; |
| | | 10 | | using MyNet.Globalization.Localization.Translation; |
| | | 11 | | |
| | | 12 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 13 | | namespace MyNet.Globalization.Facade; |
| | | 14 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 15 | | |
| | | 16 | | public 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> |
| | 153 | 29 | | 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> |
| | 0 | 37 | | 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 | | { |
| | 0 | 51 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 52 | | |
| | 0 | 53 | | var builder = new TranslationOptionsBuilder(); |
| | | 54 | | |
| | 0 | 55 | | configure(builder); |
| | | 56 | | |
| | 0 | 57 | | 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 | | { |
| | 0 | 73 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 74 | | |
| | 0 | 75 | | var builder = new TranslationOptionsBuilder(); |
| | | 76 | | |
| | 0 | 77 | | configure(builder); |
| | | 78 | | |
| | 0 | 79 | | 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> |
| | 153 | 92 | | 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) |
| | 0 | 106 | | => culture == null |
| | 0 | 107 | | ? Localizer.Translation.Translate(key, options, resourceKey) |
| | 0 | 108 | | : 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 | | { |
| | 0 | 121 | | ArgumentException.ThrowIfNullOrEmpty(key); |
| | | 122 | | |
| | 0 | 123 | | var effectiveCulture = culture.OrContext(); |
| | 0 | 124 | | var format = effectiveCulture.DateTimeFormat; |
| | 0 | 125 | | var prop = format.GetType().GetProperty(key); |
| | 0 | 126 | | 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> |
| | 0 | 139 | | 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> |
| | 0 | 151 | | 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> |
| | 0 | 164 | | 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> |
| | 0 | 175 | | 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) |
| | 6 | 184 | | => !string.IsNullOrEmpty(key) |
| | 6 | 185 | | ? string.IsNullOrEmpty(resourceFilename) |
| | 6 | 186 | | ? key.Translate(culture) |
| | 6 | 187 | | : key.Translate(resourceFilename, culture) |
| | 6 | 188 | | : fallbackText ?? string.Empty; |
| | | 189 | | } |
| | | 190 | | } |
| | | 191 | | |