LocalizationStreamParser should not fail on faulty PO files

Closes #7387
This commit is contained in:
Marek Dzikiewicz
2016-11-14 19:56:47 +01:00
committed by Sébastien Ros
parent c48635540b
commit fab73d7157
2 changed files with 34 additions and 5 deletions

View File

@@ -39,5 +39,21 @@ namespace Orchard.Tests.Localization {
Assert.AreEqual("Foo \"{0}\"", translations["~/themes/mytheme/views/myview.cshtml|foo \"{0}\""]);
}
[Test]
public void ShouldHandleUnclosedQuote() {
var parser = new LocalizationStreamParser();
var text = new StringBuilder();
text.AppendLine("#: ~/Themes/MyTheme/Views/MyView.cshtml");
text.AppendLine("msgctxt \"");
text.AppendLine("msgid \"Foo \\\"{0}\\\"\"");
text.AppendLine("msgstr \"Foo \\\"{0}\\\"\"");
var translations = new Dictionary<string, string>();
parser.ParseLocalizationStream(text.ToString(), translations, false);
Assert.AreEqual("Foo \"{0}\"", translations["|foo \"{0}\""]);
}
}
}

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using Orchard.Logging;
namespace Orchard.Localization.Services {
@@ -13,6 +14,12 @@ namespace Orchard.Localization.Services {
{ 't', '\t' }
};
public LocalizationStreamParser() {
Logger = NullLogger.Instance;
}
public ILogger Logger { get; }
public void ParseLocalizationStream(string text, IDictionary<string, string> translations, bool merge) {
var reader = new StringReader(text);
string poLine;
@@ -96,27 +103,33 @@ namespace Orchard.Localization.Services {
return sb == null ? str : sb.ToString();
}
private static string TrimQuote(string str) {
private string TrimQuote(string str) {
if (str.StartsWith("\"") && str.EndsWith("\"")) {
if (str.Length == 1) {
// Handle corner case - string containing single quote
Logger.Warning("Invalid localization string detected: " + str);
return "";
}
return str.Substring(1, str.Length - 2);
}
return str;
}
private static string ParseTranslation(string poLine) {
private string ParseTranslation(string poLine) {
return Unescape(TrimQuote(poLine.Substring(6).Trim()));
}
private static string ParseId(string poLine) {
private string ParseId(string poLine) {
return Unescape(TrimQuote(poLine.Substring(5).Trim()));
}
private static string ParseScope(string poLine) {
private string ParseScope(string poLine) {
return Unescape(TrimQuote(poLine.Substring(2).Trim()));
}
private static string ParseContext(string poLine) {
private string ParseContext(string poLine) {
return Unescape(TrimQuote(poLine.Substring(7).Trim()));
}
}