Skip to content

Commit

Permalink
simplified the registration mechanism & removed Localiser
Browse files Browse the repository at this point in the history
  • Loading branch information
MehdiK committed Apr 25, 2014
1 parent 5ca0038 commit 585f375
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() { }
}

Expand Down
20 changes: 10 additions & 10 deletions src/Humanizer/Configuration/FormatterRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ namespace Humanizer.Configuration
{
internal class FormatterRegistry : LocaliserRegistry<IFormatter>
{
public FormatterRegistry() : base(
() => new DefaultFormatter(),
new Localiser<IFormatter>("ro", () => new RomanianFormatter()),
new Localiser<IFormatter>("ru", () => new RussianFormatter()),
new Localiser<IFormatter>("ar", () => new ArabicFormatter()),
new Localiser<IFormatter>("he", () => new HebrewFormatter()),
new Localiser<IFormatter>("sk", () => new CzechSlovakPolishFormatter()),
new Localiser<IFormatter>("cs", () => new CzechSlovakPolishFormatter()),
new Localiser<IFormatter>("pl", () => new CzechSlovakPolishFormatter()),
new Localiser<IFormatter>("sr", () => new SerbianFormatter()))
public FormatterRegistry()
{
RegisterDefault<DefaultFormatter>();
Register<RomanianFormatter>("ro");
Register<RussianFormatter>("ru");
Register<ArabicFormatter>("ar");
Register<HebrewFormatter>("he");
Register<CzechSlovakPolishFormatter>("sk");
Register<CzechSlovakPolishFormatter>("cs");
Register<CzechSlovakPolishFormatter>("pl");
Register<SerbianFormatter>("sr");
}
}
}
16 changes: 0 additions & 16 deletions src/Humanizer/Configuration/Localiser.cs

This file was deleted.

32 changes: 14 additions & 18 deletions src/Humanizer/Configuration/LocaliserRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,11 @@ namespace Humanizer.Configuration
/// A registry of localised system components with their associated locales
/// </summary>
/// <typeparam name="T"></typeparam>
public class LocaliserRegistry<T>
public class LocaliserRegistry<T>

This comment has been minimized.

Copy link
@mexx

mexx Apr 25, 2014

Collaborator

Maybe we should add another type parameter for the default component?
That way we ensure a default is always registered and RegisterDefault is only needed if the user wants to replace the default default. :)

{
private readonly IDictionary<string, Lazy<T>> _localisers;

private readonly IDictionary<string, Lazy<T>> _localisers = new Dictionary<string, Lazy<T>>();
private Lazy<T> _defaultLocaliser;

internal LocaliserRegistry(Func<T> defaultLocaliser, params Localiser<T>[] localisers)
{
_defaultLocaliser = MakeLazy(defaultLocaliser);
_localisers = new Dictionary<string, Lazy<T>>();
foreach (var localiser in localisers)
_localisers.Add(localiser.LocaleCode, MakeLazy(localiser.LocaliserFactory));
}

Lazy<T> MakeLazy(Func<T> factoryMethod)
{
return new Lazy<T>(factoryMethod);
Expand All @@ -46,16 +37,21 @@ public T ResolveForUiCulture()
}

/// <summary>
/// Set the localiser for the culture provided or the default localiser if the culture is not provided
/// Registers the localiser for the culture provided
/// </summary>
public void Register(Func<T> localiser, CultureInfo culture = null)
public void Register<TLocaliser>(string localeCode)
where TLocaliser: T, new()
{
var lazyLocaliser = MakeLazy(localiser);
_localisers[localeCode] = MakeLazy(() => new TLocaliser());

This comment has been minimized.

Copy link
@hazzik

hazzik Apr 25, 2014

Member

I dont like this as it would be compilled intoto awful Activator.CreateInstance(typeof(TLocaliser)). But Register method looks good. and so I would go with Func<> instead of new() constraint

}

if (culture == null)
_defaultLocaliser = lazyLocaliser;
else
_localisers[culture.Name] = lazyLocaliser;
/// <summary>
/// Registers the localiser as the catch all
/// </summary>
public void RegisterDefault<TLocaliser>()
where TLocaliser: T, new ()
{
_defaultLocaliser = MakeLazy(() => new TLocaliser());
}
}
}
22 changes: 11 additions & 11 deletions src/Humanizer/Configuration/NumberToWordsConverterRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ namespace Humanizer.Configuration
internal class NumberToWordsConverterRegistry : LocaliserRegistry<INumberToWordsConverter>
{
public NumberToWordsConverterRegistry()
: base(() => new DefaultNumberToWordsConverter(),
new Localiser<INumberToWordsConverter>("en", () => new EnglishNumberToWordsConverter()),
new Localiser<INumberToWordsConverter>("ar", () => new ArabicNumberToWordsConverter()),
new Localiser<INumberToWordsConverter>("fa", () => new FarsiNumberToWordsConverter()),
new Localiser<INumberToWordsConverter>("es", () => new SpanishNumberToWordsConverter()),
new Localiser<INumberToWordsConverter>("pl", () => new PolishNumberToWordsConverter()),
new Localiser<INumberToWordsConverter>("pt-BR", () => new BrazilianPortugueseNumberToWordsConverter()),
new Localiser<INumberToWordsConverter>("ru", () => new RussianNumberToWordsConverter()),
new Localiser<INumberToWordsConverter>("fr", () => new FrenchNumberToWordsConverter()),
new Localiser<INumberToWordsConverter>("nl", () => new DutchNumberToWordsConverter()),
new Localiser<INumberToWordsConverter>("he", () => new HebrewNumberToWordsConverter()))
{
RegisterDefault<DefaultNumberToWordsConverter>();
Register<EnglishNumberToWordsConverter>("en");
Register<ArabicNumberToWordsConverter>("ar");
Register<FarsiNumberToWordsConverter>("fa");
Register<SpanishNumberToWordsConverter>("es");
Register<PolishNumberToWordsConverter>("pl");
Register<BrazilianPortugueseNumberToWordsConverter>("pt-BR");
Register<RussianNumberToWordsConverter>("ru");
Register<FrenchNumberToWordsConverter>("fr");
Register<DutchNumberToWordsConverter>("nl");
Register<HebrewNumberToWordsConverter>("he");
}
}
}
12 changes: 6 additions & 6 deletions src/Humanizer/Configuration/OrdinalizerRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ namespace Humanizer.Configuration
internal class OrdinalizerRegistry : LocaliserRegistry<IOrdinalizer>
{
public OrdinalizerRegistry()
: base(() => new DefaultOrdinalizer(),
new Localiser<IOrdinalizer>("en", () => new EnglishOrdinalizer()),
new Localiser<IOrdinalizer>("es", () => new SpanishOrdinalizer()),
new Localiser<IOrdinalizer>("pt-BR", () => new BrazilianPortugueseOrdinalizer()),
new Localiser<IOrdinalizer>("ru", () => new RussianOrdinalizer()))
{
RegisterDefault<DefaultOrdinalizer>();
Register<EnglishOrdinalizer>("en");
Register<SpanishOrdinalizer>("es");
Register<RussianOrdinalizer>("ru");
Register<BrazilianPortugueseOrdinalizer>("pt-BR");
}
}
}
}
1 change: 0 additions & 1 deletion src/Humanizer/Humanizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
<ItemGroup>
<Compile Include="Configuration\Localiser.cs" />
<Compile Include="Localisation\Formatters\SerbianFormatter.cs" />
<Compile Include="Configuration\LocaliserRegistry.cs" />
<Compile Include="Configuration\FormatterRegistry.cs" />
Expand Down

0 comments on commit 585f375

Please sign in to comment.