2 using System.Collections.Immutable;
8 static class ServerLanguageOptions
10 public readonly record
struct LanguageOption(
12 LanguageIdentifier Identifier,
13 ImmutableArray<LanguageIdentifier> MapsFrom)
15 public static LanguageOption FromXElement(XElement element)
16 =>
new LanguageOption(
18 element.GetAttributeString(
"label",
""),
20 element.GetAttributeIdentifier(
"identifier", LanguageIdentifier.None.Value)
21 .ToLanguageIdentifier(),
23 element.GetAttributeIdentifierArray(
"mapsFrom", Array.Empty<Identifier>())
24 .Select(
id =>
id.ToLanguageIdentifier()).ToImmutableArray());
27 public static readonly ImmutableArray<LanguageOption> Options;
29 static ServerLanguageOptions()
31 var languageOptionElements
32 = XMLExtensions.TryLoadXml(
"Data/languageoptions.xml")?.Root?.Elements()
33 ?? Enumerable.Empty<XElement>();
34 Options = languageOptionElements
36 .Select(LanguageOption.FromXElement)
38 .DistinctBy(p => p.Identifier)
40 .Where(p => !p.Label.IsNullOrWhiteSpace() && p.Identifier != LanguageIdentifier.None)
42 .OrderBy(p => p.Label)
46 public static LanguageIdentifier PickLanguage(LanguageIdentifier
id)
48 if (
id == LanguageIdentifier.None)
50 id = GameSettings.CurrentConfig.Language;
53 foreach (var (_, identifier, mapsFrom) in Options)
55 if (
id == identifier || mapsFrom.Contains(
id))
61 return TextManager.DefaultLanguage;