Sharpen.MessageFormat.Format C# (CSharp) Method

Format() public static method

public static Format ( string message ) : string
message string
return string
        public static string Format(string message, params object[] args)
        {
            StringBuilder sb = new StringBuilder ();
            bool inQuote = false;
            bool inPlaceholder = false;
            int argStartPos = 0;
            List<string> placeholderArgs = new List<string> (3);

            for (int n=0; n<message.Length; n++) {
                char c = message[n];
                if (c == '\'') {
                    if (!inQuote)
                        inQuote = true;
                    else if (n > 0 && message [n-1] == '\'') {
                        inQuote = false;
                    }
                    else {
                        inQuote = false;
                        continue;
                    }
                }
                else if (c == '{' && !inQuote) {
                    inPlaceholder = true;
                    argStartPos = n + 1;
                    continue;
                }
                else if (c == '}' && !inQuote && inPlaceholder) {
                    inPlaceholder = false;
                    placeholderArgs.Add (message.Substring (argStartPos, n - argStartPos));
                    AddFormatted (sb, placeholderArgs, args);
                    placeholderArgs.Clear ();
                    continue;
                }
                else if (c == ',' && inPlaceholder) {
                    placeholderArgs.Add (message.Substring (argStartPos, n - argStartPos));
                    argStartPos = n + 1;
                    continue;
                }
                else if (inPlaceholder)
                    continue;

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

Usage Example

Example #1
0
		public static string GetMessage(string messageId, object[] args)
		{
			Context cx = Context.GetCurrentContext();
			CultureInfo locale = cx == null ? CultureInfo.CurrentCulture : cx.GetLocale();
			// ResourceBundle does caching.
			ResourceBundle rb = ResourceBundle.GetBundle("org.mozilla.javascript.tools.resources.Messages", locale);
			string formatString;
			try
			{
				formatString = rb.GetString(messageId);
			}
			catch (MissingResourceException)
			{
				throw new Exception("no message resource found for message property " + messageId);
			}
			if (args == null)
			{
				return formatString;
			}
			else
			{
				MessageFormat formatter = new MessageFormat(formatString);
				return formatter.Format(args);
			}
		}
All Usage Examples Of Sharpen.MessageFormat::Format