A game framework written with osu! in mind.
at master 3.6 kB view raw
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. 2// See the LICENCE file in the repository root for full licence text. 3 4using System; 5using System.Globalization; 6 7#nullable enable 8 9namespace osu.Framework.Localisation 10{ 11 /// <summary> 12 /// A string which can apply case transformations to underlying localisable string. 13 /// </summary> 14 public class CaseTransformableString : IEquatable<CaseTransformableString>, ILocalisableStringData 15 { 16 /// <summary> 17 /// The case to apply to the underlying data. 18 /// </summary> 19 public readonly Casing Casing; 20 21 /// <summary> 22 /// The underlying localisable string of this transformable string. 23 /// </summary> 24 public readonly LocalisableString String; 25 26 /// <summary> 27 /// Constructs a new transformable string with specified underlying localisable string and casing. 28 /// </summary> 29 /// <param name="str">The localisable string to apply case transformations on.</param> 30 /// <param name="casing">The casing to use on the localisable string.</param> 31 public CaseTransformableString(LocalisableString str, Casing casing) 32 { 33 String = str; 34 Casing = casing; 35 } 36 37 public string GetLocalised(LocalisationParameters parameters) 38 { 39 var stringData = getStringData(parameters); 40 var cultureText = parameters.Store?.EffectiveCulture?.TextInfo ?? CultureInfo.InvariantCulture.TextInfo; 41 42 switch (Casing) 43 { 44 case Casing.UpperCase: 45 return cultureText.ToUpper(stringData); 46 47 case Casing.TitleCase: 48 return cultureText.ToTitleCase(stringData); 49 50 case Casing.LowerCase: 51 return cultureText.ToLower(stringData); 52 53 case Casing.Default: 54 default: 55 return stringData; 56 } 57 } 58 59 public override string ToString() => GetLocalised(new LocalisationParameters(null, false)); 60 61 public bool Equals(ILocalisableStringData? other) => other is CaseTransformableString transformable && Equals(transformable); 62 63 public bool Equals(CaseTransformableString? other) 64 { 65 if (ReferenceEquals(null, other)) return false; 66 if (ReferenceEquals(this, other)) return true; 67 68 return Casing == other.Casing && String.Equals(other.String); 69 } 70 71 private string getStringData(LocalisationParameters localisationParameters) 72 { 73 switch (String.Data) 74 { 75 case string plain: 76 return plain; 77 78 case ILocalisableStringData data: 79 return data.GetLocalised(localisationParameters); 80 81 default: 82 return string.Empty; 83 } 84 } 85 } 86 87 /// <summary> 88 /// Case applicable to the underlying localisable string of a <see cref="CaseTransformableString"/>. 89 /// </summary> 90 public enum Casing 91 { 92 /// <summary> 93 /// Use the string data case. 94 /// </summary> 95 Default, 96 97 /// <summary> 98 /// Transform the string data to uppercase. 99 /// </summary> 100 UpperCase, 101 102 /// <summary> 103 /// Transform the string data to title case aka capitalized case 104 /// </summary> 105 TitleCase, 106 107 /// <summary> 108 /// Transform the string data to lowercase. 109 /// </summary> 110 LowerCase 111 } 112}