From b62cf7ee9abc7d926698563a2abc2e8ca51d21ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Mail=C3=A4nder?= Date: Tue, 6 Sep 2022 21:10:48 +0200 Subject: [PATCH] Add translation arguments to the Lua API. --- .../Scripting/Global/UserInterfaceGlobal.cs | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/OpenRA.Mods.Common/Scripting/Global/UserInterfaceGlobal.cs b/OpenRA.Mods.Common/Scripting/Global/UserInterfaceGlobal.cs index 438a599dce..fdb9b89861 100644 --- a/OpenRA.Mods.Common/Scripting/Global/UserInterfaceGlobal.cs +++ b/OpenRA.Mods.Common/Scripting/Global/UserInterfaceGlobal.cs @@ -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(); + 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); } }