From 8abaa9d09dfd819785932839fddd0a1cf7606404 Mon Sep 17 00:00:00 2001 From: jbe2277 Date: Sun, 18 Aug 2024 20:38:09 +0200 Subject: [PATCH] InfoMan: add support for CLI culture configuration --- .../InformationManager/Assembler/App.xaml.cs | 29 ++++++++++++++++++- .../Assembler/Assembler.csproj | 2 ++ .../Assembler/Properties/AppConfig.cs | 8 +++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/System.Waf/Samples/InformationManager/Assembler/Properties/AppConfig.cs diff --git a/src/System.Waf/Samples/InformationManager/Assembler/App.xaml.cs b/src/System.Waf/Samples/InformationManager/Assembler/App.xaml.cs index 0ca28bf0..16cd2923 100644 --- a/src/System.Waf/Samples/InformationManager/Assembler/App.xaml.cs +++ b/src/System.Waf/Samples/InformationManager/Assembler/App.xaml.cs @@ -1,4 +1,5 @@ -using NLog; +using Microsoft.Extensions.Configuration; +using NLog; using NLog.Targets; using NLog.Targets.Wrappers; using System.ComponentModel.Composition; @@ -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() ?? 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 @@ -87,6 +100,7 @@ protected override void OnStartup(StartupEventArgs e) batch.AddExportedValue(container); container.Compose(batch); + InitializeCultures(appConfig); var presentationServices = container.GetExportedValues(); foreach (var x in presentationServices) x.Initialize(); @@ -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); diff --git a/src/System.Waf/Samples/InformationManager/Assembler/Assembler.csproj b/src/System.Waf/Samples/InformationManager/Assembler/Assembler.csproj index ddf79f31..3777bf8d 100644 --- a/src/System.Waf/Samples/InformationManager/Assembler/Assembler.csproj +++ b/src/System.Waf/Samples/InformationManager/Assembler/Assembler.csproj @@ -10,6 +10,8 @@ + + diff --git a/src/System.Waf/Samples/InformationManager/Assembler/Properties/AppConfig.cs b/src/System.Waf/Samples/InformationManager/Assembler/Properties/AppConfig.cs new file mode 100644 index 00000000..3cde73ea --- /dev/null +++ b/src/System.Waf/Samples/InformationManager/Assembler/Properties/AppConfig.cs @@ -0,0 +1,8 @@ +namespace Waf.InformationManager.Assembler.Properties; + +public class AppConfig +{ + public string? Culture { get; init; } + + public string? UICulture { get; init; } +}