I18NCommander/I18N Commander/Processor/AppSettings.cs

322 lines
10 KiB
C#
Raw Normal View History

using DataModel.Database;
using DataModel.Database.Common;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace Processor;
public static class AppSettings
{
2022-07-30 21:07:04 +00:00
#region DeepL Settings
2022-07-30 13:49:19 +00:00
#region DeepL Mode
2022-07-30 15:04:32 +00:00
private static SettingDeepLMode CACHE_DEEPL_MODE = SettingDeepLMode.DISABLED;
private static bool CACHE_DEEPL_MODE_IS_LOADED = false;
2022-07-30 14:09:58 +00:00
public static async Task SetDeepLMode(SettingDeepLMode mode)
{
// Convert the enum to its int value:
var intValue = (int)mode;
2022-07-30 15:04:32 +00:00
// Update the cache:
CACHE_DEEPL_MODE = mode;
CACHE_DEEPL_MODE_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_MODE) is {} existingSetting)
{
existingSetting.IntegerValue = intValue;
await db.SaveChangesAsync();
}
// Does not exist, so create it:
else
{
var setting = new Setting
{
Code = SettingNames.DEEPL_MODE,
IntegerValue = intValue,
};
await db.Settings.AddAsync(setting);
await db.SaveChangesAsync();
}
}
2022-07-30 14:09:58 +00:00
public static async Task<SettingDeepLMode> GetDeepLMode()
{
2022-07-30 15:04:32 +00:00
if (CACHE_DEEPL_MODE_IS_LOADED)
return CACHE_DEEPL_MODE;
// 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_MODE) is { } existingSetting)
2022-07-30 14:09:58 +00:00
return (SettingDeepLMode) existingSetting.IntegerValue;
// Does not exist, so create it:
var setting = new Setting
{
Code = SettingNames.DEEPL_MODE,
2022-07-30 14:09:58 +00:00
IntegerValue = (int)SettingDeepLMode.DISABLED,
};
await db.Settings.AddAsync(setting);
await db.SaveChangesAsync();
2022-07-30 15:04:32 +00:00
var mode = (SettingDeepLMode) setting.IntegerValue;
CACHE_DEEPL_MODE = mode;
CACHE_DEEPL_MODE_IS_LOADED = true;
return mode;
}
2022-07-30 13:49:19 +00:00
#endregion
#region DeepL API Key
2022-07-30 15:04:32 +00:00
private static string CACHE_DEEPL_API_KEY = string.Empty;
private static bool CACHE_DEEPL_API_KEY_IS_LOADED = false;
2022-07-30 13:49:19 +00:00
public static async Task SetDeepLAPIKey(string apiKey)
{
2022-07-30 15:04:32 +00:00
// Update the cache:
CACHE_DEEPL_API_KEY = apiKey;
CACHE_DEEPL_API_KEY_IS_LOADED = true;
2022-07-30 13:49:19 +00:00
// 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_API_KEY) is {} existingSetting)
{
existingSetting.TextValue = apiKey;
await db.SaveChangesAsync();
}
// Does not exist, so create it:
else
{
var setting = new Setting
{
Code = SettingNames.DEEPL_API_KEY,
TextValue = apiKey,
};
await db.Settings.AddAsync(setting);
await db.SaveChangesAsync();
}
}
public static async Task<string> GetDeepLAPIKey()
{
// Check the cache:
if (CACHE_DEEPL_API_KEY_IS_LOADED)
return CACHE_DEEPL_API_KEY;
2022-07-30 13:49:19 +00:00
// 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_API_KEY) is { } existingSetting)
return existingSetting.TextValue;
// Does not exist, so create it:
var setting = new Setting
{
Code = SettingNames.DEEPL_API_KEY,
TextValue = string.Empty,
};
await db.Settings.AddAsync(setting);
await db.SaveChangesAsync();
2022-07-30 15:04:32 +00:00
var key = setting.TextValue;
CACHE_DEEPL_API_KEY = key;
CACHE_DEEPL_API_KEY_IS_LOADED = true;
return key;
2022-07-30 13:49:19 +00:00
}
#endregion
2022-07-30 14:38:38 +00:00
#region DeepL Action
2022-07-30 15:04:32 +00:00
private static SettingDeepLAction CACHE_DEEPL_ACTION = SettingDeepLAction.MANUAL;
private static bool CACHE_DEEPL_ACTION_IS_LOADED = false;
2022-07-30 14:38:38 +00:00
public static async Task SetDeepLAction(SettingDeepLAction action)
{
// Convert the enum to its int value:
var intValue = (int)action;
2022-07-30 15:04:32 +00:00
// Update the cache:
CACHE_DEEPL_ACTION = action;
CACHE_DEEPL_ACTION_IS_LOADED = true;
2022-07-30 14:38:38 +00:00
// 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_ACTION) is {} existingSetting)
{
existingSetting.IntegerValue = intValue;
await db.SaveChangesAsync();
}
// Does not exist, so create it:
else
{
var setting = new Setting
{
Code = SettingNames.DEEPL_ACTION,
IntegerValue = intValue,
};
await db.Settings.AddAsync(setting);
await db.SaveChangesAsync();
}
}
public static async Task<SettingDeepLAction> GetDeepLAction()
{
// Check the cache:
if (CACHE_DEEPL_ACTION_IS_LOADED)
return CACHE_DEEPL_ACTION;
2022-07-30 14:38:38 +00:00
// 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_ACTION) is { } existingSetting)
return (SettingDeepLAction) existingSetting.IntegerValue;
// Does not exist, so create it:
var setting = new Setting
{
Code = SettingNames.DEEPL_ACTION,
IntegerValue = (int)SettingDeepLAction.MANUAL,
};
await db.Settings.AddAsync(setting);
await db.SaveChangesAsync();
2022-07-30 15:04:32 +00:00
var action = (SettingDeepLAction) setting.IntegerValue;
CACHE_DEEPL_ACTION = action;
CACHE_DEEPL_ACTION_IS_LOADED = true;
return action;
2022-07-30 14:38:38 +00:00
}
#endregion
2022-07-30 21:07:04 +00:00
#endregion
#region Translation Settings
#region Number of cultures
2022-07-30 21:17:57 +00:00
private static int CACHE_NUMBER_OF_CULTURES = -1;
2022-07-30 21:07:04 +00:00
public static async Task<int> GetNumberCultures()
{
2022-07-30 21:17:57 +00:00
// When possible, use the cache:
if (CACHE_NUMBER_OF_CULTURES > -1)
return CACHE_NUMBER_OF_CULTURES;
2022-07-30 21:07:04 +00:00
// 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)
2022-07-30 21:17:57 +00:00
{
CACHE_NUMBER_OF_CULTURES = 1;
2022-07-30 21:07:04 +00:00
return 1;
2022-07-30 21:17:57 +00:00
}
2022-07-30 21:07:04 +00:00
2022-07-30 21:17:57 +00:00
CACHE_NUMBER_OF_CULTURES = count;
2022-07-30 21:07:04 +00:00
return count;
}
#endregion
#region Get a culture
2022-07-30 21:14:39 +00:00
// Cache the cultures:
private static readonly Dictionary<int, string> CACHE_CULTURES = new();
2022-07-30 21:07:04 +00:00
public static async Task<string> GetCultureCode(int index)
{
2022-07-30 21:14:39 +00:00
// Check the cache:
if (CACHE_CULTURES.TryGetValue(index, out var cultureCode))
return cultureCode;
2022-07-30 21:07:04 +00:00
// 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);
2022-07-30 21:14:39 +00:00
// When the culture code is not set & index = 1, use the default culture en-US:
if(code is null && index == 1)
{
CACHE_CULTURES.Add(index, "en-US");
2022-07-30 21:07:04 +00:00
return "en-US";
2022-07-30 21:14:39 +00:00
}
2022-07-30 21:07:04 +00:00
2022-07-30 21:14:39 +00:00
// Update the cache & return the code:
var codeText = code?.TextValue ?? string.Empty;
CACHE_CULTURES[index] = codeText;
return codeText;
2022-07-30 21:07:04 +00:00
}
#endregion
#region Set a culture
public static async Task SetCultureCode(int index, string code)
{
2022-07-30 21:14:39 +00:00
// Update the cache:
CACHE_CULTURES[index] = code;
2022-07-30 21:07:04 +00:00
// 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();
2022-07-30 21:17:57 +00:00
// Update the number of cultures cache:
if(CACHE_NUMBER_OF_CULTURES > -1)
CACHE_NUMBER_OF_CULTURES++;
2022-07-30 21:07:04 +00:00
}
}
#endregion
#endregion
}