Exemple #1
0
        public void BuildSimplePluralizedStringTest()
        {
            var defaultLocalizedString = new LocalizedString("let", "let");
            var psA = new PluralizedString(defaultLocalizedString);

            Assert.AreEqual(defaultLocalizedString, psA.GetPluralizedLocalizedString(0));
        }
Exemple #2
0
        public void OverlapExceptionTest()
        {
            var exceptionThrown = false;

            var defaultLocalizedString = new LocalizedString("let", "let");
            var psA = new PluralizedString(defaultLocalizedString);

            psA.Add(new PluralizationInterval(int.MinValue, -5), new LocalizedString("let", "let"));
            psA.Add(new PluralizationInterval(-4, -2), new LocalizedString("let", "roky"));
            psA.Add(new PluralizationInterval(-1, -1), new LocalizedString("let", "rok"));
            psA.Add(new PluralizationInterval(0, 0), new LocalizedString("let", "let"));
            psA.Add(new PluralizationInterval(1, 1), new LocalizedString("let", "rok"));
            psA.Add(new PluralizationInterval(2, 4), new LocalizedString("let", "roky"));
            psA.Add(new PluralizationInterval(5, int.MaxValue), new LocalizedString("let", "let"));

            try
            {
                psA.Add(new PluralizationInterval(1, 2), new LocalizedString("let", "rok"));
            }
            catch (PluralizedStringIntervalOverlapException)
            {
                exceptionThrown = true;
            }

            Assert.IsTrue(exceptionThrown);
        }
        public IDictionary <string, PluralizedString> GetPluralizedDictionary(CultureInfo cultureInfo, string scope)
        {
            using (var dbContext = m_dbContextFunc.Invoke())
            {
                var culture         = GetCultureByNameOrGetDefault(dbContext, cultureInfo.Name);
                var dictionaryScope = GetDictionaryScope(dbContext, scope);

                var pluralizedStaticTextDao = new PluralizedStaticTextDao(dbContext.PluralizedStaticText);
                var result           = pluralizedStaticTextDao.FindAllByCultureAndScope(culture, dictionaryScope);
                var resultDictionary = new Dictionary <string, PluralizedString>();
                foreach (var singleplPluralizedStaticText in result)
                {
                    var pluralizedString = new PluralizedString(new LocalizedString(singleplPluralizedStaticText.Name,
                                                                                    singleplPluralizedStaticText.Text, false));
                    foreach (var intervalText in singleplPluralizedStaticText.IntervalTexts)
                    {
                        pluralizedString.Add(new PluralizationInterval(intervalText.IntervalStart, intervalText.IntervalEnd)
                                             , new LocalizedString(singleplPluralizedStaticText.Name, intervalText.Text));
                    }

                    resultDictionary.Add(singleplPluralizedStaticText.Name, pluralizedString);
                }

                return(resultDictionary);
            }
        }
        public void IsOvelapingOverlapsTest()
        {
            var psA = new PluralizedString(new LocalizedString("a", "a"));
            var pA  = new PluralizationInterval(-20, 20);
            var pB  = new PluralizationInterval(-10, 10);

            psA.Add(pA, new LocalizedString("b", "b"));

            Assert.ThrowsException <PluralizedStringIntervalOverlapException>(() => psA.Add(pB, new LocalizedString("c", "c")));
        }
Exemple #5
0
        public void ConstructorDefaultStringNullTest()
        {
            var exceptionThrown = false;

            try
            {
                var psA = new PluralizedString(null);
            }
            catch (ArgumentNullException)
            {
                exceptionThrown = true;
            }

            Assert.IsTrue(exceptionThrown);
        }
