Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 16 additions & 19 deletions package/AgentWindowsManaged/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<string, string> 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;
});
}

Expand Down
Loading