Refactor LocalizedMessage.

This commit is contained in:
Matthias Mailänder
2023-08-19 12:58:53 +02:00
committed by Gustas
parent 1899eed839
commit b742a776eb
2 changed files with 19 additions and 30 deletions

View File

@@ -9,10 +9,8 @@
*/ */
#endregion #endregion
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions;
using Linguini.Shared.Types.Bundle; using Linguini.Shared.Types.Bundle;
namespace OpenRA.Network namespace OpenRA.Network
@@ -57,45 +55,36 @@ namespace OpenRA.Network
public readonly string Key = string.Empty; public readonly string Key = string.Empty;
[FieldLoader.LoadUsing(nameof(LoadArguments))] [FieldLoader.LoadUsing(nameof(LoadArguments))]
public readonly FluentArgument[] Arguments = Array.Empty<FluentArgument>(); public readonly Dictionary<string, object> Arguments;
public string TranslatedText { get; }
static object LoadArguments(MiniYaml yaml) static object LoadArguments(MiniYaml yaml)
{ {
var arguments = new List<FluentArgument>(); var arguments = new Dictionary<string, object>();
var argumentsNode = yaml.Nodes.FirstOrDefault(n => n.Key == "Arguments"); var argumentsNode = yaml.Nodes.FirstOrDefault(n => n.Key == "Arguments");
if (argumentsNode != null) if (argumentsNode != null)
{ {
var regex = new Regex(@"Argument@\d+"); foreach (var argumentNode in argumentsNode.Value.Nodes)
foreach (var argument in argumentsNode.Value.Nodes) {
if (regex.IsMatch(argument.Key)) var argument = FieldLoader.Load<FluentArgument>(argumentNode.Value);
arguments.Add(FieldLoader.Load<FluentArgument>(argument.Value)); if (argument.Type == FluentArgument.FluentArgumentType.Number)
{
if (!double.TryParse(argument.Value, out var number))
Log.Write("debug", $"Failed to parse {argument.Value}");
arguments.Add(argument.Key, number);
}
else
arguments.Add(argument.Key, argument.Value);
}
} }
return arguments.ToArray(); return arguments;
} }
public LocalizedMessage(MiniYaml yaml) public LocalizedMessage(MiniYaml yaml)
{ {
// Let the FieldLoader do the dirty work of loading the public fields. // Let the FieldLoader do the dirty work of loading the public fields.
FieldLoader.Load(this, yaml); FieldLoader.Load(this, yaml);
var argumentDictionary = new Dictionary<string, object>();
foreach (var argument in Arguments)
{
if (argument.Type == FluentArgument.FluentArgumentType.Number)
{
if (!double.TryParse(argument.Value, out var number))
Log.Write("debug", $"Failed to parse {argument.Value}");
argumentDictionary.Add(argument.Key, number);
}
else
argumentDictionary.Add(argument.Key, argument.Value);
}
TranslatedText = TranslationProvider.GetString(Key, argumentDictionary);
} }
public static string Serialize(string key, Dictionary<string, object> arguments = null) public static string Serialize(string key, Dictionary<string, object> arguments = null)

View File

@@ -51,11 +51,11 @@ namespace OpenRA.Network
{ {
var localizedMessage = new LocalizedMessage(node.Value); var localizedMessage = new LocalizedMessage(node.Value);
if (localizedMessage.Key == Joined) if (localizedMessage.Key == Joined)
TextNotificationsManager.AddPlayerJoinedLine(localizedMessage.TranslatedText); TextNotificationsManager.AddPlayerJoinedLine(localizedMessage.Key, localizedMessage.Arguments);
else if (localizedMessage.Key == Left) else if (localizedMessage.Key == Left)
TextNotificationsManager.AddPlayerLeftLine(localizedMessage.TranslatedText); TextNotificationsManager.AddPlayerLeftLine(localizedMessage.Key, localizedMessage.Arguments);
else else
TextNotificationsManager.AddSystemLine(localizedMessage.TranslatedText); TextNotificationsManager.AddSystemLine(localizedMessage.Key, localizedMessage.Arguments);
} }
break; break;