示例#1
0
        /// <summary>
        ///   Creates or reuses a number formatter for the specified <see cref="Locale"/>.
        /// </summary>
        /// <param name="locale">
        ///   The locale.
        /// </param>
        /// <param name="options">
        ///   The options to apply when formating a number.
        /// </param>
        /// <returns>
        ///   The formatter that is the best for the <paramref name="locale"/>.
        /// </returns>
        public static INumberFormatter Create(Locale locale, NumberOptions options = null)
        {
            var             numberingSystem = NumberingSystem.Create(locale);
            NumberFormatter formatter;

            if (numberingSystem.IsNumeric)
            {
                formatter = new NumericFormatter
                {
                    Locale          = locale,
                    NumberingSystem = numberingSystem,
                    Symbols         = NumberSymbols.Create(locale),
                    Options         = options ?? new NumberOptions()
                };
            }

            else // Must be Algorithmic
            {
                formatter = new AlgorithmicFormatter
                {
                    Locale          = locale,
                    NumberingSystem = numberingSystem,
                    Options         = options ?? new NumberOptions()
                };
            }

            formatter.Resolve();
            return(formatter);
        }
示例#2
0
        /// <summary>
        ///   Creates or reuses a numbering system for the specified <see cref="Locale"/>.
        /// </summary>
        /// <param name="locale">
        ///   The locale.
        /// </param>
        /// <returns>
        ///   A numbering system that is the best for the <paramref name="locale"/>.
        /// </returns>
        /// <remarks>
        ///   The locale identifier can use the "u-nu-XXX" extension to specify a numbering system.
        ///   If the extension's numbering system doesn't exist or is not specified,
        ///   then the default numbering system for the locale is used.
        ///   <para>
        ///   The <see href="http://unicode.org/reports/tr35/tr35-numbers.html#otherNumberingSystems">other numbering systems</see>
        ///   ("u-nu-native", "u-nu-finance" and "u-nu-traditio") are also allowed.
        ///   </para>
        /// </remarks>
        public static NumberingSystem Create(Locale locale)
        {
            string name;

            if (locale.Id.UnicodeExtension.Keywords.TryGetValue("nu", out name))
            {
                try
                {
                    if (Others.Contains(name))
                    {
                        // Consistency is the hobgoblin of small minds.
                        var other = name == "traditio"
                            ? "traditional"
                            : name;
                        name = locale.Find($"ldml/numbers/otherNumberingSystems/{other}").Value;
                    }
                    return(NumberingSystem.Create(name));
                }
                catch (KeyNotFoundException)
                {
                    // eat it, will fallback to default numbering system.
                }
            }

            // Find the default numbering system for the locale.
            var ns = locale.Find("ldml/numbers/defaultNumberingSystem/text()").Value;

            return(NumberingSystem.Create(ns));
        }
示例#3
0
        /// <summary>
        ///   Creates the symbols for the specified <see cref="Locale"/>.
        /// </summary>
        /// <param name="locale">
        ///   The locale.
        /// </param>
        /// <returns>
        ///   The symbols that are the best for the <paramref name="locale"/>.
        /// </returns>
        public static NumberSymbols Create(Locale locale)
        {
            var ns      = NumberingSystem.Create(locale).Id;
            var path    = $"ldml/numbers/symbols[@numberSystem='{ns}']/";
            var symbols = new NumberSymbols
            {
                Decimal                = locale.Find(path + "decimal").Value,
                Exponential            = locale.Find(path + "exponential").Value,
                Group                  = locale.Find(path + "group").Value,
                Infinity               = locale.Find(path + "infinity").Value,
                List                   = locale.Find(path + "list").Value,
                MinusSign              = locale.Find(path + "minusSign").Value,
                NotANumber             = locale.Find(path + "nan").Value,
                PercentSign            = locale.Find(path + "percentSign").Value,
                PerMille               = locale.Find(path + "perMille").Value,
                PlusSign               = locale.Find(path + "plusSign").Value,
                SuperscriptingExponent = locale.Find(path + "superscriptingExponent").Value
            };

            var found = locale.FindOrDefault(path + "currencyDecimal");

            if (found != null)
            {
                symbols.CurrencyDecimal = found.Value;
            }

            found = locale.FindOrDefault(path + "currencyGroup");
            if (found != null)
            {
                symbols.CurrencyGroup = found.Value;
            }

            return(symbols);
        }
示例#4
0
        public void Create_From_Id()
        {
            var hanidec = NumberingSystem.Create("HANIDEC");

            Assert.AreEqual("hanidec", hanidec.Id);
            Assert.AreEqual("〇一二三四五六七八九", String.Join("", hanidec.Digits));
        }
示例#5
0
        public void Digits()
        {
            var ns = NumberingSystem.Create("mathbold");

            Assert.AreEqual("\U0001D7CE", ns.Digits[0]);
            Assert.AreEqual("\U0001D7D7", ns.Digits[9]);
        }
示例#6
0
        public void Create_Uses_A_Cache()
        {
            var a = NumberingSystem.Create("arab");
            var b = NumberingSystem.Create("arab");
            var c = NumberingSystem.Create("arabext");

            Assert.AreSame(a, b);
            Assert.AreNotSame(a, c);
        }
示例#7
0
        public void Create_From_Locale_Traditional()
        {
            var ta_default = Locale.Create("ta");

            Assert.AreEqual("latn", NumberingSystem.Create(ta_default).Id);

            var ta_traditional = Locale.Create("ta-u-nu-traditio");

            Assert.AreEqual("taml", NumberingSystem.Create(ta_traditional).Id);
        }
示例#8
0
        public void Create_From_Locale_Finance()
        {
            var zh_default = Locale.Create("zh");

            Assert.AreEqual("latn", NumberingSystem.Create(zh_default).Id);

            var zh_finance = Locale.Create("zh-u-nu-finance");

            Assert.AreEqual("hansfin", NumberingSystem.Create(zh_finance).Id);
        }
示例#9
0
        public void Create_From_Locale_Native()
        {
            var hi_default = Locale.Create("hi-IN");

            Assert.AreEqual("latn", NumberingSystem.Create(hi_default).Id);

            var hi_native = Locale.Create("hi-IN-u-nu-native");

            Assert.AreEqual("deva", NumberingSystem.Create(hi_native).Id);
        }
示例#10
0
        public void Create_From_Locale_Default()
        {
            var en = Locale.Create("en");

            Assert.AreEqual("latn", NumberingSystem.Create(en).Id);

            var ar = Locale.Create("ar");

            Assert.AreEqual("arab", NumberingSystem.Create(ar).Id);
        }
示例#11
0
        public void Create_From_Locale_Specified()
        {
            var a = Locale.Create("zh");

            Assert.AreEqual("latn", NumberingSystem.Create(a).Id);

            var b = Locale.Create("zh-u-nu-hanidec");

            Assert.AreEqual("hanidec", NumberingSystem.Create(b).Id);

            var c = Locale.Create("zh-u-nu-unknown");

            Assert.AreEqual("latn", NumberingSystem.Create(c).Id);
        }
示例#12
0
 public void Create_From_Undefined_Id()
 {
     ExceptionAssert.Throws <KeyNotFoundException>(() => NumberingSystem.Create("ns-is-not-defined"));
 }