Exemplo n.º 1
0
        private CldrRelativeTimeFormat(string locale, string type)
        {
            int pos = type.IndexOf('-');

            this.unit   = IntlProviderOptions.ParseEnum <RelativeTimeUnit>(pos < 0 ? type : type.Substring(0, pos));
            this.locale = locale;
        }
Exemplo n.º 2
0
        public ReadOnlyDictionary <PluralCategories, ReadOnlyDictionary <int, NumberFormatPattern> > GetCompactNotationFormats(NumberCompactDisplayFormat style)
        {
            XElement decimalFormats = elements.FirstOrDefault(v => v.Name.LocalName == "decimalFormats");

            if (decimalFormats == null)
            {
                return(this.Parent.GetCompactNotationFormats(style));
            }
            if (CldrUtility.IsAlias(decimalFormats, out string numberSystem))
            {
                return(GetFormat(locale, numberSystem).GetCompactNotationFormats(style));
            }
            Dictionary <PluralCategories, Dictionary <int, NumberFormatPattern> > dict = new Dictionary <PluralCategories, Dictionary <int, NumberFormatPattern> >();

            foreach (PluralCategories kind in pluralRules.EnumerateCategories())
            {
                dict[kind] = new Dictionary <int, NumberFormatPattern>();
            }
            XElement decimalFormat = decimalFormats.XPathSelectElement(String.Format("decimalFormatLength[@type = '{0}']/decimalFormat", IntlProviderOptions.ToStringValue(style)));

            foreach (XElement pattern in decimalFormat.XPathSelectElements("pattern"))
            {
                PluralCategories count = IntlProviderOptions.ParseEnum <PluralCategories>(pattern.Attribute("count").Value);
                dict[count][pattern.Attribute("type").Value.Length - 1] = ParseNumberFormatPattern(pattern.Value);
            }
            if (decimalFormat.Attribute("inherits") != null)
            {
                ReadOnlyDictionary <PluralCategories, ReadOnlyDictionary <int, NumberFormatPattern> > parent = this.Parent.GetCompactNotationFormats(style);
                foreach (KeyValuePair <PluralCategories, ReadOnlyDictionary <int, NumberFormatPattern> > e in parent)
                {
                    CldrUtility.CopyPatternFromParent(dict[e.Key], e.Value);
                }
            }
            return(new ReadOnlyDictionary <PluralCategories, ReadOnlyDictionary <int, NumberFormatPattern> >(dict.ToDictionary(v => v.Key, v => new ReadOnlyDictionary <int, NumberFormatPattern>(v.Value))));
        }
Exemplo n.º 3
0
        public static EcmaValue ResolvedOptions([This] EcmaValue thisValue)
        {
            PluralRules      rules      = thisValue.GetUnderlyingObject <PluralRules>();
            List <EcmaValue> categories = new List <EcmaValue>();

            foreach (KeyValuePair <string, PluralCategories> e in StringValueMap <PluralCategories> .Default.Forward)
            {
                if ((rules.PluralCategories & e.Value) != 0)
                {
                    categories.Add(e.Key);
                }
            }
            EcmaObject obj = new EcmaObject();

            obj.CreateDataPropertyOrThrow(PropertyKey.Locale, rules.Locale);
            obj.CreateDataPropertyOrThrow(PropertyKey.PluralCategories, new EcmaArray(categories));
            obj.CreateDataPropertyOrThrow(PropertyKey.Type, IntlProviderOptions.ToStringValue(rules.Type));
            if (rules.Digits.UseSignificantDigits)
            {
                obj.CreateDataPropertyOrThrow(PropertyKey.MinimumSignificantDigits, rules.Digits.MinimumSignificantDigits);
                obj.CreateDataPropertyOrThrow(PropertyKey.MaximumSignificantDigits, rules.Digits.MaximumSignificantDigits);
            }
            else
            {
                obj.CreateDataPropertyOrThrow(PropertyKey.MinimumIntegerDigits, rules.Digits.MinimumSignificantDigits);
                obj.CreateDataPropertyOrThrow(PropertyKey.MinimumFractionDigits, rules.Digits.MinimumFractionDigits);
                obj.CreateDataPropertyOrThrow(PropertyKey.MaximumFractionDigits, rules.Digits.MaximumFractionDigits);
            }
            return(obj);
        }
