Added culture settings

This commit is contained in:
Thorsten Sommer 2022-07-30 23:07:04 +02:00
parent 90be288d17
commit f27366e0a2
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
2 changed files with 79 additions and 0 deletions

View File

@ -2,6 +2,7 @@
public static class SettingNames public static class SettingNames
{ {
public static readonly string CULTURE = "Culture";
public static readonly string DEEPL_ACTION = "DeepL Action"; public static readonly string DEEPL_ACTION = "DeepL Action";
public static readonly string DEEPL_API_KEY = "DeepL API Key"; public static readonly string DEEPL_API_KEY = "DeepL API Key";
public static readonly string DEEPL_MODE = "DeepL Mode"; public static readonly string DEEPL_MODE = "DeepL Mode";

View File

@ -7,6 +7,8 @@ namespace Processor;
public static class AppSettings public static class AppSettings
{ {
#region DeepL Settings
#region DeepL Mode #region DeepL Mode
private static SettingDeepLMode CACHE_DEEPL_MODE = SettingDeepLMode.DISABLED; private static SettingDeepLMode CACHE_DEEPL_MODE = SettingDeepLMode.DISABLED;
@ -204,4 +206,80 @@ public static class AppSettings
} }
#endregion #endregion
#endregion
#region Translation Settings
#region Number of cultures
public static async Task<int> GetNumberCultures()
{
// Get the database:
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
// Count the number of cultures:
var count = await db.Settings.CountAsync(n => n.Code == SettingNames.CULTURE);
// We have at least one default culture:
if(count == 0)
return 1;
return count;
}
#endregion
#region Get a culture
public static async Task<string> GetCultureCode(int index)
{
// Get the database:
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
// Get the culture code:
var code = await db.Settings.FirstOrDefaultAsync(n => n.Code == SettingNames.CULTURE && n.IntegerValue == index);
// When the culture code is not set, fake the default culture as en-US:
if(code == null)
return "en-US";
// Return the code:
return code.TextValue;
}
#endregion
#region Set a culture
public static async Task SetCultureCode(int index, string code)
{
// 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.CULTURE && n.IntegerValue == index) is {} existingSetting)
{
existingSetting.TextValue = code;
await db.SaveChangesAsync();
}
// Does not exist, so create it:
else
{
var setting = new Setting
{
Code = SettingNames.CULTURE,
IntegerValue = index,
TextValue = code,
};
await db.Settings.AddAsync(setting);
await db.SaveChangesAsync();
}
}
#endregion
#endregion
} }