1
1
Ruben 11 сар өмнө
parent
commit
fcd02d815c

+ 5 - 1
src/PicView.Avalonia.MacOS/App.axaml.cs

@@ -48,12 +48,14 @@ public class App : Application, IPlatformSpecificService
         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 @@ public class App : Application, IPlatformSpecificService
             _mainWindow = new MacMainWindow();
             desktop.MainWindow = _mainWindow;
         });
+        
         _vm = new MainViewModel(this);
+        
         await Dispatcher.UIThread.InvokeAsync(() =>
         {
             _mainWindow.DataContext = _vm;

+ 4 - 2
src/PicView.Avalonia.Win32/App.axaml.cs

@@ -57,13 +57,13 @@ public partial class App : Application, IPlatformSpecificService
         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 @@ public partial class App : Application, IPlatformSpecificService
             _mainWindow = new WinMainWindow();
             desktop.MainWindow = _mainWindow;
         });
+        
         _vm = new MainViewModel(this);
+        
         await Dispatcher.UIThread.InvokeAsync(() =>
         {
             _mainWindow.DataContext = _vm;

+ 50 - 9
src/PicView.Core/Localization/TranslationHelper.cs

@@ -11,21 +11,31 @@ namespace PicView.Core.Localization;
 internal partial class LanguageSourceGenerationContext : JsonSerializerContext;
 
 /// <summary>
-/// Helper class for managing language-related tasks.
+/// Helper class for managing language-related tasks, including loading and switching languages.
 /// </summary>
 public static class TranslationHelper
 {
-    public static LanguageModel? Translation
-    {
-        get;
-        private set;
-    }
+    /// <summary>
+    /// The current language model containing translations.
+    /// </summary>
+    public static LanguageModel? Translation { get; private set; }
 
+    /// <summary>
+    /// Initializes the language model by setting all strings to empty.
+    /// </summary>
+    /// <remarks>
+    /// This is used to defer loading translations until explicitly needed.
+    /// </remarks>
     public static void Init()
     {
         Translation = new LanguageModel();
     }
 
+    /// <summary>
+    /// Retrieves the translated string for the given key.
+    /// </summary>
+    /// <param name="key">The key representing the translation string.</param>
+    /// <returns>The translated string, or the key itself if no translation is found.</returns>
     public static string GetTranslation(string key)
     {
         if (Translation == null)
@@ -38,9 +48,10 @@ public static class TranslationHelper
     }
 
     /// <summary>
-    /// 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.
     /// </summary>
-    /// <param name="isoLanguageCode">The culture code representing the desired language.</param>
+    /// <param name="isoLanguageCode">The ISO language code (e.g., 'en', 'da').</param>
+    /// <returns>Returns true if the language file was successfully loaded; false if an error occurred.</returns>
     public static async Task<bool> LoadLanguage(string isoLanguageCode)
     {
         var jsonLanguageFile = DetermineLanguageFilePath(isoLanguageCode);
@@ -66,6 +77,9 @@ public static class TranslationHelper
         }
     }
 
+    /// <summary>
+    /// Determines the correct language based on the system's current culture and loads the corresponding language file.
+    /// </summary>
     public static async Task DetermineAndLoadLanguage()
     {
         var isoLanguageCode = DetermineCorrectLanguage();
@@ -73,12 +87,20 @@ public static class TranslationHelper
         await LoadLanguage(isoLanguageCode).ConfigureAwait(false);
     }
 
+    /// <summary>
+    /// Retrieves a list of available language files in the language directory.
+    /// </summary>
+    /// <returns>An enumerable collection of paths to available language JSON files.</returns>
     public static IEnumerable<string> GetLanguages()
     {
         var languagesDirectory = GetLanguagesDirectory();
         return Directory.EnumerateFiles(languagesDirectory, "*.json", SearchOption.TopDirectoryOnly);
     }
 
+    /// <summary>
+    /// Changes the application's language by loading the corresponding language file.
+    /// </summary>
+    /// <param name="language">The index of the language to be changed.</param>
     public static async Task ChangeLanguage(int language)
     {
         var choice = (Languages)language;
@@ -88,6 +110,11 @@ public static class TranslationHelper
         await SettingsHelper.SaveSettingsAsync().ConfigureAwait(false);
     }
 
+    /// <summary>
+    /// Determines the file path for the specified ISO language code.
+    /// </summary>
+    /// <param name="isoLanguageCode">The ISO language code representing the desired language.</param>
+    /// <returns>The file path of the matching language file, or the English language file as a fallback.</returns>
     private static string DetermineLanguageFilePath(string isoLanguageCode)
     {
         var languagesDirectory = GetLanguagesDirectory();
@@ -98,6 +125,12 @@ public static class TranslationHelper
         return matchingFiles.FirstOrDefault() ?? Path.Combine(languagesDirectory, "en.json");
     }
 
+    /// <summary>
+    /// Loads a language from the specified file path.
+    /// </summary>
+    /// <param name="filePath">The path to the language JSON file.</param>
+    /// <returns>A task that completes once the language is loaded.</returns>
+    /// <exception cref="FileNotFoundException">Thrown when the language file is not found.</exception>
     private static async Task LoadLanguageFromFileAsync(string filePath)
     {
         if (!File.Exists(filePath))
@@ -110,11 +143,19 @@ public static class TranslationHelper
         Translation = language;
     }
 
+    /// <summary>
+    /// Retrieves the directory path where language files are stored.
+    /// </summary>
+    /// <returns>The path to the language files directory.</returns>
     private static string GetLanguagesDirectory()
     {
         return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config/Languages/");
     }
-    
+
+    /// <summary>
+    /// Determines the correct language code based on the system's current UI culture.
+    /// </summary>
+    /// <returns>The ISO language code to use for translations.</returns>
     public static string DetermineCorrectLanguage()
     {
         var userCulture = CultureInfo.CurrentUICulture;