Exemplo n.º 4
0
        public static EcmaValue ResolvedOptions([This] EcmaValue thisValue)
        {
            ListFormat formatter = thisValue.GetUnderlyingObject <ListFormat>(); EcmaObject obj = new EcmaObject();

            obj.CreateDataPropertyOrThrow(PropertyKey.Locale, formatter.Locale);
            obj.CreateDataPropertyOrThrow(PropertyKey.Style, IntlProviderOptions.ToStringValue(formatter.Style));
            obj.CreateDataPropertyOrThrow(PropertyKey.Type, IntlProviderOptions.ToStringValue(formatter.Type));
            return(obj);
        }
Exemplo n.º 5
0
        private static string GetLdmlFieldType(RelativeTimeUnit unit, RelativeTimeStyle style)
        {
            string str = IntlProviderOptions.ToStringValue(unit);

            if (style == RelativeTimeStyle.Long)
            {
                return(str);
            }
            return(str + "-" + IntlProviderOptions.ToStringValue(style));
        }
Exemplo n.º 6
0
        public static EcmaValue ResolvedOptions([This] EcmaValue thisValue)
        {
            RelativeTimeFormat formatter = thisValue.GetUnderlyingObject <RelativeTimeFormat>();
            EcmaObject         obj       = new EcmaObject();

            obj.CreateDataPropertyOrThrow(PropertyKey.Locale, formatter.Locale);
            obj.CreateDataPropertyOrThrow(PropertyKey.Style, IntlProviderOptions.ToStringValue(formatter.Style));
            obj.CreateDataPropertyOrThrow(PropertyKey.Numeric, IntlProviderOptions.ToStringValue(formatter.Numeric));
            obj.CreateDataPropertyOrThrow(PropertyKey.NumberingSystem, formatter.NumberingSystem);
            return(obj);
        }
Exemplo n.º 7
0
        private ReadOnlyDictionary <PluralCategories, string> GetUnitSubFormat(string unit, UnitDisplayFormat format, bool perUnit = false)
        {
            XDocument units      = CldrUtility.LoadXml("Codeless.Ecma.Intl.Data.units.xml.gz");
            XElement  unitLength = units.XPathSelectElement(String.Format("/root/units[@locale = '{0}']/unitLength[@type = '{1}']", locale, IntlProviderOptions.ToStringValue(format)));

            if (unitLength == null)
            {
                return(this.Parent.GetUnitSubFormat(unit, format));
            }
            if (CldrUtility.IsAlias(unitLength, out string type))
            {
                return(GetUnitSubFormat(unit, IntlProviderOptions.ParseEnum <UnitDisplayFormat>(type), perUnit));
            }
            bool hasInheritedValues = unitLength.Attribute("inherits") != null;

            if (unit == "per")
            {
                XElement perCompountUnit = unitLength.XPathSelectElement("compoundUnit[@type = 'per']/compoundUnitPattern");
                if (perCompountUnit == null)
                {
                    return(hasInheritedValues ? this.Parent.GetUnitSubFormat(unit, format) : null);
                }
                return(CreateSingleResult(perCompountUnit.Value));
            }
            XElement unitElm = unitLength.XPathSelectElement(String.Format("unit[substring-after(@type, '-') = '{0}']", unit));

            if (unitElm == null)
            {
                return(hasInheritedValues ? this.Parent.GetUnitSubFormat(unit, format) : null);
            }
            if (perUnit)
            {
                XElement perUnitPattern = unitElm.XPathSelectElement("perUnitPattern");
                if (perUnitPattern == null)
                {
                    return(hasInheritedValues ? this.Parent.GetUnitSubFormat(unit, format) : null);
                }
                return(CreateSingleResult(perUnitPattern.Value));
            }
            Dictionary <PluralCategories, string> dict = new Dictionary <PluralCategories, string>();

            foreach (XElement unitPattern in unitElm.XPathSelectElements("unitPattern"))
            {
                dict[IntlProviderOptions.ParseEnum <PluralCategories>(unitPattern.Attribute("count").Value)] = unitPattern.Value;
            }
            if (unitElm.Attribute("inherits") != null)
            {
                ReadOnlyDictionary <PluralCategories, string> parent = this.Parent.GetUnitSubFormat(unit, format);
                CldrUtility.CopyPatternFromParent(dict, parent);
            }
            return(new ReadOnlyDictionary <PluralCategories, string>(dict));
        }
