mirror of
https://github.com/hmaxnl/DotBased.git
synced 2025-01-18 18:14:20 +01:00
Rework: saving regions in dictionary
This commit is contained in:
parent
74d01fc648
commit
552085b478
|
@ -9,6 +9,7 @@ var serilogLogger = SetupSerilog();
|
|||
LogService.AddLogAdapter(new SerilogAdapter(serilogLogger));
|
||||
var logger = LogService.RegisterLogger(typeof(Program));
|
||||
|
||||
logger.Information("Whoah... Hi!");
|
||||
|
||||
Console.ReadKey(); // Hold console app open.
|
||||
return;
|
||||
|
|
|
@ -5,7 +5,7 @@ namespace DotBased.Utilities;
|
|||
public static class Culture
|
||||
{
|
||||
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>
|
||||
/// 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>
|
||||
public static IEnumerable<CultureInfo> GetSystemCultures()
|
||||
{
|
||||
//TODO: Probally need some internal caching for this
|
||||
if (_sysCultures.Count == 0)
|
||||
_sysCultures = CultureInfo.GetCultures(CultureTypes.AllCultures).ToList();
|
||||
return _sysCultures;
|
||||
|
@ -25,10 +24,17 @@ public static class Culture
|
|||
/// </summary>
|
||||
/// <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>
|
||||
public static IEnumerable<RegionInfo> GetRegions()
|
||||
public static Dictionary<string, RegionInfo> GetRegions()
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -62,13 +68,32 @@ public static class Culture
|
|||
/// </summary>
|
||||
/// <param name="amount">The amount to format</param>
|
||||
/// <param name="culture">The culture to be formatted in</param>
|
||||
/// <returns></returns>
|
||||
public static string AmountToCultureCurrency(double amount, CultureInfo culture) => string.Format(culture, "{0:C}", amount);
|
||||
/// <returns>Formatted amount in the given culture format</returns>
|
||||
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>
|
||||
/// Get a list of ISO 3 letter currency symbols.
|
||||
/// </summary>
|
||||
/// <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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user