diff --git a/src/PicView.Avalonia.MacOS/App.axaml.cs b/src/PicView.Avalonia.MacOS/App.axaml.cs index 5e5a24b6f..71b517024 100644 --- a/src/PicView.Avalonia.MacOS/App.axaml.cs +++ b/src/PicView.Avalonia.MacOS/App.axaml.cs @@ -48,12 +48,14 @@ public override async void OnFrameworkInitializationCompleted() try { settingsExists = await SettingsHelper.LoadSettingsAsync().ConfigureAwait(false); - await TranslationHelper.LoadLanguage(SettingsHelper.Settings.UIProperties.UserLanguage).ConfigureAwait(false); } catch (TaskCanceledException) { return; } + + TranslationHelper.Init(); + await Dispatcher.UIThread.InvokeAsync(() => { ThemeManager.DetermineTheme(Current, settingsExists); @@ -61,7 +63,9 @@ await Dispatcher.UIThread.InvokeAsync(() => _mainWindow = new MacMainWindow(); desktop.MainWindow = _mainWindow; }); + _vm = new MainViewModel(this); + await Dispatcher.UIThread.InvokeAsync(() => { _mainWindow.DataContext = _vm; diff --git a/src/PicView.Avalonia.Win32/App.axaml.cs b/src/PicView.Avalonia.Win32/App.axaml.cs index 7256f0e7d..3a7141c91 100644 --- a/src/PicView.Avalonia.Win32/App.axaml.cs +++ b/src/PicView.Avalonia.Win32/App.axaml.cs @@ -57,13 +57,13 @@ public override async void OnFrameworkInitializationCompleted() try { settingsExists = await SettingsHelper.LoadSettingsAsync().ConfigureAwait(false); - TranslationHelper.Init(); - //await TranslationHelper.LoadLanguage(SettingsHelper.Settings.UIProperties.UserLanguage); } catch (TaskCanceledException) { return; } + + TranslationHelper.Init(); await Dispatcher.UIThread.InvokeAsync(() => { @@ -72,7 +72,9 @@ await Dispatcher.UIThread.InvokeAsync(() => _mainWindow = new WinMainWindow(); desktop.MainWindow = _mainWindow; }); + _vm = new MainViewModel(this); + await Dispatcher.UIThread.InvokeAsync(() => { _mainWindow.DataContext = _vm; diff --git a/src/PicView.Core/Localization/TranslationHelper.cs b/src/PicView.Core/Localization/TranslationHelper.cs index 689f27d08..fbbaa8652 100644 --- a/src/PicView.Core/Localization/TranslationHelper.cs +++ b/src/PicView.Core/Localization/TranslationHelper.cs @@ -11,21 +11,31 @@ namespace PicView.Core.Localization; internal partial class LanguageSourceGenerationContext : JsonSerializerContext; /// -/// Helper class for managing language-related tasks. +/// Helper class for managing language-related tasks, including loading and switching languages. /// public static class TranslationHelper { - public static LanguageModel? Translation - { - get; - private set; - } + /// + /// The current language model containing translations. + /// + public static LanguageModel? Translation { get; private set; } + /// + /// Initializes the language model by setting all strings to empty. + /// + /// + /// This is used to defer loading translations until explicitly needed. + /// public static void Init() { Translation = new LanguageModel(); } + /// + /// Retrieves the translated string for the given key. + /// + /// The key representing the translation string. + /// The translated string, or the key itself if no translation is found. public static string GetTranslation(string key) { if (Translation == null) @@ -38,9 +48,10 @@ public static string GetTranslation(string key) } /// - /// Determines the language based on the specified culture and loads the corresponding language file. + /// Loads the language file based on the provided ISO language code. /// - /// The culture code representing the desired language. + /// The ISO language code (e.g., 'en', 'da'). + /// Returns true if the language file was successfully loaded; false if an error occurred. public static async Task LoadLanguage(string isoLanguageCode) { var jsonLanguageFile = DetermineLanguageFilePath(isoLanguageCode); @@ -66,6 +77,9 @@ public static async Task LoadLanguage(string isoLanguageCode) } } + /// + /// Determines the correct language based on the system's current culture and loads the corresponding language file. + /// public static async Task DetermineAndLoadLanguage() { var isoLanguageCode = DetermineCorrectLanguage(); @@ -73,12 +87,20 @@ public static async Task DetermineAndLoadLanguage() await LoadLanguage(isoLanguageCode).ConfigureAwait(false); } + /// + /// Retrieves a list of available language files in the language directory. + /// + /// An enumerable collection of paths to available language JSON files. public static IEnumerable GetLanguages() { var languagesDirectory = GetLanguagesDirectory(); return Directory.EnumerateFiles(languagesDirectory, "*.json", SearchOption.TopDirectoryOnly); } + /// + /// Changes the application's language by loading the corresponding language file. + /// + /// The index of the language to be changed. public static async Task ChangeLanguage(int language) { var choice = (Languages)language; @@ -88,6 +110,11 @@ public static async Task ChangeLanguage(int language) await SettingsHelper.SaveSettingsAsync().ConfigureAwait(false); } + /// + /// Determines the file path for the specified ISO language code. + /// + /// The ISO language code representing the desired language. + /// The file path of the matching language file, or the English language file as a fallback. private static string DetermineLanguageFilePath(string isoLanguageCode) { var languagesDirectory = GetLanguagesDirectory(); @@ -98,6 +125,12 @@ private static string DetermineLanguageFilePath(string isoLanguageCode) return matchingFiles.FirstOrDefault() ?? Path.Combine(languagesDirectory, "en.json"); } + /// + /// Loads a language from the specified file path. + /// + /// The path to the language JSON file. + /// A task that completes once the language is loaded. + /// Thrown when the language file is not found. private static async Task LoadLanguageFromFileAsync(string filePath) { if (!File.Exists(filePath)) @@ -110,11 +143,19 @@ private static async Task LoadLanguageFromFileAsync(string filePath) Translation = language; } + /// + /// Retrieves the directory path where language files are stored. + /// + /// The path to the language files directory. private static string GetLanguagesDirectory() { return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config/Languages/"); } - + + /// + /// Determines the correct language code based on the system's current UI culture. + /// + /// The ISO language code to use for translations. public static string DetermineCorrectLanguage() { var userCulture = CultureInfo.CurrentUICulture;