Exemplo n.º 8
0
 private CldrPluralRules(XElement rules)
 {
     this.PluralCategories = PluralCategories.Other;
     foreach (XElement node in rules.Elements("pluralRule"))
     {
         PluralCategories category = IntlProviderOptions.ParseEnum <PluralCategories>(node.Attribute("count").Value);
         if (category != PluralCategories.Other)
         {
             string textContent = node.Value;
             ruleset[category]      = ParseExpression(textContent);
             this.PluralCategories |= category;
         }
     }
 }
Exemplo n.º 9
0
        public static EcmaValue ResolvedOptions([This] EcmaValue thisValue)
        {
            DateTimeFormat     formatter          = thisValue.GetUnderlyingObject <DateTimeFormat>();
            DateTimePartStyles dateTimePartStyles = formatter.DateTimePartStyles;
            EcmaObject         obj = new EcmaObject();

            obj.CreateDataPropertyOrThrow(PropertyKey.Locale, formatter.Locale);
            obj.CreateDataPropertyOrThrow(PropertyKey.Calendar, formatter.Calendar);
            obj.CreateDataPropertyOrThrow(PropertyKey.TimeZone, formatter.TimeZone);
            obj.CreateDataPropertyOrThrow(PropertyKey.Hour12, formatter.Hour12);
            obj.CreateDataPropertyOrThrow(PropertyKey.HourCycle, IntlProviderOptions.ToStringValue(formatter.HourCycle));
            obj.CreateDataPropertyOrThrow(PropertyKey.FormatMatcher, IntlProviderOptions.ToStringValue(formatter.FormatMatcher));
            if (dateTimePartStyles.Weekday != default)
            {
                obj.CreateDataPropertyOrThrow(PropertyKey.Weekday, IntlProviderOptions.ToStringValue(dateTimePartStyles.Weekday));
            }
            if (dateTimePartStyles.Era != default)
            {
                obj.CreateDataPropertyOrThrow(PropertyKey.Era, IntlProviderOptions.ToStringValue(dateTimePartStyles.Era));
            }
            if (dateTimePartStyles.Year != default)
            {
                obj.CreateDataPropertyOrThrow(PropertyKey.Year, IntlProviderOptions.ToStringValue(dateTimePartStyles.Year));
            }
            if (dateTimePartStyles.Month != default)
            {
                obj.CreateDataPropertyOrThrow(PropertyKey.Month, IntlProviderOptions.ToStringValue(dateTimePartStyles.Month));
            }
            if (dateTimePartStyles.Day != default)
            {
                obj.CreateDataPropertyOrThrow(PropertyKey.Day, IntlProviderOptions.ToStringValue(dateTimePartStyles.Day));
            }
            if (dateTimePartStyles.Hour != default)
            {
                obj.CreateDataPropertyOrThrow(PropertyKey.Hour, IntlProviderOptions.ToStringValue(dateTimePartStyles.Hour));
            }
            if (dateTimePartStyles.Minute != default)
            {
                obj.CreateDataPropertyOrThrow(PropertyKey.Minute, IntlProviderOptions.ToStringValue(dateTimePartStyles.Minute));
            }
            if (dateTimePartStyles.Second != default)
            {
                obj.CreateDataPropertyOrThrow(PropertyKey.Second, IntlProviderOptions.ToStringValue(dateTimePartStyles.Second));
            }
            if (dateTimePartStyles.TimeZoneName != default)
            {
                obj.CreateDataPropertyOrThrow(PropertyKey.TimeZoneName, IntlProviderOptions.ToStringValue(dateTimePartStyles.TimeZoneName));
            }
            return(obj);
        }
