コード例 #1
0
        protected override void InitInternal(ICollection <string> locales, NumberFormatOptions options)
        {
            Hashtable     ht            = new Hashtable();
            LocaleMatcher localeMatcher = options.LocaleMatcher;

            ht["nu"]             = options.NumberingSystem;
            this.Locale          = ResolveLocale(locales, localeMatcher, relevantExtensionKeys, ht, out ht);
            this.NumberingSystem = (string)ht["nu"];
            this.Style           = options.Style;

            string currency = options.Currency;

            if (currency != null)
            {
                if (!IntlUtility.IsWellFormedCurrencyCode(currency))
                {
                    throw new EcmaRangeErrorException("Invalid currency codes: {0}", currency);
                }
                this.Currency = currency;
            }
            this.CurrencyDisplay = options.CurrencyDisplay;
            this.CurrencySign    = options.CurrencySign;

            this.Unit = options.Unit;
            if (this.Unit != null)
            {
                if (!IntlUtility.IsWellFormedUnitIdentifier(this.Unit))
                {
                    throw new EcmaRangeErrorException("Invalid unit identifier: {0}", this.Unit);
                }
            }
            this.UnitDisplay = options.UnitDisplay;

            NumberFormatInfo numberFormat = CultureInfo.GetCultureInfo(this.Locale).NumberFormat;
            int defaultMinFractionDigits, defaultMaxFractionDigits;

            if (this.Style == NumberStyle.Currency)
            {
                if (currency == null)
                {
                    throw new EcmaTypeErrorException("Currency code is required with currency style");
                }
                defaultMinFractionDigits = numberFormat.CurrencyDecimalDigits;
                defaultMaxFractionDigits = numberFormat.CurrencyDecimalDigits;
            }
            else
            {
                if (this.Style == NumberStyle.Unit && this.Unit == null)
                {
                    throw new EcmaTypeErrorException("Unit is required with unit style");
                }
                defaultMinFractionDigits = 0;
                defaultMaxFractionDigits = this.Style == NumberStyle.Percent ? 0 : 3;
            }
            this.Notation = options.Notation;
            this.Digits   = options.GetNumberFormatDigitOptions(defaultMinFractionDigits, defaultMaxFractionDigits, this.Notation);

            NumberCompactDisplayFormat compactDisplay = options.CompactDisplay;

            if (this.Notation == NumberNotation.Compact)
            {
                this.CompactDisplay = compactDisplay;
            }
            this.UseGrouping    = options.UseGrouping;
            this.SignDisplay    = options.SignDisplay;
            this.BoundFormat    = Literal.FunctionLiteral(this.FormatInternal);
            this.formatProvider = CldrNumberFormat.GetFormat(this.Locale, this.NumberingSystem);
            this.pluralRules    = CldrPluralRules.Resolve(PluralRuleType.Cardinal, this.Locale);

            NumberFormatInfo formatter = formatProvider.FormatProvider;

            this.symbolType = new Dictionary <string, FormattedPartType> {
                [formatter.PositiveSign]           = FormattedPartType.PlusSign,
                [formatter.NegativeSign]           = FormattedPartType.MinusSign,
                [formatter.NumberGroupSeparator]   = FormattedPartType.Group,
                [formatter.NumberDecimalSeparator] = FormattedPartType.Decimal,
                [formatter.PercentSymbol]          = FormattedPartType.PercentSign,
                ["E"] = FormattedPartType.ExponentSeparator
            };
            this.styleFormats = GetStyleFormat().ToDictionary(v => v.Key, v => v.Value);
            if (this.Notation == NumberNotation.Compact)
            {
                this.compactNotations = formatProvider.GetCompactNotationFormats(this.CompactDisplay);
            }
            else if (this.Notation == NumberNotation.Scientific)
            {
                this.notationFormats = formatProvider.GetScientificNotationFormat().ToDisplayFormat(this);
            }
            else
            {
                this.notationFormats = formatProvider.GetDecimalNotationFormat().ToDisplayFormat(this);
            }
        }
コード例 #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))));
        }