turquoiseowl / i18n

Smart internationalization for ASP.NET
Other
556 stars 156 forks source link

Issue saving \t \\ and other special chars #332

Open magomigo opened 7 years ago

magomigo commented 7 years ago

Loading a po file containing the following msg string: msgstr "You are leaving this page.\r\nContinue?"

and saving the translation back to file, the message string is changed to: msgstr "You are leaving this page.\r\nContinue?"

which makes the line split into two sentences instead of writing back the original string. This is problably due to a missing escape of the \ char.

In our case it is important to preserve the "\r\n" as a string of chars because it is used in a javascript alert which otherwise throw a javascript error.

The same behaviour occurs with \t and other encoded chars.

A fix could be change the function escape from:

private string escape(string s)
{
            if (string.IsNullOrWhiteSpace(s))
            {
                return null;
            }
            return s.Replace("\"", "\\\"");
}

to:

private string escape(string s)
{
            if (string.IsNullOrWhiteSpace(s))
            {
                return null;
            }
            return s.Replace("\\","\\\\")
                    .Replace("\a", "\\a")
                    .Replace("\b", "\\b")
                    .Replace("\f", "\\f")
                    .Replace("\v", "\\v")
                    .Replace("\t", "\\t")
                    .Replace("\"", "\\\"");
}