Skip to content

Commit

Permalink
#5 - Escape special characters
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaskabrt committed Oct 13, 2018
1 parent 999fbce commit ea90166
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions PoExtractor/PotFile.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace PoExtractor.Core {
public class PotFile : IDisposable {
Expand All @@ -17,14 +18,14 @@ public void WriteRecord(LocalizableString record) {
}

if (!string.IsNullOrEmpty(record.Context)) {
_writer.WriteLine($"msgctxt \"{record.Context}\"");
_writer.WriteLine($"msgctxt \"{this.Escape(record.Context)}\"");
}

_writer.WriteLine($"msgid \"{record.Text}\"");
_writer.WriteLine($"msgid \"{this.Escape(record.Text)}\"");
if (string.IsNullOrEmpty(record.TextPlural)) {
_writer.WriteLine($"msgstr \"\"");
} else {
_writer.WriteLine($"msgid_plural \"{record.TextPlural}\"");
_writer.WriteLine($"msgid_plural \"{this.Escape(record.TextPlural)}\"");
_writer.WriteLine($"msgstr[0] \"\"");
}

Expand All @@ -49,5 +50,13 @@ protected virtual void Dispose(bool disposing) {
_writer.Dispose();
}
}

private string Escape(string text) {
var sb = new StringBuilder(text);
sb.Replace("\\", "\\\\"); // \ -> \\
sb.Replace("\"", "\\\""); // " -> \"

return sb.ToString();
}
}
}

0 comments on commit ea90166

Please sign in to comment.