Example #1
0
        public ReadOnlyDictionary <PluralCategories, NumberFormatPattern> GetUnitStyleFormat(string unit, UnitDisplayFormat format)
        {
            IDictionary <PluralCategories, string> pattern = GetUnitSubFormat(unit, format);

            if (pattern == null)
            {
                int pos = unit.IndexOf("-per-");
                if (pos <= 0)
                {
                    throw new ArgumentOutOfRangeException("unit");
                }
                string nUnit          = unit.Substring(0, pos);
                string dUnit          = unit.Substring(pos + 5);
                string perUnitPattern = GetUnitSubFormat(unit, format, true)[PluralCategories.Other];
                ReadOnlyDictionary <PluralCategories, string> nUnitPattern = GetUnitSubFormat(nUnit, format);
                if (perUnitPattern != null)
                {
                    pattern = nUnitPattern.ToDictionary(v => v.Key, v => String.Format(perUnitPattern, v.Value));
                }
                else
                {
                    string compoundUnitPattern = GetUnitSubFormat("per", format)[PluralCategories.Other];
                    string dUnitPattern        = GetUnitSubFormat(dUnit, format)[pluralRules.Match(1)];
                    pattern = nUnitPattern.ToDictionary(v => v.Key, v => String.Format(compoundUnitPattern, v.Value, dUnitPattern.Replace("{0}", "").Trim()));
                }
            }
            return(new ReadOnlyDictionary <PluralCategories, NumberFormatPattern>(pattern.ToDictionary(v => v.Key, v => new NumberFormatPattern(FormattedString.Parse(v.Value)))));
        }
Example #2
0
        public FormattedString Format(double value, NumberFormat formatter, RelativeTimeNumericFormat numeric, out string[] units)
        {
            FormattedString pattern;

            if (numeric == RelativeTimeNumericFormat.Auto)
            {
                double intValue = Math.Floor(value);
                if (intValue == value && intValue >= Int32.MinValue && intValue <= Int32.MaxValue)
                {
                    if (relative.TryGetValue((int)intValue, out pattern))
                    {
                        units = new string[1];
                        return(pattern);
                    }
                }
            }
            CldrPluralRules  pluralRules = CldrPluralRules.Resolve(PluralRuleType.Cardinal, locale);
            PluralCategories key         = pluralRules.Match(value);

            if ((value < 0 ? past : future).TryGetValue(key, out pattern))
            {
                FormattedString numParts = formatter.Format(Math.Abs(value));
                FormattedPart[] parts    = pattern.GetParts();
                int             index    = Array.FindIndex(parts, v => v.Type == FormattedPartType.Placeholder);
                string          unitStr  = IntlProviderOptions.ToStringValue(unit);
                if (numParts.PartCount > 1)
                {
                    List <FormattedPart> parts1 = new List <FormattedPart>(parts);
                    parts1.RemoveAt(index);
                    parts1.InsertRange(index, numParts);
                    units = new string[parts1.Count];
                    for (int j = 0, len2 = numParts.PartCount; j < len2; j++)
                    {
                        units[index + j] = unitStr;
                    }
                    return(new FormattedString(parts1));
                }
                else
                {
                    units        = new string[parts.Length];
                    units[index] = unitStr;
                    parts[index] = numParts[0];
                    return(new FormattedString(parts));
                }
            }
            units = new string[0];
            return(FormattedString.Empty);
        }