| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="LocalizedString.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using MyNet.Globalization.Culture; |
| | | 9 | | using MyNet.Globalization.Facade; |
| | | 10 | | using MyNet.Text; |
| | | 11 | | using MyNet.Text.TextCasing; |
| | | 12 | | |
| | | 13 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 14 | | namespace MyNet.Observable; |
| | | 15 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Culture-bound string resolved from a translation resource key. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <remarks> |
| | | 21 | | /// Initializes a new instance of the <see cref="LocalizedString"/> class with an explicit culture service. |
| | | 22 | | /// </remarks> |
| | | 23 | | /// <param name="key">The translation resource key.</param> |
| | | 24 | | /// <param name="cultureService">Culture service used to detect culture changes.</param> |
| | | 25 | | /// <param name="casing">Optional letter casing applied after translation.</param> |
| | | 26 | | /// <param name="filename">Optional resource file name.</param> |
| | 15 | 27 | | public class LocalizedString(string key, ICultureService cultureService, LetterCasing casing = LetterCasing.Normal, stri |
| | | 28 | | { |
| | | 29 | | /// <summary> |
| | | 30 | | /// Initializes a new instance of the <see cref="LocalizedString"/> class using <see cref="GlobalizationServices.Cur |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="key">The translation resource key.</param> |
| | | 33 | | /// <param name="casing">Optional letter casing applied after translation.</param> |
| | | 34 | | /// <param name="filename">Optional resource file name.</param> |
| | | 35 | | public LocalizedString(string key, LetterCasing casing = LetterCasing.Normal, string? filename = "") |
| | 24 | 36 | | : this(key, GlobalizationServices.Current, casing, filename) { } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Gets the translation resource key. |
| | | 40 | | /// </summary> |
| | 15 | 41 | | public string Key { get; } = key; |
| | | 42 | | |
| | | 43 | | /// <inheritdoc /> |
| | 3 | 44 | | public override bool Equals(object? obj) => obj is LocalizedString o && Key.Equals(o.Key, StringComparison.OrdinalIg |
| | | 45 | | |
| | | 46 | | /// <inheritdoc /> |
| | 6 | 47 | | public override int GetHashCode() => Key.GetHashCode(StringComparison.CurrentCultureIgnoreCase); |
| | | 48 | | |
| | | 49 | | private static string Resolve(string key, LetterCasing casing, string? filename) |
| | 6 | 50 | | => string.IsNullOrEmpty(key) |
| | 6 | 51 | | ? string.Empty |
| | 6 | 52 | | : string.IsNullOrEmpty(filename) |
| | 6 | 53 | | ? key.Translate().ApplyCase(casing) |
| | 6 | 54 | | : key.Translate(filename).ApplyCase(casing); |
| | | 55 | | } |
| | | 56 | | |