Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added translation support for server orders.
- Loading branch information
1 parent
e321201
commit 137a0d8
Showing
19 changed files
with
676 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
#region Copyright & License Information | ||
/* | ||
* Copyright 2007-2021 The OpenRA Developers (see AUTHORS) | ||
* This file is part of OpenRA, which is free software. It is made | ||
* available to you under the terms of the GNU General Public License | ||
* as published by the Free Software Foundation, either version 3 of | ||
* the License, or (at your option) any later version. For more | ||
* information, see COPYING. | ||
*/ | ||
#endregion | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
using OpenRA.Widgets; | ||
|
||
namespace OpenRA.Network | ||
{ | ||
[Flags] | ||
public enum SupportedTypes | ||
{ | ||
String = 0, | ||
Int = 1, | ||
Long = 2, | ||
} | ||
|
||
public class Argument | ||
{ | ||
public readonly string Key; | ||
public readonly string Value; | ||
public readonly SupportedTypes Type; | ||
|
||
public Argument() { } | ||
|
||
public Argument(string key, object value) | ||
{ | ||
Key = key; | ||
|
||
if (value is string) | ||
{ | ||
Type = SupportedTypes.String; | ||
Value = (string)value; | ||
} | ||
else if (value is int) | ||
{ | ||
Type = SupportedTypes.Int; | ||
Value = ((int)value).ToString(); | ||
} | ||
else if (value is long) | ||
{ | ||
Type = SupportedTypes.Long; | ||
Value = ((long)value).ToString(); | ||
} | ||
} | ||
} | ||
|
||
public class LocalizedMessage | ||
{ | ||
public const int ProtocolVersion = 1; | ||
|
||
public readonly string Key; | ||
|
||
[FieldLoader.LoadUsing(nameof(LoadArguments))] | ||
public readonly Argument[] Arguments; | ||
|
||
static object LoadArguments(MiniYaml yaml) | ||
{ | ||
var arguments = new List<Argument>(); | ||
var argumentsNode = yaml.Nodes.FirstOrDefault(n => n.Key == "Arguments"); | ||
if (argumentsNode != null) | ||
{ | ||
var regex = new Regex(@"Argument@\d+"); | ||
foreach (var argument in argumentsNode.Value.Nodes) | ||
if (regex.IsMatch(argument.Key)) | ||
arguments.Add(FieldLoader.Load<Argument>(argument.Value)); | ||
} | ||
|
||
return arguments.ToArray(); | ||
} | ||
|
||
static readonly string[] SerializeFields = { "Key" }; | ||
|
||
public LocalizedMessage(MiniYaml yaml) | ||
{ | ||
FieldLoader.Load(this, yaml); | ||
} | ||
|
||
public LocalizedMessage(string key, Dictionary<string, object> arguments = null) | ||
{ | ||
Key = key; | ||
Arguments = arguments?.Select(a => new Argument(a.Key, a.Value)).ToArray(); | ||
} | ||
|
||
public string Serialize() | ||
{ | ||
var root = new List<MiniYamlNode>() { new MiniYamlNode("Protocol", ProtocolVersion.ToString()) }; | ||
foreach (var field in SerializeFields) | ||
root.Add(FieldSaver.SaveField(this, field)); | ||
|
||
if (Arguments != null) | ||
{ | ||
var argumentsNode = new MiniYaml(""); | ||
var i = 0; | ||
foreach (var argument in Arguments) | ||
argumentsNode.Nodes.Add(new MiniYamlNode("Argument@" + i++, FieldSaver.Save(argument))); | ||
|
||
root.Add(new MiniYamlNode("Arguments", argumentsNode)); | ||
} | ||
|
||
return new MiniYaml("", root) | ||
.ToLines("LocalizedMessage") | ||
.JoinWith("\n"); | ||
} | ||
|
||
public string Translate() | ||
{ | ||
var argumentDictionary = new Dictionary<string, object>(); | ||
foreach (var argument in Arguments) | ||
{ | ||
if (argument.Type == SupportedTypes.Int && int.TryParse(argument.Value, out var integerValue)) | ||
argumentDictionary.Add(argument.Key, integerValue); | ||
else if (argument.Type == SupportedTypes.Long && long.TryParse(argument.Value, out var longValue)) | ||
argumentDictionary.Add(argument.Key, longValue); | ||
else | ||
argumentDictionary.Add(argument.Key, argument.Value); | ||
} | ||
|
||
return Ui.Translate(Key, argumentDictionary); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.