Beispiel #1
0
        public void Should_make_contains_test()
        {
            Assert.True(config_0.Contains(Language.EN));

            Assert.False(config_0.Contains(Language.FR));
            Assert.False(config_0.Contains(null !));
        }
Beispiel #2
0
        public static void CanUpdate(LanguagesConfig languages, UpdateLanguage command)
        {
            Guard.NotNull(command, nameof(command));

            var config = GetConfigOrThrow(languages, command.Language);

            Validate.It(() => "Cannot update language.", e =>
            {
                if (command.Language == null)
                {
                    e("Language is required.", nameof(command.Language));
                }

                if ((languages.Master == config || command.IsMaster) && command.IsOptional)
                {
                    e("Master language cannot be made optional.", nameof(command.IsMaster));
                }

                if (command.Fallback == null)
                {
                    return;
                }

                foreach (var fallback in command.Fallback)
                {
                    if (!languages.Contains(fallback))
                    {
                        e($"App does not have fallback language '{fallback}'.", nameof(command.Fallback));
                    }
                }
            });
        }
Beispiel #3
0
        public static void CanUpdate(LanguagesConfig languages, UpdateLanguage command)
        {
            Guard.NotNull(command, nameof(command));

            var languageConfig = GetLanguageConfigOrThrow(languages, command.Language);

            Validate.It(() => "Cannot update language.", error =>
            {
                if (command.Language == null)
                {
                    error(new ValidationError("Language cannot be null.", nameof(command.Language)));
                }

                if ((languages.Master == languageConfig || command.IsMaster) && command.IsOptional)
                {
                    error(new ValidationError("Cannot make master language optional.", nameof(command.IsMaster)));
                }

                if (command.Fallback != null)
                {
                    foreach (var fallback in command.Fallback)
                    {
                        if (!languages.Contains(fallback))
                        {
                            error(new ValidationError($"Config does not contain fallback language {fallback}.", nameof(command.Fallback)));
                        }
                    }
                }
            });
        }
Beispiel #4
0
 private static void EnsureConfigExists(LanguagesConfig languages, Language language)
 {
     if (!languages.Contains(language))
     {
         throw new DomainObjectNotFoundException(language);
     }
 }
Beispiel #5
0
        public static FieldConverter FilterLanguages(LanguagesConfig languagesConfig, IEnumerable <Language> languages)
        {
            if (languages == null)
            {
                return((data, field) => data);
            }

            var languageCodes = languages.Select(x => x.Iso2Code).Where(x => languagesConfig.Contains(x));
            var languageSet   = new HashSet <string>(languageCodes, StringComparer.OrdinalIgnoreCase);

            if (languageSet.Count == 0)
            {
                return((data, field) => data);
            }

            return((data, field) =>
            {
                if (field.Partitioning.Equals(Partitioning.Language))
                {
                    var result = new ContentFieldData();

                    foreach (var languageCode in languageSet)
                    {
                        if (data.TryGetValue(languageCode, out var value))
                        {
                            result[languageCode] = value;
                        }
                    }

                    return result;
                }

                return data;
            });
        }
Beispiel #6
0
        public static void CanAdd(LanguagesConfig languages, AddLanguage command)
        {
            Guard.NotNull(command, nameof(command));

            Validate.It(() => "Cannot add language.", e =>
            {
                if (command.Language == null)
                {
                    e("Language code is required.", nameof(command.Language));
                }
                else if (languages.Contains(command.Language))
                {
                    e("Language has already been added.");
                }
            });
        }
Beispiel #7
0
        public static void CanAdd(LanguagesConfig languages, AddLanguage command)
        {
            Guard.NotNull(command, nameof(command));

            Validate.It(() => "Cannot add language.", error =>
            {
                if (command.Language == null)
                {
                    error(new ValidationError("Language cannot be null.", nameof(command.Language)));
                }
                else if (languages.Contains(command.Language))
                {
                    error(new ValidationError("Language already added.", nameof(command.Language)));
                }
            });
        }
