From 74d01fc648b2825e85afb051c3e3903c793e3ebd Mon Sep 17 00:00:00 2001 From: Max <51083570+DRdrProfessor@users.noreply.github.com> Date: Sat, 4 May 2024 22:21:15 +0200 Subject: [PATCH] Added culture utilities --- CLI/Program.cs | 1 + DotBased/Utilities/Culture.cs | 74 +++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 DotBased/Utilities/Culture.cs diff --git a/CLI/Program.cs b/CLI/Program.cs index 444ad5a..e2d579d 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)); + Console.ReadKey(); // Hold console app open. return; diff --git a/DotBased/Utilities/Culture.cs b/DotBased/Utilities/Culture.cs new file mode 100644 index 0000000..bcc49bf --- /dev/null +++ b/DotBased/Utilities/Culture.cs @@ -0,0 +1,74 @@ +using System.Globalization; + +namespace DotBased.Utilities; + +public static class Culture +{ + private static List _sysCultures = new List(); + private static List _regions = new List(); + + /// + /// Get all system known cultures. + /// + /// Will be cached after first call, to clear the internal list call function + /// 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; + } + + /// + /// Get the regions the system knows. + /// + /// The list will internally be cached, clear the cache with the function + /// A list with regions from the system + public static IEnumerable GetRegions() + { + if (_regions.Count == 0) + _regions = GetSystemCultures().Where(cul => !cul.IsNeutralCulture).Where(cul => cul.LCID != 0x7F).Select(x => new RegionInfo(x.Name)).ToList(); + return _regions; + } + + /// + /// Clears the specified cache. + /// + public static void ClearCached(CacheType type) + { + switch (type) + { + case CacheType.Culture: + _sysCultures.Clear(); + break; + case CacheType.Region: + _regions.Clear(); + break; + default: + throw new ArgumentOutOfRangeException(nameof(type), type, null); + } + } + public enum CacheType + { + Culture, + Region + } + + public static class Currency + { + /// + /// Formats the currency amount to the given culture currency. + /// + /// The amount to format + /// The culture to be formatted in + /// + public static string AmountToCultureCurrency(double amount, CultureInfo culture) => string.Format(culture, "{0:C}", amount); + + /// + /// Get a list of ISO 3 letter currency symbols. + /// + /// List with ISOCurrencySymbols + public static IEnumerable GetIsoCurrencySymbols() => GetRegions().Select(x => x.ISOCurrencySymbol).Distinct().ToList(); + } +} \ No newline at end of file