コード例 #1
0
        public static FieldConverter ResolveLanguages(LanguagesConfig languages)
        {
            var codeForInvariant = InvariantPartitioning.Key;

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

                    foreach (var languageCode in languages.AllKeys)
                    {
                        if (data.TryGetValue(languageCode, out var value))
                        {
                            result[languageCode] = value;
                        }
                        else if (languages.IsMaster(languageCode) && data.TryGetValue(codeForInvariant, out value))
                        {
                            result[languageCode] = value;
                        }
                    }

                    return result;
                }

                return data;
            });
        }
コード例 #2
0
        public static AppLanguageDto FromLanguage(Language language, LanguageConfig config, LanguagesConfig languages)
        {
            var result = new AppLanguageDto
            {
                EnglishName = language.EnglishName,
                IsMaster    = languages.IsMaster(language),
                IsOptional  = languages.IsOptional(language),
                Iso2Code    = language.Iso2Code,
                Fallback    = config.Fallbacks.ToArray()
            };

            return(result);
        }
コード例 #3
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));
                        }
                    }
                }
            });
        }
コード例 #4
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));
                        }
                    }
                }
            });
        }
コード例 #5
0
        public static void CanRemove(LanguagesConfig languages, RemoveLanguage command)
        {
            Guard.NotNull(command);

            Validate.It(() => "Cannot remove 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))
                    {
                        e("Master language cannot be removed.");
                    }
                }
            });
        }
コード例 #6
0
        public static void CanRemove(LanguagesConfig languages, RemoveLanguage 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))
                    {
                        e(T.Get("apps.languages.masterLanguageNotRemovable"));
                    }
                }
            });
        }