예제 #1
0
        public static IDictionary <string, string> CreateDictionary(ITranslationTemplate source)
        {
            var dictionary = new Dictionary <string, string>();

            var templateProperties = typeof(ITranslationTemplate).GetProperties();

            foreach (var templateProperty in templateProperties)
            {
                if (templateProperty.PropertyType == typeof(string))
                {
                    dictionary.Add(templateProperty.Name, (string)templateProperty.GetValue(source));
                }
                else
                {
                    var group = templateProperty.GetValue(source);

                    var groupProperties = templateProperty.PropertyType.GetProperties();

                    foreach (var groupProperty in groupProperties)
                    {
                        var value = (string)groupProperty.GetValue(group);

                        dictionary.Add($"{templateProperty.Name}.{groupProperty.Name}", value);
                    }
                }
            }

            return(dictionary);
        }
예제 #2
0
        public static void AssertAddTranslations(IValidationContext validationContext, string translationName, ITranslationTemplate translation)
        {
            AssertTranslationAdded(validationContext, translationName);

            Assert.Null(validationContext.ValidationOptions.TranslationName);

            AssertPhrasesAdded(validationContext.Translations[translationName], translation);
        }
예제 #3
0
        // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local
        private static void AssertPhrasesAdded(IReadOnlyDictionary <string, string> dictionary, ITranslationTemplate translation)
        {
            Assert.Equal(Phrases.English.Keys.Count, dictionary.Keys.Count());

            foreach (var key in Phrases.English.Keys)
            {
                Assert.Contains(key, dictionary.Keys);
            }

            var translationProperties = typeof(ITranslationTemplate).GetProperties();

            foreach (var translationProperty in translationProperties)
            {
                if (translationProperty.PropertyType == typeof(string))
                {
                    Assert.Contains(translationProperty.Name, dictionary.Keys);

                    Assert.Equal((string)translationProperty.GetValue(translation), dictionary[translationProperty.Name]);
                }
                else
                {
                    var group = translationProperty.GetValue(translation);

                    var groupProperties = translationProperty.PropertyType.GetProperties();

                    foreach (var groupProperty in groupProperties)
                    {
                        var key = $"{translationProperty.Name}.{groupProperty.Name}";

                        var value = (string)groupProperty.GetValue(group);

                        Assert.Contains(key, dictionary.Keys);

                        Assert.Equal(value, dictionary[key]);
                    }
                }
            }
        }