Skip to content

Commit

Permalink
Set system default locale
Browse files Browse the repository at this point in the history
  • Loading branch information
Drombeys committed Apr 24, 2024
1 parent 7038995 commit 6a23c65
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
3 changes: 1 addition & 2 deletions src/ImeSense.Launchers.Belarus.Avalonia/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public override async void OnFrameworkInitializationCompleted()

var userManager = _serviceProvider.GetRequiredService<UserManager>();
var localeManager = _serviceProvider.GetRequiredService<ILocaleManager>();
var locale = userManager.UserSettings?.Locale?.Key;
var locale = userManager.UserSettings?.Locale?.Key!;

var splashScreenViewModel = _serviceProvider.GetRequiredService<SplashScreenViewModel>();
var mainViewModel = _serviceProvider.GetRequiredService<MainWindowViewModel>();
Expand All @@ -144,7 +144,6 @@ public override async void OnFrameworkInitializationCompleted()
desktop.Shutdown();
return;
}

}

base.OnFrameworkInitializationCompleted();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,9 @@ public void InitializeLocale()
var userSettings = _userManager.UserSettings ??
throw new Exception("Error loading user config!");

userSettings.Locale = new();

if (userSettings.Locale.Key == string.Empty) {
var defaultLocale = _launcherStorage.Locales[0];
userSettings.Locale = defaultLocale;
if (userSettings.Locale is null) {
_logger.LogError("Locale was not set");
userSettings.Locale = _launcherStorage.Locales[0];
}

_localeManager.SetLocale(userSettings.Locale.Key);
Expand Down
30 changes: 24 additions & 6 deletions src/ImeSense.Launchers.Belarus.Core/Manager/UserManager.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,53 @@
using System.Globalization;
using System.Text.Json;

using ImeSense.Launchers.Belarus.Core.Models;
using ImeSense.Launchers.Belarus.Core.Storage;
using ImeSense.Launchers.Belarus.Core.Validators;

using Microsoft.Extensions.Logging;

using ReactiveUI;
using ReactiveUI.Fody.Helpers;

namespace ImeSense.Launchers.Belarus.Core.Manager;

public class UserManager : ReactiveObject
{
private readonly ILogger<UserManager>? _logger;
private readonly IAuthenticationValidator _authenticationValidator;
private readonly IStartGameValidator _startGameValidator;
private readonly ILauncherStorage _launcherStorage;

[Reactive]
public UserSettings? UserSettings { get; set; }

public UserManager(IAuthenticationValidator authenticationValidator, IStartGameValidator startGameValidator)
public UserManager(ILogger<UserManager>? logger, IAuthenticationValidator authenticationValidator, IStartGameValidator startGameValidator, ILauncherStorage launcherStorage)
{
_logger = logger;
_authenticationValidator = authenticationValidator;
_startGameValidator = startGameValidator;

_launcherStorage = launcherStorage;
Load();
}

private UserSettings CreateDefaultUserSettings() {
var userSettings = new UserSettings();
var systemCulture = CultureInfo.CurrentCulture;
if (systemCulture.ThreeLetterISOLanguageName.Equals(_launcherStorage.Locales[0].Key)) {
userSettings.Locale = _launcherStorage.Locales[0];
} else {
userSettings.Locale = _launcherStorage.Locales[1];
}
_logger?.LogInformation("Set locale: {locale}", userSettings.Locale.Title);

return userSettings;
}

private void Load()
{
if (!File.Exists(PathStorage.LauncherSetting)) {
UserSettings = new UserSettings();

UserSettings = CreateDefaultUserSettings();
return;
}

Expand All @@ -47,9 +65,9 @@ private void Load()
_authenticationValidator.IsUsernameCorrectCharacters(user.Username);
UserSettings = isUsernameCorrect
? user
: new UserSettings();
: CreateDefaultUserSettings();
} catch {
UserSettings = new UserSettings();
UserSettings = CreateDefaultUserSettings();
}
}

Expand Down

0 comments on commit 6a23c65

Please sign in to comment.