-
Notifications
You must be signed in to change notification settings - Fork 0
/
UpdateDraft.cs
170 lines (153 loc) · 5.64 KB
/
UpdateDraft.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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;
public static class UpdateDraft
{
private static string BaseDir { get; set; }
private static readonly Regex _rtlRegex = new Regex("[\u0591-\u07FF]");
private static readonly Dictionary<string, string> _forcedCodeToSignMap = new Dictionary<string, string>()
{
{ "USD", "$" },
{ "JPY", "¥" },
{ "GBP", "£" },
{ "KRW", "₩" },
{ "SGD", "S$" }
};
private static Lazy<List<XmlDocument>> _localeDocs = new Lazy<List<XmlDocument>>(() =>
{
var res = new List<XmlDocument>();
var dir = new DirectoryInfo(Path.Combine(BaseDir, "main"));
foreach (var fileInfo in dir.GetFiles("*.xml"))
{
if (fileInfo.FullName.EndsWith("brx.xml"))
{
// The Boro language contains many devanagari signs that are unreadable to other cultures
continue;
}
var xml = new XmlDocument { XmlResolver = null };
xml.LoadXml(File.ReadAllText(fileInfo.FullName));
res.Add(xml);
}
return res;
});
private static Lazy<HashSet<string>> _actualCodes = new Lazy<HashSet<string>>(() =>
{
var res = new HashSet<string>();
var xml = new XmlDocument { XmlResolver = null };
xml.LoadXml(File.ReadAllText(Path.Combine(BaseDir, "supplemental", "supplementalData.xml")));
foreach (XmlNode node in xml.SelectNodes("//supplementalData/currencyData/region/currency"))
{
var code = node.Attributes["iso4217"].Value.ToUpperInvariant();
var from = node.Attributes["from"];
var to = node.Attributes["to"];
if (from != null && to == null)
{
res.Add(code);
}
}
return res;
});
private static HashSet<string> GetDuplicatedSymbols()
{
var actualCodes = _actualCodes.Value;
var signToCurrency = new Dictionary<string, string>();
var skippedSigns = new HashSet<string>();
foreach (var xml in _localeDocs.Value)
{
foreach (XmlNode node in xml.SelectNodes("//ldml/numbers/currencies/currency"))
{
var code = node.Attributes["type"].Value.ToUpperInvariant();
if (!actualCodes.Contains(code))
{
continue;
}
var symbols = new HashSet<string>();
foreach (XmlNode symbolNode in node.SelectNodes("symbol[not(@draft)]"))
{
symbols.Add(symbolNode.InnerText);
}
foreach (var symbol in symbols)
{
string oldCode;
if (!signToCurrency.TryGetValue(symbol, out oldCode))
{
signToCurrency.Add(symbol, code);
}
else if (oldCode != code)
{
skippedSigns.Add(symbol);
}
}
}
}
return skippedSigns;
}
private static Dictionary<string, List<string>> GetCodeToSignMap()
{
var actualCodes = _actualCodes.Value;
var codeToSigns = new Dictionary<string, List<string>>();
var skippedSigns = GetDuplicatedSymbols();
foreach (var xml in _localeDocs.Value)
{
foreach (XmlNode node in xml.SelectNodes("//ldml/numbers/currencies/currency"))
{
var code = node.Attributes["type"].Value.ToUpperInvariant();
if (!actualCodes.Contains(code))
{
continue;
}
foreach (XmlNode symbolNode in node.SelectNodes("symbol[not(@draft)]"))
{
var sign = symbolNode.InnerText.Trim(' ', '\t', '\u200B');
if (sign.Length == 0)
{
continue;
}
string forcedSign;
_forcedCodeToSignMap.TryGetValue(code, out forcedSign);
if (sign == code ||
_rtlRegex.IsMatch(sign) ||
skippedSigns.Contains(sign) && sign != forcedSign)
{
continue;
}
List<string> oldSignList;
if (!codeToSigns.TryGetValue(code, out oldSignList))
{
oldSignList = new List<string>();
codeToSigns[code] = oldSignList;
}
if (!oldSignList.Contains(sign))
{
oldSignList.Add(sign);
}
}
}
}
foreach (var list in codeToSigns.Values)
{
list.Sort(StringComparer.Ordinal);
}
return codeToSigns;
}
public static void Generate(string cldrDir, string filePath)
{
BaseDir = cldrDir;
var res = new List<string>();
foreach (var pair in GetCodeToSignMap())
{
res.Add(pair.Key + ',' + string.Join("|", pair.Value.ToArray()));
}
res.Sort(StringComparer.Ordinal);
res.Insert(0, "Code,Signs");
File.WriteAllText(filePath, string.Join("\n", res));
}
public static void Main()
{
Generate(
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), @"CLDR\common"),
"../../../draft.csv");
}
}