Client LuaCsForBarotrauma
LocalizedString.cs
1 #nullable enable
2 using System;
3 using System.Collections.Generic;
4 
5 namespace Barotrauma
6 {
7  public abstract class LocalizedString : IComparable
8  {
9  protected enum LoadedSuccessfully
10  {
11  Unknown,
12  No,
13  Yes
14  }
15 
16  public LanguageIdentifier Language { get; private set; } = LanguageIdentifier.None;
17  private int languageVersion = 0;
18 
19  protected string cachedValue = "";
20  public string Value
21  {
22  get
23  {
24  if (MustRetrieveValue()) { RetrieveValue(); }
25  return cachedValue;
26  }
27  }
28 
29  public int Length => Value.Length;
30 
31  public abstract bool Loaded { get; }
32 
33  protected void UpdateLanguage()
34  {
35  Language = GameSettings.CurrentConfig.Language;
36  languageVersion = TextManager.LanguageVersion;
37  }
38 
39  protected virtual bool MustRetrieveValue() //this can't be called on other LocalizedStrings by derived classes
40  {
41  return Language != GameSettings.CurrentConfig.Language || languageVersion != TextManager.LanguageVersion;
42  }
43 
44  protected static bool MustRetrieveValue(LocalizedString str) //this can be called by derived classes
45  {
46  return str.MustRetrieveValue();
47  }
48 
49  public abstract void RetrieveValue();
50 
51  public static readonly RawLString EmptyString = new RawLString("");
52  public static implicit operator LocalizedString(string value)
53  => !value.IsNullOrEmpty()
54  ? new RawLString(value)
55  : EmptyString;
56  public static implicit operator LocalizedString(char value) => new RawLString(value.ToString());
57 
59  {
60  // If either side of the concatenation is an empty string,
61  // return the other string instead of creating a new object
62  if (left is RawLString { Value.Length: 0 }) { return right; }
63  if (right is RawLString { Value.Length: 0 }) { return left; }
64 
65  return new ConcatLString(left, right);
66  }
67 
68  public static LocalizedString operator+(LocalizedString left, object right) => left + (right.ToString() ?? "");
69  public static LocalizedString operator+(object left, LocalizedString right) => (left.ToString() ?? "") + right;
70 
71  public static bool operator==(LocalizedString? left, LocalizedString? right)
72  {
73  return left?.Value == right?.Value;
74  }
75 
76  public static bool operator!=(LocalizedString? left, LocalizedString? right)
77  {
78  return !(left == right);
79  }
80 
81  public override string ToString()
82  {
83  return Value;
84  }
85 
86  public bool Contains(string subStr, StringComparison comparison = StringComparison.Ordinal)
87  {
88  return !Value.IsNullOrEmpty() && Value.Contains(subStr, comparison);
89  }
90 
91  public bool Contains(char chr, StringComparison comparison = StringComparison.Ordinal)
92  {
93  return Value.Contains(chr, comparison);
94  }
95 
96  public virtual LocalizedString ToUpper()
97  {
98  return new UpperLString(this);
99  }
100 
101  public static LocalizedString Join(string separator, params LocalizedString[] subStrs)
102  {
103  return Join(separator, (IEnumerable<LocalizedString>)subStrs);
104  }
105 
106  public static LocalizedString Join(string separator, IEnumerable<LocalizedString> subStrs)
107  {
108  return new JoinLString(separator, subStrs);
109  }
110 
116  public LocalizedString Fallback(LocalizedString fallback, bool useDefaultLanguageIfFound = true)
117  {
118  return new FallbackLString(this, fallback, useDefaultLanguageIfFound);
119  }
120 
121  public IReadOnlyList<LocalizedString> Split(params char[] separators)
122  {
123  var splitter = new LStringSplitter(this, separators);
124  return splitter.Substrings;
125  }
126 
127  public LocalizedString Replace(Identifier find, LocalizedString replace, StringComparison stringComparison = StringComparison.Ordinal)
128  {
129  return new ReplaceLString(this, stringComparison, (find, replace));
130  }
131 
132  public LocalizedString Replace(string find, LocalizedString replace, StringComparison stringComparison = StringComparison.Ordinal)
133  {
134  return new ReplaceLString(this, stringComparison, (find.ToIdentifier(), replace));
135  }
136 
138  StringComparison stringComparison = StringComparison.Ordinal)
139  {
140  return new ReplaceLString(this, stringComparison, (find, replace));
141  }
142 
144  {
145  return new TrimLString(this, TrimLString.Mode.Start);
146  }
147 
149  {
150  return new TrimLString(this, TrimLString.Mode.End);
151  }
152 
154  {
155  return new LowerLString(this);
156  }
157 
158  public override bool Equals(object? obj)
159  {
160  if (obj is LocalizedString lStr) { return Equals(lStr, StringComparison.Ordinal); }
161  if (obj is string str) { return Equals(str, StringComparison.Ordinal); }
162  return base.Equals(obj);
163  }
164 
165  public bool Equals(LocalizedString other, StringComparison comparison = StringComparison.Ordinal)
166  {
167  return Equals(other.Value, comparison);
168  }
169 
170  public bool Equals(string other, StringComparison comparison = StringComparison.Ordinal)
171  {
172  return string.Equals(Value, other, comparison);
173  }
174 
175  public bool StartsWith(LocalizedString other, StringComparison comparison = StringComparison.Ordinal)
176  {
177  return StartsWith(other.Value, comparison);
178  }
179 
180  public bool StartsWith(string other, StringComparison comparison = StringComparison.Ordinal)
181  {
182  return Value.StartsWith(other, comparison);
183  }
184 
185  public override int GetHashCode()
186  {
187  return Value.GetHashCode();
188  }
189 
190  public int CompareTo(object? obj)
191  {
192  return Value.CompareTo(obj?.ToString() ?? "");
193  }
194  }
195 }
LocalizedString Replace(string find, LocalizedString replace, StringComparison stringComparison=StringComparison.Ordinal)
IReadOnlyList< LocalizedString > Split(params char[] separators)
LanguageIdentifier Language
static bool operator!=(LocalizedString? left, LocalizedString? right)
bool Contains(string subStr, StringComparison comparison=StringComparison.Ordinal)
LocalizedString Fallback(LocalizedString fallback, bool useDefaultLanguageIfFound=true)
Use this text instead if the original text cannot be found.
bool Equals(string other, StringComparison comparison=StringComparison.Ordinal)
LocalizedString Replace(LocalizedString find, LocalizedString replace, StringComparison stringComparison=StringComparison.Ordinal)
static bool MustRetrieveValue(LocalizedString str)
override bool Equals(object? obj)
override string ToString()
static LocalizedString operator+(LocalizedString left, object right)
abstract void RetrieveValue()
static readonly RawLString EmptyString
virtual LocalizedString ToUpper()
LocalizedString TrimStart()
static LocalizedString Join(string separator, params LocalizedString[] subStrs)
LocalizedString Replace(Identifier find, LocalizedString replace, StringComparison stringComparison=StringComparison.Ordinal)
static LocalizedString operator+(LocalizedString left, LocalizedString right)
static LocalizedString operator+(object left, LocalizedString right)
bool Equals(LocalizedString other, StringComparison comparison=StringComparison.Ordinal)
bool StartsWith(string other, StringComparison comparison=StringComparison.Ordinal)
virtual bool MustRetrieveValue()
bool Contains(char chr, StringComparison comparison=StringComparison.Ordinal)
static LocalizedString Join(string separator, IEnumerable< LocalizedString > subStrs)
bool StartsWith(LocalizedString other, StringComparison comparison=StringComparison.Ordinal)
static bool operator==(LocalizedString? left, LocalizedString? right)
static readonly LanguageIdentifier None
Definition: TextPack.cs:12