Exemple #1
0
        public object ToLanguageModel(LanguagesConfig languagesConfig, IReadOnlyCollection <Language> languagePreferences = null)
        {
            Guard.NotNull(languagesConfig, nameof(languagesConfig));

            if (languagePreferences == null || languagePreferences.Count == 0)
            {
                return(this);
            }

            if (languagePreferences.Count == 1 && languagesConfig.TryGetConfig(languagePreferences.First(), out var languageConfig))
            {
                languagePreferences = languagePreferences.Union(languageConfig.Fallback).ToList();
            }

            var result = new Dictionary <string, JToken>();

            foreach (var fieldValue in this)
            {
                var fieldValues = fieldValue.Value;

                foreach (var language in languagePreferences)
                {
                    if (fieldValues.TryGetValue(language, out var value) && value != null)
                    {
                        result[fieldValue.Key] = value;

                        break;
                    }
                }
            }

            return(result);
        }
        public static object ToFlatLanguageModel(this NamedContentData content, LanguagesConfig languagesConfig, IReadOnlyCollection <Language> languagePreferences = null)
        {
            Guard.NotNull(languagesConfig, nameof(languagesConfig));

            if (languagePreferences == null || languagePreferences.Count == 0)
            {
                return(content);
            }

            if (languagePreferences.Count == 1 && languagesConfig.TryGetConfig(languagePreferences.First(), out var languageConfig))
            {
                languagePreferences = languagePreferences.Union(languageConfig.LanguageFallbacks).ToList();
            }

            var result = new Dictionary <string, IJsonValue>();

            foreach (var fieldValue in content)
            {
                var fieldData = fieldValue.Value;

                foreach (var language in languagePreferences)
                {
                    if (fieldData.TryGetValue(language, out var value) && value.Type != JsonValueType.Null)
                    {
                        result[fieldValue.Key] = value;

                        break;
                    }
                }
            }

            return(result);
        }
Exemple #3
0
        private static LanguageConfig GetConfigOrThrow(LanguagesConfig languages, Language language)
        {
            if (language == null)
            {
                return(null);
            }

            if (!languages.TryGetConfig(language, out var languageConfig))
            {
                throw new DomainObjectNotFoundException(language, "Languages", typeof(IAppEntity));
            }

            return(languageConfig);
        }