blob: 1d605cc47f0233da64819587242c5bcef74139dc (
plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Linq;
using System;
namespace NLangDetect.Core.Utils
{
public static class Messages
{
private static readonly Dictionary<string, string> _messages;
static Messages()
{
_messages = LoadMessages();
}
public static string getString(string key)
{
string value;
return
_messages.TryGetValue(key, out value)
? value
: string.Format("!{0}!", key);
}
private static Dictionary<string, string> LoadMessages()
{
var manifestName = typeof(Messages).Assembly.GetManifestResourceNames().FirstOrDefault(i => i.IndexOf("messages.properties", StringComparison.Ordinal) != -1) ;
Stream messagesStream =
typeof(Messages).Assembly
.GetManifestResourceStream(manifestName);
if (messagesStream == null)
{
throw new InternalException(string.Format("Couldn't get embedded resource named '{0}'.", manifestName));
}
using (messagesStream)
using (var sr = new StreamReader(messagesStream))
{
var messages = new Dictionary<string, string>();
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
if (string.IsNullOrEmpty(line))
{
continue;
}
string[] keyValue = line.Split('=');
if (keyValue.Length != 2)
{
throw new InternalException(string.Format("Invalid format of the 'Messages.properties' resource. Offending line: '{0}'.", line.Trim()));
}
string key = keyValue[0];
string value = UnescapeUnicodeString(keyValue[1]);
messages.Add(key, value);
}
return messages;
}
}
/// <remarks>
/// Taken from: http://stackoverflow.com/questions/1615559/converting-unicode-strings-to-escaped-ascii-string/1615860#1615860
/// </remarks>
private static string UnescapeUnicodeString(string s)
{
if (s == null)
{
return null;
}
return
Regex.Replace(
s,
@"\\u(?<Value>[a-zA-Z0-9]{4})",
match => ((char)int.Parse(match.Groups["Value"].Value, NumberStyles.HexNumber)).ToString());
}
}
}
|