コード例 #1
0
        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));
        }
コード例 #2
0
        internal string Pluralize(string locale, ParsedArguments arguments, double n, double offset)
        {
            Pluralizer pluralizer;

            if (this.Pluralizers.TryGetValue(locale, out pluralizer) == false)
            {
                pluralizer = this.Pluralizers["en"];
            }

            var        pluralForm = pluralizer(n - offset);
            KeyedBlock other      = null;

            foreach (var keyedBlock in arguments.KeyedBlocks)
            {
                if (keyedBlock.Key == OtherKey)
                {
                    other = keyedBlock;
                }

                if (keyedBlock.Key.StartsWith("="))
                {
                    var numberLiteral = Convert.ToDouble(keyedBlock.Key.Substring(1));

                    // ReSharper disable once CompareOfFloatsByEqualityOperator
                    if (numberLiteral == n)
                    {
                        return(keyedBlock.BlockText);
                    }
                }

                if (keyedBlock.Key.StartsWith("%"))
                {
                    var numberLiteral = Convert.ToInt32(keyedBlock.Key.Substring(1));

                    if (n % numberLiteral == 0)
                    {
                        return(keyedBlock.BlockText);
                    }
                }

                if (keyedBlock.Key == pluralForm)
                {
                    return(keyedBlock.BlockText);
                }
            }

            if (other == null)
            {
                throw new MessageFormatterException("'other' option not found in pattern.");
            }

            return(other.BlockText);
        }