Beispiel #8
0
        public static void CanAdd(LanguagesConfig languages, AddLanguage command)
        {
            Guard.NotNull(command);

            Validate.It(() => "Cannot add language.", e =>
            {
                var language = command.Language;

                if (language == null)
                {
                    e(Not.Defined("Language code"), nameof(command.Language));
                }
                else if (languages.Contains(language))
                {
                    e("Language has already been added.");
                }
            });
        }
        public static void CanAdd(LanguagesConfig languages, AddLanguage command)
        {
            Guard.NotNull(command, nameof(command));

            Validate.It(e =>
            {
                var language = command.Language;

                if (language == null)
                {
                    e(Not.Defined(nameof(command.Language)), nameof(command.Language));
                }
                else if (languages.Contains(language))
                {
                    e(T.Get("apps.languages.languageAlreadyAdded"));
                }
            });
        }
Beispiel #10
0
        public static void CanUpdate(LanguagesConfig languages, UpdateLanguage command)
        {
            Guard.NotNull(command);

            Validate.It(() => "Cannot update language.", e =>
            {
                var language = command.Language;

                if (language == null)
                {
                    e(Not.Defined("Language code"), nameof(command.Language));
                }
                else
                {
                    EnsureConfigExists(languages, language);

                    if (languages.IsMaster(language) || command.IsMaster)
                    {
                        if (command.IsOptional)
                        {
                            e("Master language cannot be made optional.", nameof(command.IsMaster));
                        }

                        if (command.Fallback?.Count > 0)
                        {
                            e("Master language cannot have fallback languages.", nameof(command.Fallback));
                        }
                    }

                    if (command.Fallback == null)
                    {
                        return;
                    }

                    foreach (var fallback in command.Fallback)
                    {
                        if (!languages.Contains(fallback))
                        {
                            e($"App does not have fallback language '{fallback}'.", nameof(command.Fallback));
                        }
                    }
                }
            });
        }
Beispiel #11
0
        public static void CanUpdate(LanguagesConfig languages, UpdateLanguage command)
        {
            Guard.NotNull(command, nameof(command));

            Validate.It(e =>
            {
                var language = command.Language;

                if (language == null)
                {
                    e(Not.Defined(nameof(command.Language)), nameof(command.Language));
                }
                else
                {
                    EnsureConfigExists(languages, language);

                    if (languages.IsMaster(language) || command.IsMaster)
                    {
                        if (command.IsOptional)
                        {
                            e(T.Get("apps.languages.masterLanguageNotOptional"), nameof(command.IsMaster));
                        }

                        if (command.Fallback?.Length > 0)
                        {
                            e(T.Get("apps.languages.masterLanguageNoFallbacks"), nameof(command.Fallback));
                        }
                    }

                    if (command.Fallback == null)
                    {
                        return;
                    }

                    foreach (var fallback in command.Fallback)
                    {
                        if (!languages.Contains(fallback))
                        {
                            e(T.Get("apps.languages.fallbackNotFound", new { fallback }), nameof(command.Fallback));
                        }
                    }
                }
            });
        }
Beispiel #12
0
        public static FieldConverter FilterLanguages(LanguagesConfig config, IEnumerable <Language>?languages)
        {
            if (languages?.Any() != true)
            {
                return((data, field) => data);
            }

            var languageSet = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            foreach (var language in languages)
            {
                if (config.Contains(language.Iso2Code))
                {
                    languageSet.Add(language.Iso2Code);
                }
            }

            if (languageSet.Count == 0)
            {
                languageSet.Add(config.Master);
            }

            return((data, field) =>
            {
                if (field.Partitioning.Equals(Partitioning.Language))
                {
                    var result = new ContentFieldData();

                    foreach (var languageCode in languageSet)
                    {
                        if (data.TryGetValue(languageCode, out var value))
                        {
                            result[languageCode] = value;
                        }
                    }

                    return result;
                }

                return data;
            });
        }
Beispiel #13
0
        public static FieldConverter FilterLanguages(LanguagesConfig config, IEnumerable <Language>?languages)
        {
            if (languages?.Any() != true)
            {
                return(Noop);
            }

            var languageSet = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            foreach (var language in languages)
            {
                if (config.Contains(language.Iso2Code))
                {
                    languageSet.Add(language.Iso2Code);
                }
            }

            if (languageSet.Count == 0)
            {
                languageSet.Add(config.Master);
            }

            return((data, field) =>
            {
                if (field.Partitioning.Equals(Partitioning.Language))
                {
                    foreach (var(key, _) in data.ToList())
                    {
                        if (!languageSet.Contains(key))
                        {
                            data.Remove(key);
                        }
                    }
                }

                return data;
            });
        }