| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="CultureExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Globalization; |
| | | 9 | | using MyNet.Globalization.Localization.Translation; |
| | | 10 | | using MyNet.Primitives; |
| | | 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 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 |
| | 0 | 28 | | 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> |
| | 0 | 42 | | 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> |
| | 0 | 54 | | 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 | | { |
| | 0 | 68 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 69 | | |
| | 0 | 70 | | var builder = new TranslationOptionsBuilder(); |
| | | 71 | | |
| | 0 | 72 | | configure(builder); |
| | | 73 | | |
| | 0 | 74 | | 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 | | { |
| | 0 | 90 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 91 | | |
| | 0 | 92 | | var builder = new TranslationOptionsBuilder(); |
| | | 93 | | |
| | 0 | 94 | | configure(builder); |
| | | 95 | | |
| | 0 | 96 | | 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> |
| | 0 | 109 | | 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> |
| | 0 | 122 | | public string Translate(string key, TranslationOptions options, string resourceKey) => Localizer.Translation.Tra |
| | | 123 | | } |
| | | 124 | | } |
| | | 125 | | |