Beispiel #1
0
 private CldrNumberFormat(string locale, string numberSystem)
 {
     this.locale       = locale;
     this.numberSystem = numberSystem;
     this.pluralRules  = CldrPluralRules.Resolve(PluralRuleType.Cardinal, locale);
     this.elements     = xDocument.XPathSelectElements(String.Format("/root/numbers[@locale = '{0}']/*[local-name() = 'currencies' or @numberSystem = '{1}']", locale, numberSystem)).ToArray();
 }
Beispiel #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);
        }
Beispiel #3
0
        public static CldrPluralRules Resolve(PluralRuleType type, string locale)
        {
            string normalizedLocale = IntlUtility.RemoveUnicodeExtensions(locale);
            ConcurrentDictionary <string, CldrPluralRules> cache = type == PluralRuleType.Cardinal ? cardinalRules : ordinalRules;

            return(cache.GetOrAdd(normalizedLocale, _ => {
                XDocument doc = CldrUtility.LoadXml(type == PluralRuleType.Cardinal ? CardinalFileName : OrdinalFileName);
                foreach (XElement node in doc.XPathSelectElements(Xpath))
                {
                    string[] locales = node.Attribute("locales").Value.Split(' ');
                    if (locales.Contains(normalizedLocale))
                    {
                        CldrPluralRules rule = new CldrPluralRules(node);
                        foreach (string v in locales)
                        {
                            cache.TryAdd(v, rule);
                        }
                        return rule;
                    }
                }
                return Resolve(type, CldrUtility.GetParentLocale(normalizedLocale));
            }));
        }