-
Notifications
You must be signed in to change notification settings - Fork 967
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
simplified the registration mechanism & removed Localiser
- Loading branch information
Showing
7 changed files
with
44 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.
Sorry, something went wrong. |
||
{ | ||
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); | ||
|
@@ -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.
Sorry, something went wrong.
hazzik
Member
|
||
} | ||
|
||
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. :)