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
4#nullable enable
5
6using System;
7using System.Collections.Generic;
8using System.Globalization;
9
10namespace osu.Framework.Localisation
11{
12 /// <summary>
13 /// A string which formats an <see cref="IFormattable"/>s using the current locale.
14 /// </summary>
15 public class LocalisableFormattableString : IEquatable<LocalisableFormattableString>, ILocalisableStringData
16 {
17 public readonly IFormattable Value;
18 public readonly string? Format;
19
20 /// <summary>
21 /// Creates a <see cref="LocalisableFormattableString"/> with an <see cref="IFormattable"/> value and a format string.
22 /// </summary>
23 /// <param name="value">The <see cref="IFormattable"/> value.</param>
24 /// <param name="format">The format string.</param>
25 public LocalisableFormattableString(IFormattable value, string? format)
26 {
27 Value = value;
28 Format = format;
29 }
30
31 public string GetLocalised(LocalisationParameters parameters)
32 {
33 if (parameters.Store == null)
34 return ToString();
35
36 return Value.ToString(Format, parameters.Store.EffectiveCulture);
37 }
38
39 public override string ToString() => Value.ToString(Format, CultureInfo.InvariantCulture);
40
41 public bool Equals(LocalisableFormattableString? other)
42 {
43 if (ReferenceEquals(null, other)) return false;
44 if (ReferenceEquals(this, other)) return true;
45
46 return EqualityComparer<object>.Default.Equals(Value, other.Value) &&
47 Format == other.Format;
48 }
49
50 public bool Equals(ILocalisableStringData? other) => other is LocalisableFormattableString formattable && Equals(formattable);
51 public override bool Equals(object? obj) => obj is LocalisableFormattableString formattable && Equals(formattable);
52
53 public override int GetHashCode() => HashCode.Combine(Value, Format);
54 }
55}