-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathTranslationService.cs
More file actions
77 lines (69 loc) · 2.34 KB
/
TranslationService.cs
File metadata and controls
77 lines (69 loc) · 2.34 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace Fluent.Net.SimpleExample
{
public class TranslationService
{
IEnumerable<MessageContext> _contexts;
public TranslationService(IEnumerable<MessageContext> contexts)
{
_contexts = contexts;
}
public static Dictionary<string, object> Args(string name, object value, params object[] args)
{
if (String.IsNullOrEmpty(name))
{
throw new ArgumentNullException(nameof(name));
}
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
if (args.Length % 2 != 0)
{
throw new ArgumentException("Expected a comma separated list " +
"of name, value arguments but the number of arguments is " +
"not a multiple of two", nameof(args));
}
var argsDic = new Dictionary<string, object>
{
{ name, value }
};
for (int i = 0; i < args.Length; i += 2)
{
name = args[i] as string;
if (String.IsNullOrEmpty(name))
{
throw new ArgumentException("Expected the argument at " +
$"index {i} to be a non-empty string",
nameof(args));
}
value = args[i + 1];
if (value == null)
{
throw new ArgumentNullException("args",
$"Expected the argument at index {i + 1} " +
"to be a non-null value");
}
argsDic.Add(name, value);
}
return argsDic;
}
public string GetString(string id, IDictionary<string, object> args = null,
ICollection<FluentError> errors = null)
{
foreach (var context in _contexts)
{
var msg = context.GetMessage(id);
if (msg != null)
{
return context.Format(msg, args, errors);
}
}
return "";
}
public CultureInfo Culture => _contexts.First().Culture;
}
}