Bram
10/11/2024, 11:27 AMJemayn
10/12/2024, 11:22 AMBram
10/15/2024, 7:28 AMpublic async Task HandleAsync(ContentPublishedNotification notification, CancellationToken cancellationToken)
{
foreach (var content in notification.PublishedEntities)
{
if (content.ContentType.VariesByCulture())
{
var allLanguages = _localizationService.GetAllLanguages();
var allCultures = allLanguages.Select(l => l.IsoCode).ToList();
var currentCulture = content.CultureInfos?.Values.FirstOrDefault()?.Culture;
foreach (var culture in allCultures)
{
// do logic for each
CreateNewVersion()
}
}
}
}
Bram
10/15/2024, 7:28 AMprivate void CreateNewCultureVersion(IContent content, string culture, Dictionary<string, string> newValues)
{
using (var scope = _scopeProvider.CreateScope())
{
try
{
// Retrieve existing content in the target culture, if it exists
var existingContent = _contentService.GetById(content.Id);
if (existingContent == null)
{
// Handle case where content does not exist
return;
}
// Update properties with altered versions
foreach (var property in existingContent.Properties)
{
existingContent.SetValue(property.Alias, newValues[property.Alias], culture);
}
// Save the updated content for the target culture
_contentService.Save(existingContent);
scope.Complete();
}
catch (Exception ex)
{
_logger.LogError(ex, "Could not create new page variant");
throw;
}
}
}
Jemayn
10/15/2024, 7:41 AMBalázs Kerper
10/15/2024, 8:29 AMIContent
you can potentially just make the changes for all languages, and do 1 save operation instead of however many cultures you have. It would speed up the handler, and potentially you would have less points of failure for DB locks as well.Bram
10/15/2024, 11:17 AMBram
10/15/2024, 11:18 AMJemayn
10/15/2024, 11:21 AMBram
10/16/2024, 8:37 PMIContent.SetCultureName(string value, string culture)
but now i've created a new version of my page! Thanks a lot for the help