// 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 System.Globalization; #nullable enable namespace osu.Framework.Localisation { /// /// A string which can apply case transformations to underlying localisable string. /// public class CaseTransformableString : IEquatable, ILocalisableStringData { /// /// The case to apply to the underlying data. /// public readonly Casing Casing; /// /// The underlying localisable string of this transformable string. /// public readonly LocalisableString String; /// /// Constructs a new transformable string with specified underlying localisable string and casing. /// /// The localisable string to apply case transformations on. /// The casing to use on the localisable string. public CaseTransformableString(LocalisableString str, Casing casing) { String = str; Casing = casing; } public string GetLocalised(LocalisationParameters parameters) { var stringData = getStringData(parameters); var cultureText = parameters.Store?.EffectiveCulture?.TextInfo ?? CultureInfo.InvariantCulture.TextInfo; switch (Casing) { case Casing.UpperCase: return cultureText.ToUpper(stringData); case Casing.TitleCase: return cultureText.ToTitleCase(stringData); case Casing.LowerCase: return cultureText.ToLower(stringData); case Casing.Default: default: return stringData; } } public override string ToString() => GetLocalised(new LocalisationParameters(null, false)); public bool Equals(ILocalisableStringData? other) => other is CaseTransformableString transformable && Equals(transformable); public bool Equals(CaseTransformableString? other) { if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; return Casing == other.Casing && String.Equals(other.String); } private string getStringData(LocalisationParameters localisationParameters) { switch (String.Data) { case string plain: return plain; case ILocalisableStringData data: return data.GetLocalised(localisationParameters); default: return string.Empty; } } } /// /// Case applicable to the underlying localisable string of a . /// public enum Casing { /// /// Use the string data case. /// Default, /// /// Transform the string data to uppercase. /// UpperCase, /// /// Transform the string data to title case aka capitalized case /// TitleCase, /// /// Transform the string data to lowercase. /// LowerCase } }