-
Notifications
You must be signed in to change notification settings - Fork 23
/
FormatterLibrary.cs
60 lines (52 loc) · 1.78 KB
/
FormatterLibrary.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// MessageFormat for .NET
// - FormatterLibrary.cs
// Author: Jeff Hansen <jeff@jeffijoe.com>
// Copyright (C) Jeff Hansen 2014. All rights reserved.
using System.Collections.Generic;
using Jeffijoe.MessageFormat.Formatting.Formatters;
namespace Jeffijoe.MessageFormat.Formatting
{
/// <summary>
/// Manages formatters to use.
/// </summary>
public class FormatterLibrary : List<IFormatter>, IFormatterLibrary
{
#region Constructors and Destructors
/// <summary>
/// Initializes a new instance of the <see cref="FormatterLibrary" /> class, and adds the default formatters.
/// </summary>
public FormatterLibrary()
{
this.Add(new VariableFormatter());
this.Add(new SelectFormatter());
this.Add(new PluralFormatter());
this.Add(new NumberFormatter());
this.Add(new DateFormatter());
this.Add(new TimeFormatter());
}
#endregion
#region Public Methods and Operators
/// <summary>
/// Gets the formatter to use. If none was found, throws an exception.
/// </summary>
/// <param name="request">
/// The request.
/// </param>
/// <returns>
/// The <see cref="IFormatter" />.
/// </returns>
/// <exception cref="FormatterNotFoundException">
/// Thrown when the formatter was not found.
/// </exception>
public IFormatter GetFormatter(FormatterRequest request)
{
foreach (var formatter in this)
{
if (formatter.CanFormat(request))
return formatter;
}
throw new FormatterNotFoundException(request);
}
#endregion
}
}