Fixed culture setting handling for non-continuous indices

This commit is contained in:
tsommer committed 2022-08-02 20:43:12 +02:00
1 parent fd3d844df4
commit fee963f8bd
3 files changed
+34 -30

No files matched your search

+18 -18
View File
@@ -288,29 +288,30 @@ public static class AppSettings
#region Number of cultures
private static int CACHE_NUMBER_OF_CULTURES = -1;
private static List<int> CACHE_CULTURES_INDICES = new();
public static async Task<int> GetNumberCultures()
public static async Task<List<int>> GetCultureIndices()
{
// When possible, use the cache:
if (CACHE_NUMBER_OF_CULTURES > -1)
return CACHE_NUMBER_OF_CULTURES;
if (CACHE_CULTURES_INDICES.Count > 0)
return CACHE_CULTURES_INDICES;
// 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.StartsWith(SettingNames.CULTURE));
var list = await db.Settings.Where(n => n.Code.StartsWith(SettingNames.CULTURE)).OrderBy(n => n.IntegerValue).Select(n => n.IntegerValue).ToListAsync();
// We have at least one default culture:
if(count == 0)
if(list.Count == 0)
{
CACHE_NUMBER_OF_CULTURES = 1;
return 1;
// Add the default culture, which is en-US:
await AppSettings.SetCultureCode(1, "en-US");
}
CACHE_NUMBER_OF_CULTURES = count;
return count;
else
CACHE_CULTURES_INDICES = list;
return CACHE_CULTURES_INDICES;
}
#endregion
@@ -377,9 +378,8 @@ public static class AppSettings
await db.Settings.AddAsync(setting);
await db.SaveChangesAsync();
// Update the number of cultures cache:
if(CACHE_NUMBER_OF_CULTURES > -1)
CACHE_NUMBER_OF_CULTURES++;
// Update the list of cultures indices:
CACHE_CULTURES_INDICES.Add(index);
}
}
@@ -392,15 +392,15 @@ public static class AppSettings
public static async Task<List<CultureInfo>> GetCultureInfos()
{
// Get the number of cultures:
var numberOfCultures = await AppSettings.GetNumberCultures();
var cultureIndices = await AppSettings.GetCultureIndices();
// Get the culture codes:
var cultureInfos = new List<CultureInfo>();
for (var i = 1; i <= numberOfCultures; i++)
foreach (var cultureIndex in cultureIndices)
cultureInfos.Add(new CultureInfo
{
Code = await AppSettings.GetCultureCode(i),
Index = i,
Code = await AppSettings.GetCultureCode(cultureIndex),
Index = cultureIndex,
});
return cultureInfos;