diff --git a/package/AgentWindowsManaged/Program.cs b/package/AgentWindowsManaged/Program.cs index febd3f055..b02f54176 100644 --- a/package/AgentWindowsManaged/Program.cs +++ b/package/AgentWindowsManaged/Program.cs @@ -13,9 +13,7 @@ using System.IO.Compression; using System.Linq; using System.Security.Cryptography; -using System.Text.RegularExpressions; using System.Windows.Forms; -using System.Xml; using WixSharp; using WixSharp.CommonTasks; @@ -425,33 +423,32 @@ private static void Project_UnhandledException(ExceptionEventArgs e) private static void Project_UIInitialized(SetupEventArgs e) { string lcid = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName == "fr" ? frFR.Key : enUS.Key; + string resourceName = $"DevolutionsAgent.Resources.{Languages[lcid]}"; - using Stream stream = Assembly.GetExecutingAssembly() - .GetManifestResourceStream($"DevolutionsAgent.Resources.{Languages[lcid]}"); + using Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName); - XmlDocument xml = new(); - xml.Load(stream); + if (stream is null) + { + throw new FileNotFoundException($"Missing localization resource: {resourceName}"); + } - Dictionary strings = new(); + using MemoryStream memory = new(); + stream.CopyTo(memory); - foreach (XmlNode s in xml.GetElementsByTagName("String")) + if (e.ManagedUI.Shell.RuntimeContext is not InstallerRuntime runtime) { - strings.Add(s.Attributes["Id"].Value, s.InnerText); + throw new InvalidOperationException("Managed UI runtime is not available"); } + runtime.UIText.InitFromWxl(memory.ToArray(), true); + string I18n(string key) { - if (!strings.TryGetValue(key, out string result)) + string localized = $"[{key}]".LocalizeWith(runtime.Localize); + return localized.LocalizeWith(name => { - return key; - } - - return Regex.Replace(result, @"\[(.*?)]", (match) => - { - string property = match.Groups[1].Value; - string value = e.Session[property]; - - return string.IsNullOrEmpty(value) ? property : value; + string value = e.Session[name]; + return string.IsNullOrEmpty(value) ? null : value; }); }