From 585f375ac8b35defcf7bd5fc7cd5157ded0fbd0d Mon Sep 17 00:00:00 2001 From: MehdiK Date: Fri, 25 Apr 2014 16:35:20 +0430 Subject: [PATCH] simplified the registration mechanism & removed Localiser --- ...provalTest.approve_public_api.approved.txt | 4 ++- .../Configuration/FormatterRegistry.cs | 20 ++++++------ src/Humanizer/Configuration/Localiser.cs | 16 ---------- .../Configuration/LocaliserRegistry.cs | 32 ++++++++----------- .../NumberToWordsConverterRegistry.cs | 22 ++++++------- .../Configuration/OrdinalizerRegistry.cs | 12 +++---- src/Humanizer/Humanizer.csproj | 1 - 7 files changed, 44 insertions(+), 63 deletions(-) delete mode 100644 src/Humanizer/Configuration/Localiser.cs diff --git a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt index 0c2afa6ea..6f8a78b63 100644 --- a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt +++ b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt @@ -73,7 +73,9 @@ public class Configurator public class LocaliserRegistry`1 { - public void Register(System.Func<> localiser, System.Globalization.CultureInfo culture) { } + public LocaliserRegistry`1() { } + public void Register(string localeCode) { } + public void RegisterDefault() { } public T ResolveForUiCulture() { } } diff --git a/src/Humanizer/Configuration/FormatterRegistry.cs b/src/Humanizer/Configuration/FormatterRegistry.cs index 99b3963be..ec8b1c03a 100644 --- a/src/Humanizer/Configuration/FormatterRegistry.cs +++ b/src/Humanizer/Configuration/FormatterRegistry.cs @@ -4,17 +4,17 @@ namespace Humanizer.Configuration { internal class FormatterRegistry : LocaliserRegistry { - public FormatterRegistry() : base( - () => new DefaultFormatter(), - new Localiser("ro", () => new RomanianFormatter()), - new Localiser("ru", () => new RussianFormatter()), - new Localiser("ar", () => new ArabicFormatter()), - new Localiser("he", () => new HebrewFormatter()), - new Localiser("sk", () => new CzechSlovakPolishFormatter()), - new Localiser("cs", () => new CzechSlovakPolishFormatter()), - new Localiser("pl", () => new CzechSlovakPolishFormatter()), - new Localiser("sr", () => new SerbianFormatter())) + public FormatterRegistry() { + RegisterDefault(); + Register("ro"); + Register("ru"); + Register("ar"); + Register("he"); + Register("sk"); + Register("cs"); + Register("pl"); + Register("sr"); } } } \ No newline at end of file diff --git a/src/Humanizer/Configuration/Localiser.cs b/src/Humanizer/Configuration/Localiser.cs deleted file mode 100644 index b6a642ba9..000000000 --- a/src/Humanizer/Configuration/Localiser.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; - -namespace Humanizer.Configuration -{ - internal class Localiser - { - public Localiser(string localeCode, Func localiserFactory) - { - LocaleCode = localeCode; - LocaliserFactory = localiserFactory; - } - - public string LocaleCode { get; private set; } - public Func LocaliserFactory { get; private set; } - } -} \ No newline at end of file diff --git a/src/Humanizer/Configuration/LocaliserRegistry.cs b/src/Humanizer/Configuration/LocaliserRegistry.cs index 2caf70669..ada69cec9 100644 --- a/src/Humanizer/Configuration/LocaliserRegistry.cs +++ b/src/Humanizer/Configuration/LocaliserRegistry.cs @@ -8,20 +8,11 @@ namespace Humanizer.Configuration /// A registry of localised system components with their associated locales /// /// - public class LocaliserRegistry + public class LocaliserRegistry { - private readonly IDictionary> _localisers; - + private readonly IDictionary> _localisers = new Dictionary>(); private Lazy _defaultLocaliser; - internal LocaliserRegistry(Func defaultLocaliser, params Localiser[] localisers) - { - _defaultLocaliser = MakeLazy(defaultLocaliser); - _localisers = new Dictionary>(); - foreach (var localiser in localisers) - _localisers.Add(localiser.LocaleCode, MakeLazy(localiser.LocaliserFactory)); - } - Lazy MakeLazy(Func factoryMethod) { return new Lazy(factoryMethod); @@ -46,16 +37,21 @@ public T ResolveForUiCulture() } /// - /// Set the localiser for the culture provided or the default localiser if the culture is not provided + /// Registers the localiser for the culture provided /// - public void Register(Func localiser, CultureInfo culture = null) + public void Register(string localeCode) + where TLocaliser: T, new() { - var lazyLocaliser = MakeLazy(localiser); + _localisers[localeCode] = MakeLazy(() => new TLocaliser()); + } - if (culture == null) - _defaultLocaliser = lazyLocaliser; - else - _localisers[culture.Name] = lazyLocaliser; + /// + /// Registers the localiser as the catch all + /// + public void RegisterDefault() + where TLocaliser: T, new () + { + _defaultLocaliser = MakeLazy(() => new TLocaliser()); } } } diff --git a/src/Humanizer/Configuration/NumberToWordsConverterRegistry.cs b/src/Humanizer/Configuration/NumberToWordsConverterRegistry.cs index 6331fc884..40e18ef81 100644 --- a/src/Humanizer/Configuration/NumberToWordsConverterRegistry.cs +++ b/src/Humanizer/Configuration/NumberToWordsConverterRegistry.cs @@ -5,18 +5,18 @@ namespace Humanizer.Configuration internal class NumberToWordsConverterRegistry : LocaliserRegistry { public NumberToWordsConverterRegistry() - : base(() => new DefaultNumberToWordsConverter(), - new Localiser("en", () => new EnglishNumberToWordsConverter()), - new Localiser("ar", () => new ArabicNumberToWordsConverter()), - new Localiser("fa", () => new FarsiNumberToWordsConverter()), - new Localiser("es", () => new SpanishNumberToWordsConverter()), - new Localiser("pl", () => new PolishNumberToWordsConverter()), - new Localiser("pt-BR", () => new BrazilianPortugueseNumberToWordsConverter()), - new Localiser("ru", () => new RussianNumberToWordsConverter()), - new Localiser("fr", () => new FrenchNumberToWordsConverter()), - new Localiser("nl", () => new DutchNumberToWordsConverter()), - new Localiser("he", () => new HebrewNumberToWordsConverter())) { + RegisterDefault(); + Register("en"); + Register("ar"); + Register("fa"); + Register("es"); + Register("pl"); + Register("pt-BR"); + Register("ru"); + Register("fr"); + Register("nl"); + Register("he"); } } } \ No newline at end of file diff --git a/src/Humanizer/Configuration/OrdinalizerRegistry.cs b/src/Humanizer/Configuration/OrdinalizerRegistry.cs index 617594790..6e1249e32 100644 --- a/src/Humanizer/Configuration/OrdinalizerRegistry.cs +++ b/src/Humanizer/Configuration/OrdinalizerRegistry.cs @@ -5,12 +5,12 @@ namespace Humanizer.Configuration internal class OrdinalizerRegistry : LocaliserRegistry { public OrdinalizerRegistry() - : base(() => new DefaultOrdinalizer(), - new Localiser("en", () => new EnglishOrdinalizer()), - new Localiser("es", () => new SpanishOrdinalizer()), - new Localiser("pt-BR", () => new BrazilianPortugueseOrdinalizer()), - new Localiser("ru", () => new RussianOrdinalizer())) { + RegisterDefault(); + Register("en"); + Register("es"); + Register("ru"); + Register("pt-BR"); } } -} +} \ No newline at end of file diff --git a/src/Humanizer/Humanizer.csproj b/src/Humanizer/Humanizer.csproj index 19733e06b..4846af696 100644 --- a/src/Humanizer/Humanizer.csproj +++ b/src/Humanizer/Humanizer.csproj @@ -59,7 +59,6 @@ true -