Skip to content

Commit

Permalink
Set default system culture on launch when no settings
Browse files Browse the repository at this point in the history
  • Loading branch information
ionite34 committed Sep 14, 2023
1 parent 6ebc3be commit 2b6f85e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
6 changes: 5 additions & 1 deletion StabilityMatrix.Avalonia/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,11 @@ private static void ConfigureServiceProvider()

if (settingsManager.TryFindLibrary())
{
Cultures.TrySetSupportedCulture(settingsManager.Settings.Language);
Cultures.SetSupportedCultureOrDefault(settingsManager.Settings.Language);
}
else
{
Cultures.TrySetSupportedCulture(Settings.GetDefaultCulture());
}

Services.GetRequiredService<ProgressManagerViewModel>().StartEventListener();
Expand Down
8 changes: 8 additions & 0 deletions StabilityMatrix.Avalonia/Languages/Cultures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ cultureCode is null
return culture;
}

public static void SetSupportedCultureOrDefault(string? cultureCode)
{
if (!TrySetSupportedCulture(cultureCode))
{
TrySetSupportedCulture(Default);
}
}

public static bool TrySetSupportedCulture(string? cultureCode)
{
if (
Expand Down
19 changes: 17 additions & 2 deletions StabilityMatrix.Core/Models/Settings/Settings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json.Serialization;
using System.Globalization;
using System.Text.Json.Serialization;

namespace StabilityMatrix.Core.Models.Settings;

Expand All @@ -7,7 +8,7 @@ public class Settings
public int? Version { get; set; } = 1;
public bool FirstLaunchSetupComplete { get; set; }
public string? Theme { get; set; } = "Dark";
public string? Language { get; set; } = "en-US";
public string? Language { get; set; } = GetDefaultCulture().Name;

public List<InstalledPackage> InstalledPackages { get; set; } = new();

Expand Down Expand Up @@ -83,4 +84,18 @@ public void UpdateActiveInstalledPackage()
ActiveInstalledPackageId = InstalledPackages[0].Id;
}
}

/// <summary>
/// Return either the system default culture, if supported, or en-US
/// </summary>
/// <returns></returns>
public static CultureInfo GetDefaultCulture()
{
var supportedCultures = new[] { "en-US", "ja-JP" };

var systemCulture = CultureInfo.InstalledUICulture;
return supportedCultures.Contains(systemCulture.Name)
? systemCulture
: new CultureInfo("en-US");
}
}

0 comments on commit 2b6f85e

Please sign in to comment.