Exemplo n.º 1
0
        public void Can_parse_english()
        {
            var format = "nplurals=2; plural=n != 1;";

            var expression = PluralFormsExpression.Parse(format);

            Assert.Equal(2, expression.Count);

            Assert.Equal(0, expression.Get(1));

            Assert.Equal(1, expression.Get(0));
            Assert.Equal(1, expression.Get(2));
            Assert.Equal(1, expression.Get(3));
            Assert.Equal(1, expression.Get(4));
            Assert.Equal(1, expression.Get(5));
            Assert.Equal(1, expression.Get(6));
            Assert.Equal(1, expression.Get(7));
            Assert.Equal(1, expression.Get(8));
            Assert.Equal(1, expression.Get(9));
            Assert.Equal(1, expression.Get(10));
            Assert.Equal(1, expression.Get(11));
            Assert.Equal(1, expression.Get(12));
            Assert.Equal(1, expression.Get(13));
            Assert.Equal(1, expression.Get(14));
            Assert.Equal(1, expression.Get(15));
        }
Exemplo n.º 2
0
        public void Can_parse_czech()
        {
            var format = "nplurals=3; plural=(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2;";

            var expression = PluralFormsExpression.Parse(format);

            Assert.Equal(3, expression.Count);

            Assert.Equal(0, expression.Get(1));

            Assert.Equal(1, expression.Get(2));
            Assert.Equal(1, expression.Get(3));
            Assert.Equal(1, expression.Get(4));

            Assert.Equal(2, expression.Get(0));
            Assert.Equal(2, expression.Get(5));
            Assert.Equal(2, expression.Get(6));
            Assert.Equal(2, expression.Get(7));
            Assert.Equal(2, expression.Get(8));
            Assert.Equal(2, expression.Get(9));
            Assert.Equal(2, expression.Get(10));
            Assert.Equal(2, expression.Get(11));
            Assert.Equal(2, expression.Get(12));
            Assert.Equal(2, expression.Get(13));
            Assert.Equal(2, expression.Get(14));
            Assert.Equal(2, expression.Get(15));
        }
Exemplo n.º 3
0
        public void Can_parse_slovenian()
        {
            var format = @"nplurals=4; plural=n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3;";

            var expression = PluralFormsExpression.Parse(format);

            Assert.Equal(4, expression.Count);

            Assert.Equal(0, expression.Get(1));
            Assert.Equal(0, expression.Get(101));
            Assert.Equal(0, expression.Get(201));
            Assert.Equal(0, expression.Get(301));
            Assert.Equal(0, expression.Get(1001));

            Assert.Equal(1, expression.Get(2));
            Assert.Equal(1, expression.Get(102));
            Assert.Equal(1, expression.Get(202));
            Assert.Equal(1, expression.Get(302));
            Assert.Equal(1, expression.Get(1002));

            Assert.Equal(2, expression.Get(3));
            Assert.Equal(2, expression.Get(103));
            Assert.Equal(2, expression.Get(203));
            Assert.Equal(2, expression.Get(303));
            Assert.Equal(2, expression.Get(1003));
            Assert.Equal(2, expression.Get(4));
            Assert.Equal(2, expression.Get(104));
            Assert.Equal(2, expression.Get(204));
            Assert.Equal(2, expression.Get(304));
            Assert.Equal(2, expression.Get(1004));

            Assert.Equal(3, expression.Get(0));
            Assert.Equal(3, expression.Get(5));
            Assert.Equal(3, expression.Get(6));
            Assert.Equal(3, expression.Get(7));
            Assert.Equal(3, expression.Get(8));
            Assert.Equal(3, expression.Get(9));
            Assert.Equal(3, expression.Get(10));
            Assert.Equal(3, expression.Get(11));
            Assert.Equal(3, expression.Get(12));
            Assert.Equal(3, expression.Get(13));
            Assert.Equal(3, expression.Get(14));
            Assert.Equal(3, expression.Get(15));
            Assert.Equal(3, expression.Get(16));
            Assert.Equal(3, expression.Get(105));
            Assert.Equal(3, expression.Get(205));
            Assert.Equal(3, expression.Get(305));
            Assert.Equal(3, expression.Get(1005));
        }
Exemplo n.º 4
0
        public static Catalog Load(IGettextReader reader, CultureInfo culture = null)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            var catalog = new Catalog();

            Translation translation;

            while ((translation = reader.ReadTranslation()) != null)
            {
                if (translation.Key.IsHeader)
                {
                    var headers = translation.Single().Split(HEADER_SEPARATOR);

                    foreach (var header in headers)
                    {
                        if (header.Trim().Length <= 0)
                        {
                            continue;
                        }

                        var split = header.Split(HEADER_KEY_VALUE_SEPARATORS, 2);

                        if (split.Length != 2)
                        {
                            throw new InvalidOperationException("Failed to parse header '" + header + "'");
                        }

                        var key   = split[0].Trim();
                        var value = split[1].Trim();

                        catalog._headers.Add(key, value);

                        // accept encoding
                        if (string.Equals(key, "Content-Type", StringComparison.OrdinalIgnoreCase))
                        {
                            var contentType = new ContentType(value);

                            if (contentType.CharSet != null)
                            {
                                reader.Encoding = Encoding.GetEncoding(contentType.CharSet);
                            }
                        }
                        // accept culture
                        else if (string.Equals(key, "Language", StringComparison.OrdinalIgnoreCase))
                        {
                            var cultureInfo = new CultureInfo(value);

                            catalog.Culture = cultureInfo;
                        }
                        // accept plural forms
                        else if (string.Equals(key, "Plural-Forms", StringComparison.OrdinalIgnoreCase))
                        {
                            var pluralForms = PluralFormsExpression.Parse(value);

                            catalog.PluralForms = pluralForms;
                        }
                    }
                }
                else
                {
                    catalog._translations.Add(translation.Key, translation);
                }
            }

            // override culture
            if (culture != null)
            {
                catalog.Culture = culture;
            }

            // verify integrity
            if (catalog.Culture == null)
            {
                throw new InvalidOperationException("Catalog doesn't have 'Language' header");
            }
            if (catalog.PluralForms == null)
            {
                catalog.PluralForms = PluralFormsExpression.Default;
            }

            return(catalog);
        }