public async Task <IActionResult> Localize(string contentItemId, string targetCulture) { // Invariant culture name is empty so a null value is bound. targetCulture = targetCulture ?? ""; var contentItem = await _contentManager.GetAsync(contentItemId, VersionOptions.Latest); if (contentItem == null) { return(NotFound()); } if (!await _authorizationService.AuthorizeAsync(User, Permissions.LocalizeContent, contentItem)) { return(Forbid()); } var checkContentItem = await _contentManager.NewAsync(contentItem.ContentType); // Set the current user as the owner to check for ownership permissions on creation checkContentItem.Owner = User.FindFirstValue(ClaimTypes.NameIdentifier); if (!await _authorizationService.AuthorizeAsync(User, CommonPermissions.EditContent, checkContentItem)) { return(Forbid()); } var part = contentItem.As <LocalizationPart>(); if (part == null) { return(NotFound()); } var alreadyLocalizedContent = await _contentLocalizationManager.GetContentItemAsync(part.LocalizationSet, targetCulture); if (alreadyLocalizedContent != null) { await _notifier.WarningAsync(H["A localization already exists for '{0}'.", targetCulture]); return(RedirectToAction("Edit", "Admin", new { area = "OrchardCore.Contents", contentItemId = contentItemId })); } try { var newContent = await _contentLocalizationManager.LocalizeAsync(contentItem, targetCulture); await _notifier.InformationAsync(H["Localized version of the content created successfully."]); return(RedirectToAction("Edit", "Admin", new { area = "OrchardCore.Contents", contentItemId = newContent.ContentItemId })); } catch (InvalidOperationException) { await _notifier.WarningAsync(H["Could not create localized version of the content item."]); return(RedirectToAction("Edit", "Admin", new { area = "OrchardCore.Contents", contentItemId = contentItem.ContentItemId })); } }
public async Task <IActionResult> Localize(string contentItemId, string targetCulture) { var contentItem = await _contentManager.GetAsync(contentItemId, VersionOptions.Latest); if (contentItem == null) { return(NotFound()); } if (!await _authorizationService.AuthorizeAsync(User, Permissions.EditLocalizedContent, contentItem)) { return(Unauthorized()); } var checkContentItem = await _contentManager.NewAsync(contentItem.ContentType); // Set the current user as the owner to check for ownership permissions on creation checkContentItem.Owner = User.Identity.Name; if (!await _authorizationService.AuthorizeAsync(User, CommonPermissions.EditContent, checkContentItem)) { return(Unauthorized()); } var part = contentItem.As <LocalizationPart>(); if (part == null) { return(NotFound()); } var alreadyLocalizedContent = await _contentLocalizationManager.GetContentItem(part.LocalizationSet, targetCulture); if (alreadyLocalizedContent != null) { _notifier.Warning(T["A localization already exist for '{0}'", targetCulture]); return(RedirectToAction("Edit", "Admin", new { area = "OrchardCore.Contents", contentItemId = contentItemId })); } try { var newContent = await _contentLocalizationManager.LocalizeAsync(contentItem, targetCulture); _notifier.Information(T["Successfully created localized version of the content."]); return(RedirectToAction("Edit", "Admin", new { area = "OrchardCore.Contents", contentItemId = newContent.ContentItemId })); } catch (InvalidOperationException) { _notifier.Warning(T["Could not create localized version the content item"]); return(RedirectToAction("Edit", "Admin", new { area = "OrchardCore.Contents", contentItemId = contentItem.ContentItemId })); } }