Exemplo n.º 10
0
        public static EcmaValue ResolvedOptions([This] EcmaValue thisValue)
        {
            Collator   collator = thisValue.GetUnderlyingObject <Collator>();
            EcmaObject obj      = new EcmaObject();

            obj.CreateDataPropertyOrThrow(PropertyKey.Locale, collator.Locale);
            obj.CreateDataPropertyOrThrow(PropertyKey.Usage, IntlProviderOptions.ToStringValue(collator.Usage));
            obj.CreateDataPropertyOrThrow(PropertyKey.Sensitivity, IntlProviderOptions.ToStringValue(collator.Sensitivity));
            obj.CreateDataPropertyOrThrow(PropertyKey.IgnorePunctuation, collator.IgnorePunctuation);
            obj.CreateDataPropertyOrThrow(PropertyKey.Collation, collator.Collation);
            obj.CreateDataPropertyOrThrow(PropertyKey.Numeric, collator.Numeric);
            obj.CreateDataPropertyOrThrow(PropertyKey.CaseFirst, IntlProviderOptions.ToStringValue(collator.CaseFirst));
            return(obj);
        }
Exemplo n.º 11
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);
        }
Exemplo n.º 12
0
        public static CldrRelativeTimeFormat Resolve(string locale, string type)
        {
            string key = locale + "/" + type;

            if (resolvedPatterns.TryGetValue(key, out CldrRelativeTimeFormat cached))
            {
                return(cached);
            }
            CldrRelativeTimeFormat formatter = new CldrRelativeTimeFormat(locale, type);
            XElement field = xDocument.XPathSelectElement(String.Format("/root/fields[@locale = '{0}']/field[@type = '{1}']", locale, type));

            if (field == null)
            {
                throw new InvalidOperationException("Unknown locale or type");
            }
            if (CldrUtility.IsAlias(field, out string use))
            {
                return(resolvedPatterns.GetOrAdd(key, Resolve(locale, use)));
            }
            bool hasInheritedValues = field.Attribute("inherits") != null;

            foreach (XElement relative in field.Elements("relative"))
            {
                int amount = Int32.Parse(relative.Attribute("type").Value);
                formatter.relative[amount] = FormattedString.Parse(relative.Value);
            }
            foreach (XElement relativeTime in field.Elements("relativeTime"))
            {
                Dictionary <PluralCategories, FormattedString> dict = relativeTime.Attribute("type").Value == "future" ? formatter.future : formatter.past;
                foreach (XElement child in relativeTime.Elements())
                {
                    PluralCategories category = IntlProviderOptions.ParseEnum <PluralCategories>(child.Attribute("count").Value);
                    dict[category] = FormattedString.Parse(child.Value);
                }
                hasInheritedValues |= relativeTime.Attribute("inherits") != null;
            }
            if (hasInheritedValues)
            {
                CldrRelativeTimeFormat parent = CldrUtility.GetParentPatterns(locale, type, Resolve);
                CldrUtility.CopyPatternFromParent(formatter.relative, parent.relative);
                CldrUtility.CopyPatternFromParent(formatter.future, parent.future);
                CldrUtility.CopyPatternFromParent(formatter.past, parent.past);
            }
            return(resolvedPatterns.GetOrAdd(key, formatter));
        }
