Skip to content

Commit

Permalink
InfoMan: add support for CLI culture configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
jbe2277 committed Aug 18, 2024
1 parent 2b5dd05 commit 8abaa9d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/System.Waf/Samples/InformationManager/Assembler/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NLog;
using Microsoft.Extensions.Configuration;
using NLog;
using NLog.Targets;
using NLog.Targets.Wrappers;
using System.ComponentModel.Composition;
Expand Down Expand Up @@ -72,6 +73,18 @@ protected override void OnStartup(StartupEventArgs e)
DispatcherUnhandledException += AppDispatcherUnhandledException;
AppDomain.CurrentDomain.UnhandledException += AppDomainUnhandledException;
#endif
AppConfig appConfig;
try
{
var config = new ConfigurationBuilder().AddCommandLine(Environment.GetCommandLineArgs()).Build();
appConfig = config.Get<AppConfig>() ?? new AppConfig();
}
catch (Exception ex)
{
Log.App.Error(ex, "Command line parsing error");
appConfig = new AppConfig();
}

catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(typeof(IMessageService).Assembly)); // WinApplicationFramework

Expand All @@ -87,6 +100,7 @@ protected override void OnStartup(StartupEventArgs e)
batch.AddExportedValue(container);
container.Compose(batch);

InitializeCultures(appConfig);
var presentationServices = container.GetExportedValues<IPresentationService>();
foreach (var x in presentationServices) x.Initialize();

Expand All @@ -104,6 +118,19 @@ protected override void OnExit(ExitEventArgs e)
base.OnExit(e);
}

private static void InitializeCultures(AppConfig appConfig)
{
try
{
if (!string.IsNullOrEmpty(appConfig.Culture)) Thread.CurrentThread.CurrentCulture = CultureInfo.DefaultThreadCurrentCulture = new CultureInfo(appConfig.Culture);
if (!string.IsNullOrEmpty(appConfig.UICulture)) Thread.CurrentThread.CurrentUICulture = CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo(appConfig.UICulture);
}
catch (Exception ex)
{
Log.App.Error(ex, "The specified culture code is invalid");
}
}

private void AppDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) => HandleException(e.Exception, false);

private static void AppDomainUnhandledException(object sender, UnhandledExceptionEventArgs e) => HandleException(e.ExceptionObject as Exception, e.IsTerminating);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" />
<PackageReference Include="NLog" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Waf.InformationManager.Assembler.Properties;

public class AppConfig
{
public string? Culture { get; init; }

public string? UICulture { get; init; }
}

0 comments on commit 8abaa9d

Please sign in to comment.