MonoDevelop.Gettext.StringEscaping.ToGettextFormat C# (CSharp) Method

ToGettextFormat() public static method

public static ToGettextFormat ( string text ) : string
text string
return string
        public static string ToGettextFormat(string text)
        {
            StringBuilder sb = new StringBuilder ();
            for (int i = 0; i < text.Length; i++) {
                char c = text[i];
                switch (c) {
                case '"':
                    sb.Append ("\\\"");
                    continue;

                //Gettext doesn't like escaping this
                //case '\'':
                //	sb.Append ("\\'");
                //	continue;

                //These shouldn't be in translations... but will be caught by IsControl
                //case '\a':
                //case '\b':
                //case '\f':
                //case '\v':

                //this doesn't matter since we're not dealing with trigraphs
                //case "?":
                //	sb.Append ("\\?");
                //	continue;

                case '\\':
                    sb.Append ("\\\\");
                    continue;
                case '\n':
                    sb.Append ("\\n");
                    continue;
                case '\r':
                    sb.Append ("\\r");
                    continue;
                case '\t':
                    sb.Append ("\\t");
                    continue;
                }

                if (c != '_' && char.IsControl (c))
                    throw new FormatException (String.Format (("Invalid character '{0}' in translatable string: '{1}'"),
                                                              c,
                                                              text));

                sb.Append (c);
            }
            return sb.ToString ();
        }

Usage Example

示例#1
0
        // Converts the header into string representation that can be directly written to .po file as msgid ""
        public string GetHeaderString(string lineDelimeter)
        {
            UpdateHeaderDict();
            StringBuilder sb = new StringBuilder();

            foreach (string key in headerEntries.Keys)
            {
                string value = String.Empty;
                if (headerEntries[key] != null)
                {
                    value = StringEscaping.ToGettextFormat(headerEntries[key]);
                }
                sb.AppendFormat("\"{0}: {1}\\n\"{2}", key, value, lineDelimeter);
            }
            return(sb.ToString());
        }
All Usage Examples Of MonoDevelop.Gettext.StringEscaping::ToGettextFormat