Implemented list of culture info & DeepL source culture

This commit is contained in:
Thorsten Sommer 2022-08-01 21:04:47 +02:00
parent fa112de26d
commit fd3d844df4
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
2 changed files with 91 additions and 0 deletions

View File

@ -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";

View File

@ -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<DataContext>();
// 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<int> 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<DataContext>();
// 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<List<CultureInfo>> GetCultureInfos()
{
// Get the number of cultures:
var numberOfCultures = await AppSettings.GetNumberCultures();
// Get the culture codes:
var cultureInfos = new List<CultureInfo>();
for (var i = 1; i <= numberOfCultures; i++)
cultureInfos.Add(new CultureInfo
{
Code = await AppSettings.GetCultureCode(i),
Index = i,
});
return cultureInfos;
}
#endregion
#endregion
}