Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben2776 committed Nov 3, 2024
1 parent 9769f7a commit fcd02d8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
6 changes: 5 additions & 1 deletion src/PicView.Avalonia.MacOS/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,24 @@ 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);
_mainWindow = new MacMainWindow();
desktop.MainWindow = _mainWindow;
});

_vm = new MainViewModel(this);

await Dispatcher.UIThread.InvokeAsync(() =>
{
_mainWindow.DataContext = _vm;
Expand Down
6 changes: 4 additions & 2 deletions src/PicView.Avalonia.Win32/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(() =>
{
Expand All @@ -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;
Expand Down
59 changes: 50 additions & 9 deletions src/PicView.Core/Localization/TranslationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -38,9 +48,10 @@ public static string GetTranslation(string key)
}

/// <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);
Expand All @@ -66,19 +77,30 @@ public static async Task<bool> LoadLanguage(string isoLanguageCode)
}
}

/// <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();
SettingsHelper.Settings.UIProperties.UserLanguage = isoLanguageCode;
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;
Expand All @@ -88,6 +110,11 @@ public static async Task ChangeLanguage(int language)
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();
Expand All @@ -98,6 +125,12 @@ private static string DetermineLanguageFilePath(string isoLanguageCode)
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))
Expand All @@ -110,11 +143,19 @@ private static async Task LoadLanguageFromFileAsync(string filePath)
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;
Expand Down

0 comments on commit fcd02d8

Please sign in to comment.