Rework: saving regions in dictionary

This commit is contained in:
Max 2024-05-04 23:36:50 +02:00
parent 74d01fc648
commit 552085b478
3 changed files with 34 additions and 8 deletions

View File

@ -9,6 +9,7 @@ var serilogLogger = SetupSerilog();
LogService.AddLogAdapter(new SerilogAdapter(serilogLogger)); LogService.AddLogAdapter(new SerilogAdapter(serilogLogger));
var logger = LogService.RegisterLogger(typeof(Program)); var logger = LogService.RegisterLogger(typeof(Program));
logger.Information("Whoah... Hi!");
Console.ReadKey(); // Hold console app open. Console.ReadKey(); // Hold console app open.
return; return;

View File

@ -5,7 +5,7 @@ namespace DotBased.Utilities;
public static class Culture public static class Culture
{ {
private static List<CultureInfo> _sysCultures = new List<CultureInfo>(); private static List<CultureInfo> _sysCultures = new List<CultureInfo>();
private static List<RegionInfo> _regions = new List<RegionInfo>(); private static Dictionary<string, RegionInfo> _regions = new Dictionary<string, RegionInfo>();
/// <summary> /// <summary>
/// Get all system known cultures. /// Get all system known cultures.
@ -14,7 +14,6 @@ public static class Culture
/// <returns>The list with <see cref="CultureInfo"/>'s the system knows</returns> /// <returns>The list with <see cref="CultureInfo"/>'s the system knows</returns>
public static IEnumerable<CultureInfo> GetSystemCultures() public static IEnumerable<CultureInfo> GetSystemCultures()
{ {
//TODO: Probally need some internal caching for this
if (_sysCultures.Count == 0) if (_sysCultures.Count == 0)
_sysCultures = CultureInfo.GetCultures(CultureTypes.AllCultures).ToList(); _sysCultures = CultureInfo.GetCultures(CultureTypes.AllCultures).ToList();
return _sysCultures; return _sysCultures;
@ -25,10 +24,17 @@ public static class Culture
/// </summary> /// </summary>
/// <remarks>The list will internally be cached, clear the cache with the <see cref="ClearCached"/> function</remarks> /// <remarks>The list will internally be cached, clear the cache with the <see cref="ClearCached"/> function</remarks>
/// <returns>A list with regions from the system</returns> /// <returns>A list with regions from the system</returns>
public static IEnumerable<RegionInfo> GetRegions() public static Dictionary<string, RegionInfo> GetRegions()
{ {
if (_regions.Count == 0) if (_regions.Count == 0)
_regions = GetSystemCultures().Where(cul => !cul.IsNeutralCulture).Where(cul => cul.LCID != 0x7F).Select(x => new RegionInfo(x.Name)).ToList(); {
var cultureInfos = GetSystemCultures().Where(cul => !cul.IsNeutralCulture).Where(cul => cul.LCID != 0x7F);
foreach (var culture in cultureInfos)
{
var region = new RegionInfo(culture.Name);
_regions.Add(culture.Name, region);
}
}
return _regions; return _regions;
} }
@ -62,13 +68,32 @@ public static class Culture
/// </summary> /// </summary>
/// <param name="amount">The amount to format</param> /// <param name="amount">The amount to format</param>
/// <param name="culture">The culture to be formatted in</param> /// <param name="culture">The culture to be formatted in</param>
/// <returns></returns> /// <returns>Formatted amount in the given culture format</returns>
public static string AmountToCultureCurrency(double amount, CultureInfo culture) => string.Format(culture, "{0:C}", amount); public static string FormatAmountToCultureCurrency(double amount, CultureInfo culture) => string.Format(culture, "{0:C}", amount);
/// <summary>
/// Formats the amount to the culture which is found by the ISO currency symbol.
/// </summary>
/// <remarks>WIP & slow!</remarks>
/// <param name="amount">The amount to fomrat</param>
/// <param name="isoCurencySymbol">Three-character ISO 4217 currency symbol</param>
/// <returns>Formatted amount in the given ISO currency symbol</returns>
public static string FormatAmountFromIsoCurrency(double amount, string isoCurencySymbol)
{
var culture = CultureInfo.CurrentCulture;
var systemRegion = new RegionInfo(culture.Name);
if (systemRegion.ISOCurrencySymbol != isoCurencySymbol)
{
string? result = GetRegions().Where(x => x.Value.ISOCurrencySymbol == isoCurencySymbol).Select(x => x.Key).FirstOrDefault();
culture = GetSystemCultures().FirstOrDefault(x => x.Name == result);
}
return culture == null ? string.Empty : FormatAmountToCultureCurrency(amount, culture);
}
/// <summary> /// <summary>
/// Get a list of ISO 3 letter currency symbols. /// Get a list of ISO 3 letter currency symbols.
/// </summary> /// </summary>
/// <returns>List with ISOCurrencySymbols</returns> /// <returns>List with ISOCurrencySymbols</returns>
public static IEnumerable<string> GetIsoCurrencySymbols() => GetRegions().Select(x => x.ISOCurrencySymbol).Distinct().ToList(); public static IEnumerable<string> GetIsoCurrencySymbols() => GetRegions().Select(x => x.Value.ISOCurrencySymbol).Distinct().ToList();
} }
} }

View File

@ -1,2 +1,2 @@
# DotBased # DotBased
A library Small library that i use in my projects.