Add translation arguments to the Lua API.

This commit is contained in:
Matthias Mailänder
2022-09-06 21:10:48 +02:00
committed by Gustas
parent 5d118e2634
commit b62cf7ee9a

View File

@@ -9,6 +9,8 @@
*/
#endregion
using System.Collections.Generic;
using Eluant;
using OpenRA.Mods.Common.Widgets;
using OpenRA.Primitives;
using OpenRA.Scripting;
@@ -32,8 +34,26 @@ namespace OpenRA.Mods.Common.Scripting.Global
luaLabel.GetColor = () => c;
}
public string Translate(string text)
public string Translate(string text, LuaTable table = null)
{
if (table != null)
{
var argumentDictionary = new Dictionary<string, object>();
foreach (var kv in table)
{
using (kv.Key)
using (kv.Value)
{
if (!kv.Key.TryGetClrValue(out string variable) || !kv.Value.TryGetClrValue(out object value))
throw new LuaException($"Translation arguments requires a table of [\"string\"]=value pairs. Received {kv.Key.WrappedClrType().Name},{kv.Value.WrappedClrType().Name}");
argumentDictionary.Add(variable, value);
}
}
return Context.World.Map.Translate(text, argumentDictionary);
}
return Context.World.Map.Translate(text);
}
}