diff --git a/CLI/Program.cs b/CLI/Program.cs index e2d579d..87bb37c 100644 --- a/CLI/Program.cs +++ b/CLI/Program.cs @@ -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; diff --git a/DotBased/Utilities/Culture.cs b/DotBased/Utilities/Culture.cs index bcc49bf..cc2c725 100644 --- a/DotBased/Utilities/Culture.cs +++ b/DotBased/Utilities/Culture.cs @@ -5,7 +5,7 @@ namespace DotBased.Utilities; public static class Culture { private static List _sysCultures = new List(); - private static List _regions = new List(); + private static Dictionary _regions = new Dictionary(); /// /// Get all system known cultures. @@ -14,7 +14,6 @@ public static class Culture /// The list with 's the system knows public static IEnumerable 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 /// /// The list will internally be cached, clear the cache with the function /// A list with regions from the system - public static IEnumerable GetRegions() + public static Dictionary 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 /// /// The amount to format /// The culture to be formatted in - /// - public static string AmountToCultureCurrency(double amount, CultureInfo culture) => string.Format(culture, "{0:C}", amount); + /// Formatted amount in the given culture format + public static string FormatAmountToCultureCurrency(double amount, CultureInfo culture) => string.Format(culture, "{0:C}", amount); + + /// + /// Formats the amount to the culture which is found by the ISO currency symbol. + /// + /// WIP & slow! + /// The amount to fomrat + /// Three-character ISO 4217 currency symbol + /// Formatted amount in the given ISO currency symbol + 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); + } /// /// Get a list of ISO 3 letter currency symbols. /// /// List with ISOCurrencySymbols - public static IEnumerable GetIsoCurrencySymbols() => GetRegions().Select(x => x.ISOCurrencySymbol).Distinct().ToList(); + public static IEnumerable GetIsoCurrencySymbols() => GetRegions().Select(x => x.Value.ISOCurrencySymbol).Distinct().ToList(); } } \ No newline at end of file diff --git a/README.md b/README.md index 2a00bcd..c811265 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ # DotBased -A library +Small library that i use in my projects.