Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor fixes for localizations #8726

Merged
merged 10 commits into from
Apr 2, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ private void MigrateBlogPost(ContentItem blogPost) {
var blogids = new HashSet<int> { blog.As<BlogPart>().ContentItem.Id };

//seek for same culture blog
var realBlog = _localizationService.GetLocalizations(blog).SingleOrDefault(w => w.As<LocalizationPart>().Culture == blogPostCulture);
var realBlog = _localizationService.GetLocalizations(blog)
.SingleOrDefault(w => w.Culture?.Culture == blogPostCulture.Culture);
if (realBlog.Has<LocalizationPart>() && realBlog.As<LocalizationPart>().Culture.Id == blogPostCulture.Id) {
blogPost.As<ICommonPart>().Container = realBlog;
if (blogPost.Has<AutoroutePart>()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Web.Mvc;
using System.Web.Routing;
using Orchard.ContentManagement.MetaData;
using Orchard.Environment.Extensions;
using Orchard.Localization.Services;
using Orchard.Taxonomies.Services;
using Orchard.UI.Admin;

namespace Orchard.Taxonomies.Controllers {
[OrchardFeature("Orchard.Taxonomies.LocalizationExtensions")]
public class AdminLocalizedTaxonomyController : LocalizedTaxonomyController {
private readonly RequestContext _requestContext;

public AdminLocalizedTaxonomyController(IContentDefinitionManager contentDefinitionManager,
ILocalizationService localizationService,
ITaxonomyService taxonomyService,
ITaxonomyExtensionsService
taxonomyExtensionsService,
RequestContext requestContext) : base(contentDefinitionManager,
localizationService,
taxonomyService,
taxonomyExtensionsService) {

_requestContext = requestContext;
}

[OutputCache(NoStore = true, Duration = 0)]
public new ActionResult GetTaxonomy(string contentTypeName, string taxonomyFieldName, int contentId, string culture, string selectedValues) {
AdminFilter.Apply(_requestContext);

return GetTaxonomyInternal(contentTypeName, taxonomyFieldName, contentId, culture, selectedValues);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using Orchard.ContentManagement;
using Orchard.ContentManagement.MetaData;
using Orchard.Environment.Extensions;
using Orchard.Localization.Models;
using Orchard.Localization.Services;
using Orchard.Taxonomies.Drivers;
using Orchard.Taxonomies.Fields;
using Orchard.Taxonomies.Helpers;
using Orchard.Taxonomies.Models;
using Orchard.Taxonomies.Services;
Expand All @@ -24,25 +23,31 @@ public class LocalizedTaxonomyController : Controller {
private readonly ITaxonomyExtensionsService _taxonomyExtensionsService;

public LocalizedTaxonomyController(
IContentDefinitionManager contentDefinitionManager,
ILocalizationService localizationService,
ITaxonomyService taxonomyService,
ITaxonomyExtensionsService taxonomyExtensionsService) {
IContentDefinitionManager contentDefinitionManager,
ILocalizationService localizationService,
ITaxonomyService taxonomyService,
ITaxonomyExtensionsService taxonomyExtensionsService) {

_taxonomyService = taxonomyService;
_taxonomyExtensionsService = taxonomyExtensionsService;
_contentDefinitionManager = contentDefinitionManager;
_localizationService = localizationService;
}

[OutputCache(NoStore = true, Duration = 0)]
public ActionResult GetTaxonomy(string contentTypeName, string taxonomyFieldName, int contentId, string culture) {
public ActionResult GetTaxonomy(string contentTypeName, string taxonomyFieldName, int contentId, string culture, string selectedValues) {
return GetTaxonomyInternal(contentTypeName, taxonomyFieldName, contentId, culture, selectedValues);
}

protected ActionResult GetTaxonomyInternal (string contentTypeName, string taxonomyFieldName, int contentId, string culture, string selectedValues) {
var viewModel = new TaxonomyFieldViewModel();
bool autocomplete = false;
var contentDefinition = _contentDefinitionManager.GetTypeDefinition(contentTypeName);
if (contentDefinition != null) {
var taxonomyField = contentDefinition.Parts.SelectMany(p => p.PartDefinition.Fields).Where(x => x.FieldDefinition.Name == "TaxonomyField" && x.Name == taxonomyFieldName).FirstOrDefault();
var contentTypePartDefinition = contentDefinition.Parts.Where(x => x.PartDefinition.Fields.Any(a => a.FieldDefinition.Name == "TaxonomyField" && a.Name == taxonomyFieldName)).FirstOrDefault();
ViewData.TemplateInfo.HtmlFieldPrefix = contentTypePartDefinition.PartDefinition.Name + "." + taxonomyField.Name;
var fieldPrefix = contentTypePartDefinition.PartDefinition.Name + "." + taxonomyField.Name;
ViewData.TemplateInfo.HtmlFieldPrefix = fieldPrefix;
if (taxonomyField != null) {
var taxonomySettings = taxonomyField.Settings.GetModel<TaxonomyFieldSettings>();
// Getting the translated taxonomy and its terms
Expand All @@ -60,7 +65,33 @@ public ActionResult GetTaxonomy(string contentTypeName, string taxonomyFieldName
List<TermPart> appliedTerms = new List<TermPart>();
int firstTermIdForCulture = 0;
if (contentId > 0) {
appliedTerms = _taxonomyService.GetTermsForContentItem(contentId, taxonomyFieldName, VersionOptions.Published).Distinct(new TermPartComparer()).ToList();
var selectedIds = selectedValues.Split(',');
var destinationTaxonomyCulture = taxonomy.As<LocalizationPart>()?.Culture?.Culture;
foreach (var id in selectedIds) {
if (!string.IsNullOrWhiteSpace(id)) {
var intId = 0;
int.TryParse(id, out intId);
var originalTerm = _taxonomyService.GetTerm(intId);

// The original term has to be added to applied terms in the following scenarios:
// When the original term has no LocalizationPart, which means that, when creating the taxonomy, terms have been set to be culture neutral.
// When the culture of the original term matches the culture of the taxonomy.
// In any other scenario, get the localized term and add it to the applied terms list.
// If no localization is found, nothing is added to the list for the current id.
var otCulture = originalTerm.As<LocalizationPart>()?.Culture?.Culture;
if (!originalTerm.Has<LocalizationPart>() || string.Equals(destinationTaxonomyCulture, otCulture)) {
appliedTerms.Add(originalTerm);
} else {
// Get the localized term. If no localized term is found, no term should be added to applied terms list.
var t = _localizationService.GetLocalizedContentItem(originalTerm, culture);
if (t != null) {
// Localized term has been found
appliedTerms.Add(t.As<TermPart>());
}
}

}
}

// It takes the first term localized with the culture in order to set correctly the TaxonomyFieldViewModel.SingleTermId
var firstTermForCulture = appliedTerms.FirstOrDefault(x => x.As<LocalizationPart>() != null && x.As<LocalizationPart>().Culture != null && x.As<LocalizationPart>().Culture.Culture == culture);
Expand All @@ -86,16 +117,13 @@ public ActionResult GetTaxonomy(string contentTypeName, string taxonomyFieldName
TaxonomyId = taxonomy != null ? taxonomy.Id : 0,
HasTerms = taxonomy != null && _taxonomyService.GetTermsCount(taxonomy.Id) > 0
};
if (taxonomySettings.Autocomplete)
if (taxonomySettings.Autocomplete) {
autocomplete = true;
}
}
}
var templateName = autocomplete ? "../EditorTemplates/Fields/TaxonomyField.Autocomplete" : "../EditorTemplates/Fields/TaxonomyField";
return View(templateName, viewModel);
}
private IEnumerable<TermPart> GetAppliedTerms(ContentPart part, TaxonomyField field = null, VersionOptions versionOptions = null) {
string fieldName = field != null ? field.Name : string.Empty;
return _taxonomyService.GetTermsForContentItem(part.ContentItem.Id, fieldName, versionOptions ?? VersionOptions.Published).Distinct(new TermPartComparer());
return PartialView(templateName, viewModel);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected override DriverResult Editor(TermPart termPart, IUpdateModel updater,
if (localizedParentTerm != null && localizedParentTerm != parentTerm)
_notifier.Add(NotifyType.Information, T("The parent term has been changed to its localized version associated to the culture {0}.", localizedParentTerm.As<LocalizationPart>().Culture.Culture));
}
else if (termCulture != taxonomyCulture && taxonomyCulture != null && _localizationService.GetLocalizations(parentTaxonomy).Count() > 0) {
else if (taxonomyCulture != null && termCulture.Culture != taxonomyCulture.Culture && _localizationService.GetLocalizations(parentTaxonomy).Count() > 0) {
//I can associate to a taxonomy a term of a different culture only if the taxonomy is unlocalized or has no translations
updater.AddModelError("WrongTaxonomyLocalization", T("A localization of the taxonomy in the specified language does not exist. Please create it first."));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
<ItemGroup>
<Compile Include="AdminMenu.cs" />
<Compile Include="Controllers\AdminController.cs" />
<Compile Include="Controllers\AdminLocalizedTaxonomyController.cs" />
<Compile Include="Controllers\LocalizedTaxonomyController.cs" />
<Compile Include="Drivers\LocalizedTaxonomyFieldDriver.cs" />
<Compile Include="Drivers\LocalizedTaxonomyPartDriver.cs" />
Expand Down
Loading