Exemplo n.º 13
0
        public static EcmaValue ResolvedOptions([This] EcmaValue thisValue)
        {
            NumberFormat formatter = thisValue.GetUnderlyingObject <NumberFormat>();
            EcmaObject   obj       = new EcmaObject();

            obj.CreateDataPropertyOrThrow(PropertyKey.Locale, formatter.Locale);
            obj.CreateDataPropertyOrThrow(PropertyKey.NumberingSystem, formatter.NumberingSystem);
            obj.CreateDataPropertyOrThrow(PropertyKey.Style, IntlProviderOptions.ToStringValue(formatter.Style));
            obj.CreateDataPropertyOrThrow(PropertyKey.Notation, IntlProviderOptions.ToStringValue(formatter.Notation));
            obj.CreateDataPropertyOrThrow(PropertyKey.CompactDisplay, IntlProviderOptions.ToStringValue(formatter.CompactDisplay));
            obj.CreateDataPropertyOrThrow(PropertyKey.SignDisplay, IntlProviderOptions.ToStringValue(formatter.SignDisplay));
            obj.CreateDataPropertyOrThrow(PropertyKey.UseGrouping, formatter.UseGrouping);
            switch (formatter.Style)
            {
            case NumberStyle.Currency:
                obj.CreateDataPropertyOrThrow(PropertyKey.Currency, formatter.Currency);
                obj.CreateDataPropertyOrThrow(PropertyKey.CurrencyDisplay, IntlProviderOptions.ToStringValue(formatter.CurrencyDisplay));
                obj.CreateDataPropertyOrThrow(PropertyKey.CurrencySign, IntlProviderOptions.ToStringValue(formatter.CurrencySign));
                break;

            case NumberStyle.Unit:
                obj.CreateDataPropertyOrThrow(PropertyKey.Unit, formatter.Unit);
                obj.CreateDataPropertyOrThrow(PropertyKey.UnitDisplay, IntlProviderOptions.ToStringValue(formatter.UnitDisplay));
                break;
            }
            if (formatter.Digits.UseSignificantDigits)
            {
                obj.CreateDataPropertyOrThrow(PropertyKey.MinimumSignificantDigits, formatter.Digits.MinimumSignificantDigits);
                obj.CreateDataPropertyOrThrow(PropertyKey.MaximumSignificantDigits, formatter.Digits.MaximumSignificantDigits);
            }
            else
            {
                obj.CreateDataPropertyOrThrow(PropertyKey.MinimumIntegerDigits, formatter.Digits.MinimumSignificantDigits);
                obj.CreateDataPropertyOrThrow(PropertyKey.MinimumFractionDigits, formatter.Digits.MinimumFractionDigits);
                obj.CreateDataPropertyOrThrow(PropertyKey.MaximumFractionDigits, formatter.Digits.MaximumFractionDigits);
            }
            return(obj);
        }
Exemplo n.º 14
0
        public static string GetDefaultExtensionValue(string key)
        {
            Guard.ArgumentNotNull(key, "key");
            switch (key)
            {
            case "hc":
                return(IntlProviderOptions.ToStringValue(CldrUtility.GetDefaultHourCycle(IntlContext.RegionCode)));

            case "ca":
                foreach (string type in CldrUtility.GetPreferredCalenderTypes(IntlContext.RegionCode))
                {
                    if (SupportedCalendars.ContainsKey(type))
                    {
                        return(type);
                    }
                }
                break;
            }
            if (SupportedValues.ContainsKey(key))
            {
                return(SupportedValues[key][0]);
            }
            return(null);
        }
Exemplo n.º 15
0
 public static List <string> GetSupportedLocales(ICollection <string> availableLocales, ICollection <string> requestedLocales, IntlProviderOptions options)
 {
     if (options.LocaleMatcher == LocaleMatcher.BestFit)
     {
         return(BestFitSupportedLocales(availableLocales, requestedLocales));
     }
     return(LookupSupportedLocales(availableLocales, requestedLocales));
 }
Exemplo n.º 16
0
        private NumberFormatPattern GetCurrencyStyleFormat(CurrencySignFormat sign)
        {
            XElement currencyFormats = elements.FirstOrDefault(v => v.Name.LocalName == "currencyFormats");

            if (currencyFormats == null)
            {
                return(this.Parent.GetCurrencyStyleFormat(sign));
            }
            if (CldrUtility.IsAlias(currencyFormats, out string numberSystem))
            {
                return(GetFormat(locale, numberSystem).GetCurrencyStyleFormat(sign));
            }
            XElement currencyFormat = currencyFormats.XPathSelectElement(String.Format("currencyFormatLength[not(@type)]/currencyFormat[@type = '{0}']", IntlProviderOptions.ToStringValue(sign)));

            if (currencyFormat == null)
            {
                return(this.Parent.GetCurrencyStyleFormat(sign));
            }
            return(ParseNumberFormatPattern(currencyFormat.Value));
        }