Skip to content

Commit a0471f3

Browse files
committed
Writer: Support language settings via command line
1 parent e9a65c6 commit a0471f3

File tree

7 files changed

+43
-8
lines changed

7 files changed

+43
-8
lines changed

src/System.Waf/Directory.Packages.props

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.0" />
1717
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.0" />
1818
<PackageVersion Include="Microsoft.EntityFrameworkCore.SQLite" Version="8.0.0" />
19+
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1" />
20+
<PackageVersion Include="Microsoft.Extensions.Configuration.CommandLine" Version="8.0.0" />
1921
<PackageVersion Include="NLog" Version="5.2.6" />
2022

2123
<!-- Unit tests -->

src/System.Waf/Samples/Writer/Writer.Applications.Test/Controllers/ModuleControllerTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ public void SettingsTest()
114114
{
115115
var settingsService = Get<MockSettingsService>();
116116
var settings = settingsService.Get<AppSettings>();
117-
settings.Culture = "de-DE";
118117
settings.UICulture = "de-AT";
119118

120119
var controller = Get<ModuleController>();

src/System.Waf/Samples/Writer/Writer.Applications/Properties/AppSettings.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ public sealed class AppSettings : UserSettingsBase
1717

1818
[DataMember] public bool IsMaximized { get; set; }
1919

20-
[DataMember] public string? Culture { get; set; }
21-
2220
[DataMember] public string? UICulture { get; set; }
2321

2422
[DataMember] public RecentFileList? RecentFileList { get; set; }

src/System.Waf/Samples/Writer/Writer.Presentation/App.xaml.cs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
using System.Windows.Threading;
1313
using Waf.Writer.Applications.Properties;
1414
using Waf.Writer.Applications.ViewModels;
15+
using Microsoft.Extensions.Configuration;
16+
using Waf.Writer.Presentation.Properties;
1517

1618
namespace Waf.Writer.Presentation;
1719

@@ -33,6 +35,18 @@ protected override void OnStartup(StartupEventArgs e)
3335
DispatcherUnhandledException += AppDispatcherUnhandledException;
3436
AppDomain.CurrentDomain.UnhandledException += AppDomainUnhandledException;
3537
#endif
38+
AppConfig appConfig;
39+
try
40+
{
41+
var config = new ConfigurationBuilder().AddCommandLine(Environment.GetCommandLineArgs()).Build();
42+
appConfig = config.Get<AppConfig>() ?? new AppConfig();
43+
}
44+
catch (Exception ex)
45+
{
46+
Log.Default.Error(ex, "Command line parsing error");
47+
appConfig = new AppConfig();
48+
}
49+
3650
catalog = new();
3751
catalog.Catalogs.Add(new AssemblyCatalog(typeof(IMessageService).Assembly)); // WinApplicationFramework
3852
catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly())); // Writer.Presentation
@@ -44,7 +58,7 @@ protected override void OnStartup(StartupEventArgs e)
4458

4559
var settingsService = container.GetExportedValue<ISettingsService>();
4660
settingsService.ErrorOccurred += (_, e) => Log.Default.Error(e.Error, "Error in SettingsService");
47-
InitializeCultures(settingsService.Get<AppSettings>());
61+
InitializeCultures(appConfig, settingsService.Get<AppSettings>());
4862
FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
4963

5064
moduleControllers = container.GetExportedValues<IModuleController>();
@@ -61,10 +75,19 @@ protected override void OnExit(ExitEventArgs e)
6175
base.OnExit(e);
6276
}
6377

64-
private static void InitializeCultures(AppSettings settings)
78+
private static void InitializeCultures(AppConfig appConfig, AppSettings settings)
6579
{
66-
if (!string.IsNullOrEmpty(settings.Culture)) CultureInfo.CurrentCulture = CultureInfo.DefaultThreadCurrentCulture = new CultureInfo(settings.Culture);
67-
if (!string.IsNullOrEmpty(settings.UICulture)) CultureInfo.CurrentUICulture = CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo(settings.UICulture);
80+
try
81+
{
82+
if (!string.IsNullOrEmpty(appConfig.Culture)) Thread.CurrentThread.CurrentCulture = CultureInfo.DefaultThreadCurrentCulture = new CultureInfo(appConfig.Culture);
83+
84+
var uiCulture = !string.IsNullOrEmpty(appConfig.UICulture) ? appConfig.UICulture : settings.UICulture;
85+
if (!string.IsNullOrEmpty(uiCulture)) Thread.CurrentThread.CurrentUICulture = CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo(uiCulture);
86+
}
87+
catch (Exception ex)
88+
{
89+
Log.Default.Error(ex, "The specified culture code is invalid");
90+
}
6891
}
6992

7093
private void AppDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) => HandleException(e.Exception, false);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Waf.Writer.Presentation.Properties;
2+
3+
public class AppConfig
4+
{
5+
public string? Culture { get; init; }
6+
7+
public string? UICulture { get; init; }
8+
}

src/System.Waf/Samples/Writer/Writer.Presentation/Services/SystemService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Waf.Writer.Presentation.Services;
77
[Export(typeof(ISystemService))]
88
internal sealed class SystemService : ISystemService
99
{
10-
private readonly Lazy<string?> documentFileName = new(() => Environment.GetCommandLineArgs().ElementAtOrDefault(1));
10+
private readonly Lazy<string?> documentFileName = new(() => Environment.GetCommandLineArgs().Skip(1).FirstOrDefault(x => !x.StartsWith('-')));
1111

1212
public string? DocumentFileName => documentFileName.Value;
1313

src/System.Waf/Samples/Writer/Writer.Presentation/Writer.Presentation.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
<OutputPath>..\..\..\..\..\out\Writer\$(Configuration)\</OutputPath>
1010
</PropertyGroup>
1111

12+
<ItemGroup>
13+
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" />
14+
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" />
15+
</ItemGroup>
16+
1217
<ItemGroup>
1318
<ProjectReference Include="..\Writer.Applications\Writer.Applications.csproj" />
1419
</ItemGroup>

0 commit comments

Comments
 (0)