public string Format(
            string locale, 
            FormatterRequest request, 
            Dictionary<string, object> args, 
            object value,
            IMessageFormatter messageFormatter)
        {
            var parsed = this.ParseArguments(request);
            KeyedBlock other = null;
            foreach (var keyedBlock in parsed.KeyedBlocks)
            {
                var str = Convert.ToString(value);
                if (str == keyedBlock.Key)
                {
                    return messageFormatter.FormatMessage(keyedBlock.BlockText, args);
                }
                
                if (keyedBlock.Key == OtherKey)
                {
                    other = keyedBlock;
                }
            }

            if (other == null)
            {
                throw new MessageFormatterException(
                    "'other' option not found in pattern, and variable was not present in collection.");
            }

            return messageFormatter.FormatMessage(other.BlockText, args);
        }
        public string Format(
            string locale,
            FormatterRequest request,
            IDictionary <string, object> args,
            object value,
            IMessageFormatter messageFormatter)
        {
            var        parsed = this.ParseArguments(request);
            KeyedBlock other  = null;

            foreach (var keyedBlock in parsed.KeyedBlocks)
            {
                var str = Convert.ToString(value);
                if (str == keyedBlock.Key)
                {
                    return(messageFormatter.FormatMessage(keyedBlock.BlockText, args));
                }

                if (keyedBlock.Key == OtherKey)
                {
                    other = keyedBlock;
                }
            }

            if (other == null)
            {
                throw new MessageFormatterException(
                          "'other' option not found in pattern, and variable was not present in collection.");
            }

            return(messageFormatter.FormatMessage(other.BlockText, args));
        }
 /// <summary>
 ///     Formats the specified pattern with the specified data.
 /// </summary>
 /// <remarks>
 ///     This method calls <see cref="Format(string, IDictionary{string, object})"/>
 ///     on a singleton instance using a lock.
 ///     Do not use in a tight loop, as a lock is being used to ensure thread safety.
 /// </remarks>
 /// <param name="pattern">
 ///     The pattern.
 /// </param>
 /// <param name="data">
 ///     The data.
 /// </param>
 /// <returns>
 ///     The formatted message.
 /// </returns>
 public static string Format(string pattern, IDictionary <string, object?> data)
 {
     lock (Lock)
     {
         return(Instance.FormatMessage(pattern, data));
     }
 }
Example #4
0
        public ActionResult CompetitionEntry([Bind(Include = "CompetitionEntryId,FullName,Email,Gender,FavouriteWatermelonPlace,TermsAndConditionsAccepted")] CompetitionEntry competitionEntry)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    string errorMessage = "";
                    if (_competitionEntryService.InsertCompetitionEntry(competitionEntry, out errorMessage))
                    {
                        var messageBody = _messageFormatter.FormatMessage(competitionEntry);
                        _emailService.EmailMessage(competitionEntry.Email, "You have been entered", messageBody);
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, errorMessage);
                        return(View(competitionEntry));
                    }
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError(string.Empty, "An error occured trying to create your entry");
                    return(View("CompetitionEntry", competitionEntry));
                }
                return(RedirectToAction("Thankyou"));
            }

            return(View("CompetitionEntry", competitionEntry));
        }
Example #5
0
        /// <inheritdoc />
        public string FormatMessage(string message, params object[] arguments) =>

        _messageFormatter.FormatMessage
        (
            Locale,
            _messageRepository.GetMessage(Locale, Domain, message),
            arguments
        );
        public MessageResource PostMessage(Request request)
        {
            MessageResource rawMessage       = request.MapToMessageResource();
            var             formattedMessage = formatter.FormatMessage(rawMessage);

            repository.PostMessage(formattedMessage);

            return(repository.GetMessages().Where(p => p.MessageId == formattedMessage.MessageId).SingleOrDefault());
        }
        public async Task <TMessageOut> FormatMessage(TMessageIn message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            var intermediateMessage = await _sourceFormatter.FormatMessage(message);

            return(await _destinationFormatter.FormatMessage(intermediateMessage));
        }
Example #8
0
        /// <summary>
        ///     Using the specified parameters and arguments, a formatted string shall be returned.
        ///     The <see cref="IMessageFormatter" /> is being provided as well, to enable
        ///     nested formatting. This is only called if <see cref="CanFormat" /> returns true.
        ///     The args will always contain the <see cref="FormatterRequest.Variable" />.
        /// </summary>
        /// <param name="locale">
        ///     The locale being used. It is up to the formatter what they do with this information.
        /// </param>
        /// <param name="request">
        ///     The parameters.
        /// </param>
        /// <param name="args">
        ///     The arguments.
        /// </param>
        /// <param name="value">The value of <see cref="FormatterRequest.Variable"/> from the given args dictionary. Can be null.</param>
        /// <param name="messageFormatter">
        ///     The message formatter.
        /// </param>
        /// <returns>
        ///     The <see cref="string" />.
        /// </returns>
        public string Format(
            string locale,
            FormatterRequest request,
            IDictionary <string, object> args,
            object value,
            IMessageFormatter messageFormatter)
        {
            var    arguments       = this.ParseArguments(request);
            double offset          = 0;
            var    offsetExtension = arguments.Extensions.FirstOrDefault(x => x.Extension == "offset");

            if (offsetExtension != null)
            {
                offset = Convert.ToDouble(offsetExtension.Value);
            }

            var n          = Convert.ToDouble(value);
            var pluralized = new StringBuilder(this.Pluralize(locale, arguments, n, offset));
            var result     = this.ReplaceNumberLiterals(pluralized, n - offset);
            var formatted  = messageFormatter.FormatMessage(result, args);

            return(formatted);
        }
        /// <summary>
        ///     Using the specified parameters and arguments, a formatted string shall be returned.
        ///     The <see cref="IMessageFormatter" /> is being provided as well, to enable
        ///     nested formatting. This is only called if <see cref="CanFormat" /> returns true.
        ///     The <see cref="args" /> will always contain the <see cref="FormatterRequest.Variable" />.
        /// </summary>
        /// <param name="locale">
        ///     The locale being used. It is up to the formatter what they do with this information.
        /// </param>
        /// <param name="request">
        ///     The parameters.
        /// </param>
        /// <param name="args">
        ///     The arguments.
        /// </param>
        /// <param name="value">The value of <see cref="FormatterRequest.Variable"/> from the given <see cref="args"/> dictionary. Can be null.</param>
        /// <param name="messageFormatter">
        ///     The message formatter.
        /// </param>
        /// <returns>
        ///     The <see cref="string" />.
        /// </returns>
        public string Format(
            string locale, 
            FormatterRequest request, 
            Dictionary<string, object> args, 
            object value,
            IMessageFormatter messageFormatter)
        {
            var arguments = this.ParseArguments(request);
            double offset = 0;
            var offsetExtension = arguments.Extensions.FirstOrDefault(x => x.Extension == "offset");
            if (offsetExtension != null)
            {
                offset = Convert.ToDouble(offsetExtension.Value);
            }

            var n = Convert.ToDouble(value);
            var pluralized = new StringBuilder(this.Pluralize(locale, arguments, n, offset));
            var result = this.ReplaceNumberLiterals(pluralized, n - offset);
            var formatted = messageFormatter.FormatMessage(result, args);
            return formatted;
        }
Example #10
0
 public string Format(string message)
 {
     return(_strategy.FormatMessage(message));
 }