diff --git a/I18N Commander/DataModel/Database/SettingNames.cs b/I18N Commander/DataModel/Database/SettingNames.cs index d59e7d9..d4ee3fb 100644 --- a/I18N Commander/DataModel/Database/SettingNames.cs +++ b/I18N Commander/DataModel/Database/SettingNames.cs @@ -2,6 +2,7 @@ public static class SettingNames { + public static readonly string DEEPL_SOURCE_CULTURE = "DeepL Source Culture"; public static readonly string CULTURE = "Culture"; public static readonly string DEEPL_ACTION = "DeepL Action"; public static readonly string DEEPL_API_KEY = "DeepL API Key"; diff --git a/I18N Commander/Processor/AppSettings.cs b/I18N Commander/Processor/AppSettings.cs index 8951fd2..047897f 100644 --- a/I18N Commander/Processor/AppSettings.cs +++ b/I18N Commander/Processor/AppSettings.cs @@ -215,6 +215,73 @@ public static class AppSettings #endregion + #region DeepL Source Culture + + private static int CACHE_DEEPL_SOURCE_CULTURE = -1; + private static bool CACHE_DEEPL_SOURCE_CULTURE_IS_LOADED = false; + + public static async Task SetDeepLSourceCulture(int cultureIndex) + { + // Update the cache: + CACHE_DEEPL_SOURCE_CULTURE = cultureIndex; + CACHE_DEEPL_SOURCE_CULTURE_IS_LOADED = true; + + // Get the database: + await using var db = ProcessorMeta.ServiceProvider.GetRequiredService(); + + // Check, if the setting is already set: + if (await db.Settings.FirstOrDefaultAsync(n => n.Code == SettingNames.DEEPL_SOURCE_CULTURE) is {} existingSetting) + { + existingSetting.IntegerValue = cultureIndex; + await db.SaveChangesAsync(); + } + + // Does not exist, so create it: + else + { + var setting = new Setting + { + Code = SettingNames.DEEPL_SOURCE_CULTURE, + IntegerValue = cultureIndex, + }; + + await db.Settings.AddAsync(setting); + await db.SaveChangesAsync(); + } + } + + public static async Task GetDeepLSourceCultureIndex() + { + // Check the cache: + if (CACHE_DEEPL_SOURCE_CULTURE_IS_LOADED) + return CACHE_DEEPL_SOURCE_CULTURE; + + // Get the database: + await using var db = ProcessorMeta.ServiceProvider.GetRequiredService(); + + // Check, if the setting is already set: + if (await db.Settings.FirstOrDefaultAsync(n => n.Code == SettingNames.DEEPL_SOURCE_CULTURE) is { } existingSetting) + return existingSetting.IntegerValue; + + // Does not exist, so create it: + var setting = new Setting + { + Code = SettingNames.DEEPL_SOURCE_CULTURE, + IntegerValue = -1, + }; + + await db.Settings.AddAsync(setting); + await db.SaveChangesAsync(); + + var cultureIndex = setting.IntegerValue; + CACHE_DEEPL_SOURCE_CULTURE = cultureIndex; + CACHE_DEEPL_SOURCE_CULTURE_IS_LOADED = true; + + return cultureIndex; + } + + #endregion + #endregion #region Translation Settings @@ -318,5 +385,28 @@ public static class AppSettings #endregion + #region Get a list of cultures + + public readonly record struct CultureInfo(string Code, int Index); + + public static async Task> GetCultureInfos() + { + // Get the number of cultures: + var numberOfCultures = await AppSettings.GetNumberCultures(); + + // Get the culture codes: + var cultureInfos = new List(); + for (var i = 1; i <= numberOfCultures; i++) + cultureInfos.Add(new CultureInfo + { + Code = await AppSettings.GetCultureCode(i), + Index = i, + }); + + return cultureInfos; + } + + #endregion + #endregion } \ No newline at end of file