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

VCST-2591: try localizing outline_named #775

Merged
merged 2 commits into from
Feb 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -483,11 +483,11 @@ protected virtual async Task AddLabelsAsync(IList<Aggregation> aggregations, str
}
}

var outlineAggregations = aggregations.Where(x => x.Field == "__outline");
await AddLabelsForOutlinesAsync(outlineAggregations);
var outlineNamedAggregations = aggregations.Where(x => x.Field == "__outline_named");
await TryAddLabelsAsyncForOutlinesItems(outlineNamedAggregations);
}

private async Task AddLabelsForOutlinesAsync(IEnumerable<Aggregation> outlineAggregations)
private async Task TryAddLabelsAsyncForOutlinesItems(IEnumerable<Aggregation> outlineAggregations)
{
var caregoriesDictionary = new Dictionary<string, Category>();

Expand All @@ -496,44 +496,63 @@ private async Task AddLabelsForOutlinesAsync(IEnumerable<Aggregation> outlineAgg
{
foreach (var aggregationItem in outlineAggregation.Items)
{
var aggregationItemLabel = aggregationItem.Labels.FirstOrDefault();
if (aggregationItemLabel != null)
var categoryId = GetNamedOutlineId(aggregationItem.Value as string);
if (categoryId != null)
{
caregoriesDictionary.TryAdd(aggregationItemLabel.Label, null);
caregoriesDictionary.TryAdd(categoryId, null);
}
}
}

// Load categories
var categries = await _categoryService.GetAsync(caregoriesDictionary.Keys.ToArray(), clone: false);
foreach (var category in categries)
{
caregoriesDictionary[category.Id] = category;
}

// Replace category ids with category names
// Add localized category names to labels
foreach (var outlineAggregation in outlineAggregations)
{
foreach (var aggregationItem in outlineAggregation.Items)
{
var aggregationItemLabel = aggregationItem.Labels.FirstOrDefault();
if (aggregationItemLabel != null &&
caregoriesDictionary.TryGetValue(aggregationItemLabel.Label, out var category) &&
category != null)
var categoryId = GetNamedOutlineId(aggregationItem.Value as string);
if (categoryId != null &&
caregoriesDictionary.TryGetValue(categoryId, out var category) &&
category != null &&
category.LocalizedName.Values.Count > 0)
{
aggregationItemLabel.Label = category.Name;
var lozalizedLabels = category.LocalizedName.Values
.Select(x => new AggregationLabel { Language = x.Key, Label = !string.IsNullOrEmpty(x.Value) ? x.Value : category.Name }).ToList();

if (category.LocalizedName.Values.Count > 0) // Add the language code to the label
var defaultCategoryLabel = aggregationItem.Labels.FirstOrDefault(x => x.Language == null);
if (defaultCategoryLabel != null)
{
var lozalizedLabels = category.LocalizedName.Values
.Select(x => new AggregationLabel { Language = x.Key, Label = !string.IsNullOrEmpty(x.Value) ? x.Value : category.Name }).ToList();
lozalizedLabels.Add(aggregationItemLabel);
aggregationItem.Labels = lozalizedLabels.ToArray();
defaultCategoryLabel.Label = category.Name;
lozalizedLabels.Add(defaultCategoryLabel);
}

aggregationItem.Labels = lozalizedLabels.ToArray();
}
}
}
}

private static string GetNamedOutlineId(string namedOutlineValue)
{
if (string.IsNullOrEmpty(namedOutlineValue))
{
return null;
}

// Outline structure: catalog/category1/.../categoryN/current-category-id___current-category-name
var outlineParts = namedOutlineValue.Split("/", StringSplitOptions.RemoveEmptyEntries);
var namedOutline = outlineParts[^1];

var namedOutlineParts = namedOutline.Split(ModuleConstants.OutlineDelimiter, StringSplitOptions.RemoveEmptyEntries);
return namedOutlineParts.Length == 2 ? namedOutlineParts[0] : namedOutline;
}

private async Task<bool> TryAddLabelsAsyncForProperty(IEnumerable<Property> allProperties, Aggregation aggregation)
{
// There can be many properties with the same name
Expand Down
Loading