Welcome to mirror list, hosted at ThFree Co, Russian Federation.

POProcessor.cs « StripMnemonics « po « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 172b2f41f28b8228701b8afe52981aafa5ddcd10 (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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace StripMnemonics
{
	public static class POProcessor
	{
		public static POFile Read (string poFile)
		{
			var po = new POFile {
				FileName = poFile
			};

			using (var stream = File.OpenRead (poFile))
			using (var reader = new StreamReader (stream)) {
				string line = reader.ReadLine ();

				// Read PO Copyright header.
				while (line.StartsWith ("# ", StringComparison.Ordinal)) {
					po.CopyrightHeader.Add (line);
					line = reader.ReadLine ();
				}

				po.Header = ReadBlock (ref line, reader, po);
				if (!po.NPluralsSet)
				{
					po.NPluralsSet = true;
					po.NPlurals = 2;
				}

				while (!reader.EndOfStream) {
					var block = ReadBlock (ref line, reader, po);
					if (block != null)
						po.Messages.Add (block);
				}
			}
			return po;
		}

		static POBlock ReadBlock (ref string line, StreamReader reader, POFile po)
		{
			var map = new HashSet<string> {
				{ "# " },
				{ "#. " },
				{ "#: " },
				{ "#, " },
				{ "#| msgid " },
			};

			var block = new POBlock ();
			bool init = false;

			do {
				foreach (var mapping in map) {
					if (line.StartsWith (mapping, StringComparison.Ordinal)) {
						block.Metadata.Add(line);
						goto read_next;
					}
				}

				string actual;
				bool readNext;
				if ((actual = ReadString(ref line, reader, "msgid ", out readNext, po)) != null)
				{
					block.Id = actual;
					init = true;
					if (!readNext)
						continue;
				}

				if ((actual = ReadString(ref line, reader, "msgid_plural ", out readNext, po)) != null)
				{
					block.IdPlural = actual;
					if (!readNext)
						continue;
				}

				if ((actual = ReadString(ref line, reader, "msgstr ", out readNext, po)) != null)
				{
					block.TranslatedString = actual;
					if (!readNext)
						continue;
				}

				if ((actual = ReadString(ref line, reader, "msgstr[0] ", out readNext, po)) != null)
				{
					block.SetTranslatedPlural(0, actual);
					if (!readNext)
						continue;
				}

				if ((actual = ReadString(ref line, reader, "msgstr[1] ", out readNext, po)) != null)
				{
					block.SetTranslatedPlural(1, actual);
					if (!readNext)
						continue;
				}

				if ((actual = ReadString(ref line, reader, "msgstr[2] ", out readNext, po)) != null)
				{
					block.SetTranslatedPlural(2, actual);
					if (!readNext)
						continue;
				}

			read_next:
				line = reader.ReadLine ();
			} while (!string.IsNullOrEmpty (line));

			if (init)
				return block;
			return null;
		}

		static string ReadString(ref string line, StreamReader reader, string header, out bool readNext, POFile po)
		{
			if (!line.StartsWith(header, StringComparison.Ordinal))
			{
				readNext = false;
				return null;
			}

			var actual = line.Substring(header.Length);
			actual = actual.Substring(Math.Min (1, actual.Length), Math.Max (actual.Length - 2, 0));
			bool isMultiline = string.IsNullOrEmpty(actual);
			if (!isMultiline)
			{
				readNext = true;
				return actual;
			}

			var multiLineString = new List<string>();

			line = reader.ReadLine();
			while (!string.IsNullOrEmpty(line) && line.StartsWith("\"", StringComparison.Ordinal))
			{
				var leadingStripped = line.Remove(0, 1);
				var stripped = leadingStripped.Remove(leadingStripped.Length - 1, 1);
				multiLineString.Add(stripped);

				// Parse number of plural forms to write for each string.
				if (line.StartsWith("\"Plural-Forms:", StringComparison.Ordinal))
				{
					var rest = line.Substring("\"Plural-Forms: ".Length).TrimEnd('\"').Split (';');
					int plurals;
					if (int.TryParse(rest[0].Trim().Substring("nplurals=".Length), out plurals))
					    po.NPlurals = plurals;
					po.NPluralsSet = true;
				}
				line = reader.ReadLine();
			}
			readNext = false;
			return string.Concat (multiLineString);
		}


		public static void Write(POFile po, string outPath)
		{
			using (var stream = File.Open(outPath, FileMode.Create))
			using (var writer = new StreamWriter(stream))
			{
				foreach (var line in po.CopyrightHeader)
					writer.WriteLine(line);

				writer.WriteLine("msgid \"\"");
				writer.WriteLine("msgstr \"\"");
				foreach (var line in po.Header.TranslatedString.Split(new string[] { "\\n" }, StringSplitOptions.RemoveEmptyEntries))
					writer.WriteLine($"\"{line}\\n\"");

				writer.WriteLine();
				bool isMessages = po.FileName.EndsWith("messages.po", StringComparison.OrdinalIgnoreCase);
				foreach (var block in po.Messages)
				{
					WriteBlock(block, writer, po, isMessages);
					writer.WriteLine();
				}
			}
		}

		static IEnumerable<string> LineWrap(string text, string kind)
		{
			// Gettext wraps at 80 chars, including quotes.
			// Also breaks on \n.

			// Take into account kind, quotes and space.
			if (text.Length + kind.Length + 3 > 80 || text.Contains("\\n"))
				yield return kind + " \"\"";
			else
			{
				yield return kind + $" \"{text}\"";
				yield break;
			}

			var words = text.Split(new char[] { ' ' });
			var sb = new StringBuilder();

			for (int i = 0; i < words.Length; ++i)
			{
				// Add the space into account.
				if (sb.Length + words[i].Length + 3 > 79)
				{
					yield return $"\"{sb.ToString()}\"";
					sb.Clear();
				}

				if (words[i].Contains("\\n"))
				{
					var split = words[i].Split(new string[] { "\\n" }, StringSplitOptions.None);
					for (int splitIndex = 0; splitIndex < split.Length - 1; ++splitIndex)
					{
						sb.Append(split[splitIndex]);
						sb.Append("\\n");
						yield return $"\"{sb.ToString()}\"";
						sb.Clear();
					}
					sb.Append(split[split.Length - 1]);
				}
				else
				{
					sb.Append(words[i]);
				}

				if (i != words.Length - 1)
					sb.Append(' ');
			}
			if (sb.Length > 0)
				yield return $"\"{sb.ToString()}\"";
			yield break;
		}

		static void WriteBlock(POBlock block, StreamWriter writer, POFile po, bool isMessages)
		{
			foreach (var item in block.Metadata)
				writer.WriteLine(item);

			foreach (var line in LineWrap(block.Id.Replace("\r\n", "\n"), "msgid"))
				writer.WriteLine(line);

			if (block.IdPlural != null)
			{
				foreach (var line in LineWrap(block.IdPlural.Replace("\r\n", "\n"), "msgid_plural"))
					writer.WriteLine(line);

				for (int i = 0; i < po.NPlurals; ++i)
				{
					var translatedPlural = block.GetTranslatedPlural(i);
					if (translatedPlural == null)
						continue;

					string value = isMessages ? "" : translatedPlural.Replace("\r\n", "\n");
					foreach (var line in LineWrap(value, $"msgstr[{i}]"))
						writer.WriteLine(line);
				}
			}
			else
			{
				string value = isMessages ? "" : block.TranslatedString.Replace("\r\n", "\n");
				foreach (var line in LineWrap(value, "msgstr"))
					writer.WriteLine(line);
			}
		}
	}
}