| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using System.Globalization; |
| | | 3 | | |
| | | 4 | | namespace TeleFlow.Telegram.I18n; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Represents a validated, normalized locale used by Telegram update resolution and localization engines. |
| | | 8 | | /// The value keeps culture identity explicit without changing ambient process or thread cultures. |
| | | 9 | | /// </summary> |
| | | 10 | | public sealed class Locale : IEquatable<Locale> |
| | | 11 | | { |
| | | 12 | | public Locale(string name) |
| | | 13 | | { |
| | 347 | 14 | | ArgumentException.ThrowIfNullOrWhiteSpace(name); |
| | | 15 | | |
| | 347 | 16 | | var trimmedName = name.Trim(); |
| | | 17 | | |
| | 347 | 18 | | if (trimmedName.Contains('_', StringComparison.Ordinal)) |
| | | 19 | | { |
| | 3 | 20 | | throw new ArgumentException( |
| | 3 | 21 | | "Locale names must use BCP 47 '-' separators instead of '_'.", |
| | 3 | 22 | | nameof(name)); |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | try |
| | | 26 | | { |
| | 344 | 27 | | Culture = CultureInfo.GetCultureInfo(trimmedName); |
| | 344 | 28 | | } |
| | 0 | 29 | | catch (CultureNotFoundException exception) |
| | | 30 | | { |
| | 0 | 31 | | throw new ArgumentException($"'{name}' is not a valid locale name.", nameof(name), exception); |
| | | 32 | | } |
| | | 33 | | |
| | 344 | 34 | | if (string.IsNullOrEmpty(Culture.Name)) |
| | | 35 | | { |
| | 0 | 36 | | throw new ArgumentException("The invariant culture cannot be used as a locale.", nameof(name)); |
| | | 37 | | } |
| | | 38 | | |
| | 344 | 39 | | Name = Culture.Name; |
| | 344 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Gets the normalized culture name, for example <c>en</c> or <c>ru-RU</c>. |
| | | 44 | | /// </summary> |
| | | 45 | | public string Name { get; } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Gets the read-only .NET culture represented by this locale. |
| | | 49 | | /// </summary> |
| | | 50 | | public CultureInfo Culture { get; } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Attempts to create a locale without throwing for untrusted language codes such as Telegram user metadata. |
| | | 54 | | /// </summary> |
| | | 55 | | public static bool TryCreate(string? name, [NotNullWhen(true)] out Locale? locale) |
| | | 56 | | { |
| | 36 | 57 | | if (string.IsNullOrWhiteSpace(name)) |
| | | 58 | | { |
| | 1 | 59 | | locale = null; |
| | 1 | 60 | | return false; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | try |
| | | 64 | | { |
| | 35 | 65 | | locale = new Locale(name); |
| | 33 | 66 | | return true; |
| | | 67 | | } |
| | 2 | 68 | | catch (ArgumentException) |
| | | 69 | | { |
| | 2 | 70 | | locale = null; |
| | 2 | 71 | | return false; |
| | | 72 | | } |
| | 35 | 73 | | } |
| | | 74 | | |
| | | 75 | | public bool Equals(Locale? other) |
| | | 76 | | { |
| | 2 | 77 | | return other is not null && |
| | 2 | 78 | | string.Equals(Name, other.Name, StringComparison.OrdinalIgnoreCase); |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | public override bool Equals(object? obj) |
| | | 82 | | { |
| | 0 | 83 | | return obj is Locale other && Equals(other); |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | public override int GetHashCode() |
| | | 87 | | { |
| | 2 | 88 | | return StringComparer.OrdinalIgnoreCase.GetHashCode(Name); |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | public override string ToString() |
| | | 92 | | { |
| | 1 | 93 | | return Name; |
| | | 94 | | } |
| | | 95 | | } |