// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using osu.Framework.Configuration; #nullable enable namespace osu.Framework.Localisation { /// /// A string that has a romanised fallback to allow a better experience for users that potentially can't read the original script. /// See , which can toggle the display of romanised variants. /// public class RomanisableString : IEquatable, ILocalisableStringData { /// /// The string in its original script. May be null. /// public readonly string? Original; /// /// The romanised version of the string. May be null. /// public readonly string? Romanised; /// /// Construct a new romanisable string. /// /// /// For flexibility, both of the provided strings are allowed to be null. If both are null, the returned string value from will be . /// /// The string in its original script. If null, the version will always be used. /// The romanised version of the string. If null, the version will always be used. public RomanisableString(string? original, string? romanised) { Original = original; Romanised = romanised; } public string GetLocalised(LocalisationParameters parameters) => GetPreferred(parameters.PreferOriginalScript); /// /// Get the best match for this string based on a user preference for which should be displayed. /// /// Whether to prefer the unicode (aka original) version where available. /// The best match for the provided criteria. public string GetPreferred(bool preferUnicode) { if (string.IsNullOrEmpty(Romanised)) return Original ?? string.Empty; if (string.IsNullOrEmpty(Original)) return Romanised ?? string.Empty; return preferUnicode ? Original : Romanised; } public override string ToString() => GetLocalised(new LocalisationParameters(null, false)); public bool Equals(RomanisableString? other) { if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; return Original == other.Original && Romanised == other.Romanised; } public bool Equals(ILocalisableStringData? other) => other is RomanisableString romanisable && Equals(romanisable); public override bool Equals(object? obj) => obj is RomanisableString romanisable && Equals(romanisable); public override int GetHashCode() { return HashCode.Combine(Original, Romanised); } } }