public async Task <ContentItem> LocalizeAsync(ContentItem content, string targetCulture)
        {
            var localizationPart = content.As <LocalizationPart>();
            var siteSettings     = await _siteService.GetSiteSettingsAsync();

            // not sure if this is redundant or not. The check is also done in the Admin controller
            if (!siteSettings.GetConfiguredCultures().Any(c => String.Equals(c, targetCulture, StringComparison.OrdinalIgnoreCase)))
            {
                throw new InvalidOperationException("Cannot localize an unsupported culture");
            }

            if (String.IsNullOrEmpty(localizationPart.LocalizationSet))
            {
                // If the source content item is not yet localized, define its defaults

                localizationPart.LocalizationSet = _iidGenerator.GenerateUniqueId();
                localizationPart.Culture         = await GetDefaultCultureNameAsync();

                _session.Save(content);
            }
            else
            {
                var existingContent = await GetContentItem(localizationPart.LocalizationSet, targetCulture);

                if (existingContent != null)
                {
                    // already localized
                    return(existingContent);
                }
            }

            // Cloning the content item
            var cloned = await _contentManager.CloneAsync(content);

            var clonedPart = cloned.As <LocalizationPart>();

            clonedPart.Culture         = targetCulture;
            clonedPart.LocalizationSet = localizationPart.LocalizationSet;
            clonedPart.Apply();

            var context = new LocalizationContentContext(content, localizationPart.LocalizationSet, targetCulture);

            await Handlers.InvokeAsync(async handler => await handler.LocalizingAsync(context), _logger);

            await ReversedHandlers.InvokeAsync(async handler => await handler.LocalizedAsync(context), _logger);

            _session.Save(cloned);
            return(cloned);
        }
Esempio n. 2
0
        public async Task <ContentItem> LocalizeAsync(ContentItem content, string targetCulture)
        {
            var supportedCultures = await _localizationService.GetSupportedCulturesAsync();

            if (!supportedCultures.Any(c => String.Equals(c, targetCulture, StringComparison.OrdinalIgnoreCase)))
            {
                throw new InvalidOperationException("Cannot localize an unsupported culture");
            }

            var localizationPart = content.As <LocalizationPart>();

            if (String.IsNullOrEmpty(localizationPart.LocalizationSet))
            {
                // If the source content item is not yet localized, define its defaults
                localizationPart.LocalizationSet = _iidGenerator.GenerateUniqueId();
                localizationPart.Culture         = await _localizationService.GetDefaultCultureAsync();

                _session.Save(content);
            }
            else
            {
                var existingContent = await GetContentItemAsync(localizationPart.LocalizationSet, targetCulture);

                if (existingContent != null)
                {
                    // already localized
                    return(existingContent);
                }
            }

            // Cloning the content item
            var cloned = await _contentManager.CloneAsync(content);

            var clonedPart = cloned.As <LocalizationPart>();

            clonedPart.Culture         = targetCulture;
            clonedPart.LocalizationSet = localizationPart.LocalizationSet;
            clonedPart.Apply();

            var context = new LocalizationContentContext(cloned, content, localizationPart.LocalizationSet, targetCulture);

            await Handlers.InvokeAsync((handler, context) => handler.LocalizingAsync(context), context, _logger);

            await ReversedHandlers.InvokeAsync((handler, context) => handler.LocalizedAsync(context), context, _logger);

            _session.Save(cloned);
            return(cloned);
        }
        /// <summary>
        /// Assign the Localized version of the List when localizing the Contained Item
        /// </summary>
        public override async Task LocalizingAsync(LocalizationContentContext context, LocalizationPart part)
        {
            var containedPart = context.ContentItem.As <ContainedPart>();

            // todo: remove this check and change the handler to target ContainedPart when issue 3890 is fixed
            if (containedPart != null)
            {
                var list = await _session.QueryIndex <LocalizedContentItemIndex>(i => (i.Published || i.Latest) && i.ContentItemId == containedPart.ListContentItemId).FirstOrDefaultAsync();

                var localizedList = await _session.QueryIndex <LocalizedContentItemIndex>(i => (i.Published || i.Latest) && i.LocalizationSet == list.LocalizationSet && i.Culture == context.Culture).FirstOrDefaultAsync();

                if (localizedList != null)
                {
                    containedPart.ListContentItemId = localizedList.ContentItemId;
                    containedPart.Apply();
                }
            }
        }
        /// <summary>
        /// Select Contained ContentItems that are already in the target culture
        /// but attached to the original list and reassign their ListContenItemId.
        /// </summary>
        public override async Task LocalizedAsync(LocalizationContentContext context, ListPart part)
        {
            var containedList = await _session.Query <ContentItem, ContainedPartIndex>(
                x => x.ListContentItemId == context.Original.ContentItemId).ListAsync();

            if (!containedList.Any())
            {
                return;
            }

            foreach (var item in containedList)
            {
                var localizationPart = item.As <LocalizationPart>();
                if (localizationPart.Culture == context.Culture)
                {
                    var cp = item.As <ContainedPart>();
                    cp.ListContentItemId = context.ContentItem.ContentItemId;
                    cp.Apply();
                    _session.Save(item);
                }
            }
        }