A game framework written with osu! in mind.
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 osu.Framework.Configuration;
6
7#nullable enable
8
9namespace osu.Framework.Localisation
10{
11 /// <summary>
12 /// A string that has a romanised fallback to allow a better experience for users that potentially can't read the original script.
13 /// See <see cref="FrameworkSetting.ShowUnicode"/>, which can toggle the display of romanised variants.
14 /// </summary>
15 public class RomanisableString : IEquatable<RomanisableString>, ILocalisableStringData
16 {
17 /// <summary>
18 /// The string in its original script. May be null.
19 /// </summary>
20 public readonly string? Original;
21
22 /// <summary>
23 /// The romanised version of the string. May be null.
24 /// </summary>
25 public readonly string? Romanised;
26
27 /// <summary>
28 /// Construct a new romanisable string.
29 /// </summary>
30 /// <remarks>
31 /// For flexibility, both of the provided strings are allowed to be null. If both are null, the returned string value from <see cref="GetLocalised"/> will be <see cref="string.Empty"/>.
32 /// </remarks>
33 /// <param name="original">The string in its original script. If null, the <paramref name="romanised"/> version will always be used.</param>
34 /// <param name="romanised">The romanised version of the string. If null, the <paramref name="original"/> version will always be used.</param>
35 public RomanisableString(string? original, string? romanised)
36 {
37 Original = original;
38 Romanised = romanised;
39 }
40
41 public string GetLocalised(LocalisationParameters parameters) => GetPreferred(parameters.PreferOriginalScript);
42
43 /// <summary>
44 /// Get the best match for this string based on a user preference for which should be displayed.
45 /// </summary>
46 /// <param name="preferUnicode">Whether to prefer the unicode (aka original) version where available.</param>
47 /// <returns>The best match for the provided criteria.</returns>
48 public string GetPreferred(bool preferUnicode)
49 {
50 if (string.IsNullOrEmpty(Romanised)) return Original ?? string.Empty;
51 if (string.IsNullOrEmpty(Original)) return Romanised ?? string.Empty;
52
53 return preferUnicode ? Original : Romanised;
54 }
55
56 public override string ToString() => GetLocalised(new LocalisationParameters(null, false));
57
58 public bool Equals(RomanisableString? other)
59 {
60 if (ReferenceEquals(null, other)) return false;
61 if (ReferenceEquals(this, other)) return true;
62
63 return Original == other.Original
64 && Romanised == other.Romanised;
65 }
66
67 public bool Equals(ILocalisableStringData? other) => other is RomanisableString romanisable && Equals(romanisable);
68 public override bool Equals(object? obj) => obj is RomanisableString romanisable && Equals(romanisable);
69
70 public override int GetHashCode()
71 {
72 return HashCode.Combine(Original, Romanised);
73 }
74 }
75}