Exemple #6
0
        public void IntervalCompletePluralizedStringTest()
        {
            var defaultLocalizedString = new LocalizedString("let", "let");
            var psA = new PluralizedString(defaultLocalizedString);

            psA.Add(new PluralizationInterval(int.MinValue, -5), new LocalizedString("let", "let"));
            psA.Add(new PluralizationInterval(-4, -2), new LocalizedString("let", "roky"));
            psA.Add(new PluralizationInterval(-1, -1), new LocalizedString("let", "rok"));
            psA.Add(new PluralizationInterval(0, 0), new LocalizedString("let", "let"));
            psA.Add(new PluralizationInterval(1, 1), new LocalizedString("let", "rok"));
            psA.Add(new PluralizationInterval(2, 4), new LocalizedString("let", "roky"));
            psA.Add(new PluralizationInterval(5, int.MaxValue), new LocalizedString("let", "let"));


            Assert.AreEqual("let", psA.GetPluralizedLocalizedString(-10));
            Assert.AreEqual("let", psA.GetPluralizedLocalizedString(-9));
            Assert.AreEqual("let", psA.GetPluralizedLocalizedString(-8));
            Assert.AreEqual("let", psA.GetPluralizedLocalizedString(-7));
            Assert.AreEqual("let", psA.GetPluralizedLocalizedString(-6));
            Assert.AreEqual("let", psA.GetPluralizedLocalizedString(-5));
            Assert.AreEqual("roky", psA.GetPluralizedLocalizedString(-4));
            Assert.AreEqual("roky", psA.GetPluralizedLocalizedString(-3));
            Assert.AreEqual("roky", psA.GetPluralizedLocalizedString(-2));
            Assert.AreEqual("rok", psA.GetPluralizedLocalizedString(-1));
            Assert.AreEqual("let", psA.GetPluralizedLocalizedString(0));
            Assert.AreEqual("rok", psA.GetPluralizedLocalizedString(1));
            Assert.AreEqual("roky", psA.GetPluralizedLocalizedString(2));
            Assert.AreEqual("roky", psA.GetPluralizedLocalizedString(3));
            Assert.AreEqual("roky", psA.GetPluralizedLocalizedString(4));
            Assert.AreEqual("let", psA.GetPluralizedLocalizedString(5));
            Assert.AreEqual("let", psA.GetPluralizedLocalizedString(6));
            Assert.AreEqual("let", psA.GetPluralizedLocalizedString(7));
            Assert.AreEqual("let", psA.GetPluralizedLocalizedString(8));
            Assert.AreEqual("let", psA.GetPluralizedLocalizedString(9));
            Assert.AreEqual("let", psA.GetPluralizedLocalizedString(10));
        }
        private void InitPluralizedDictionary()
        {
            if (m_pluralizedDictionary != null)
            {
                return;
            }

            var pluralizedDictionary = new ConcurrentDictionary <string, PluralizedString>();

            if (!IsPluralizationLoaded())
            {
                if (m_logger != null && m_logger.IsWarningEnabled())
                {
                    m_logger.LogWarning(NotLoadedPluralizedMsg);
                }

                m_pluralizedDictionary = pluralizedDictionary;

                return;
            }

            var keyValueObjects = (JObject)m_jsonPluralizedDictionary.SelectToken(DictionaryJPath);

            if (keyValueObjects == null)
            {
                m_pluralizedDictionary = pluralizedDictionary;
                return;
            }

            var keyValueEnumerator = keyValueObjects.GetEnumerator();

            while (keyValueEnumerator.MoveNext())
            {
                var keyValuePair = keyValueEnumerator.Current;
                var key          = keyValuePair.Key;
                var value        = keyValuePair.Value.First;

                var defaultValue           = ((JProperty)keyValuePair.Value.First).Name;
                var defaultLocalizedString = new LocalizedString(key, defaultValue);
                var pluralizedString       = new PluralizedString(defaultLocalizedString);
                var pluralizationTriples   = value.First;

                var childrenTriplesEnumerator = pluralizationTriples.Children().GetEnumerator();

                while (childrenTriplesEnumerator.MoveNext())
                {
                    var childrenTripleJToken = childrenTriplesEnumerator.Current;
                    var leftInterval         = childrenTripleJToken[0];
                    var rightInterval        = childrenTripleJToken[1];
                    var stringValue          = childrenTripleJToken[2];

                    int leftIntervalInteger;
                    if (leftInterval?.Value <string>() == null)
                    {
                        leftIntervalInteger = int.MinValue;
                    }
                    else
                    {
                        var xParsed = int.TryParse(leftInterval.ToString(), out leftIntervalInteger);
                        if (!xParsed)
                        {
                            var errorMessage = string.Format(
                                @"The x value ""{0}"" in pluralization dictionary: ""{1}"" culture: ""{2}""",
                                leftInterval, m_scope, m_cultureInfo.Name);
                            if (m_logger != null && m_logger.IsErrorEnabled())
                            {
                                m_logger.LogError(errorMessage);
                            }

                            throw new DictionaryFormatException(errorMessage);
                        }
                    }

                    int rightIntervalInteger;
                    if (rightInterval?.Value <string>() == null)
                    {
                        rightIntervalInteger = int.MaxValue;
                    }
                    else
                    {
                        var yParsed = int.TryParse(rightInterval.ToString(), out rightIntervalInteger);
                        if (!yParsed)
                        {
                            var errorMessage = string.Format(
                                @"The y value ""{0}"" in pluralization dictionary: ""{1}"" culture: ""{2}""",
                                rightInterval.ToString(), m_scope, m_cultureInfo.Name);
                            if (m_logger != null && m_logger.IsErrorEnabled())
                            {
                                m_logger.LogError(errorMessage);
                            }

                            throw new DictionaryFormatException(errorMessage);
                        }
                    }

                    var stringValueString = (string)stringValue;
                    pluralizedString.Add(new PluralizationInterval(leftIntervalInteger, rightIntervalInteger),
                                         new LocalizedString(key, stringValueString));
                }

                childrenTriplesEnumerator.Dispose();
                pluralizedDictionary.TryAdd(key, pluralizedString);
            }

            keyValueEnumerator.Dispose();

            m_pluralizedDictionary = pluralizedDictionary;
        }