This repository has been archived by the owner on Nov 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
TextKvpReader.cs
56 lines (46 loc) · 1.76 KB
/
TextKvpReader.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
using System;
using System.Collections.Generic;
using System.IO;
namespace I18NPortable.Readers
{
public class TextKvpReader : ILocaleReader
{
public Dictionary<string, string> Read(Stream stream)
{
var translations = new Dictionary<string, string>();
using (var streamReader = new StreamReader(stream))
{
string key = null;
string value = null;
while (!streamReader.EndOfStream)
{
var line = streamReader.ReadLine();
var isEmpty = string.IsNullOrWhiteSpace(line);
var isComment = !isEmpty && line.Trim().StartsWith("#");
var isKeyValuePair = !isEmpty && !isComment && line.Contains("=");
if ((isEmpty || isComment || isKeyValuePair) && key != null && value != null)
{
translations.Add(key, value);
key = null;
value = null;
}
if (isEmpty || isComment)
continue;
if (isKeyValuePair)
{
var kvp = line.Split(new[] { '=' }, 2);
key = kvp[0].Trim();
value = kvp[1].Trim().UnescapeLineBreaks();
}
else if (key != null && value != null)
{
value = value + Environment.NewLine + line.Trim().UnescapeLineBreaks();
}
}
if (key != null && value != null)
translations.Add(key, value);
}
return translations;
}
}
}