How to get culture in published content in umbraco 13
Hi everyone,
I need to retrieve the culture from the published content. In the CMS, I have both English and Arabic, but the code below simply prints the English version when the content is published in Arabic. How can I get the specific culture when publishing content? Any ideas?
var culture = publishedContent.GetCultureFromDomains();
Full code
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Web.Common;
using Microsoft.Extensions.Logging;
using Umbraco.Cms.Core.Strings;
using Umbraco.Cms.Core.Models.Blocks;
using Umbraco.Cms.Core.Models.PublishedContent;
using ManarEthara.DTO;
using uSync.Core;
using System.Net.Http;
using Newtonsoft.Json;
using System.Text;
using Microsoft.AspNetCore.Http;
public class ExternalAPIHandler : INotificationHandler<ContentPublishedNotification>
{
private readonly IContentService _contentService;
private readonly IUmbracoHelperAccessor _umbracoHelperAccessor;
private readonly IShortStringHelper _shortStringHelper;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly ILogger<ExternalAPIHandler> _logger;
private readonly HttpClient _httpClient;
public ExternalAPIHandler(IContentService contentService, IUmbracoHelperAccessor umbracoHelperAccessor, IShortStringHelper shortStringHelper, ILogger<ExternalAPIHandler> logger, HttpClient httpClient, IHttpContextAccessor httpContextAccessor)
{
_contentService = contentService;
_umbracoHelperAccessor = umbracoHelperAccessor;
_shortStringHelper = shortStringHelper;
_httpContextAccessor = httpContextAccessor;
_logger = logger;
_httpClient = httpClient;
_httpClient.Timeout = TimeSpan.FromSeconds(3600);
}
public async void Handle(ContentPublishedNotification notification)
{
if (!_umbracoHelperAccessor.TryGetUmbracoHelper(out var helper))
{
_logger.LogWarning("Failed to get UmbracoHelper.");
return;
}
foreach (var node in notification.PublishedEntities)
{
if (node.ContentType.Alias.Equals("handpickedTour", StringComparison.OrdinalIgnoreCase) || node.ContentType.Alias.Equals("programme", StringComparison.OrdinalIgnoreCase))
{
var content = _contentService.GetById(node.Id);
if (content == null)
{
System.Diagnostics.Debug.WriteLine($"Content not found for ID: {node.Id}");
continue;
}
var publishedContent = helper.Content(node.Id);
if (publishedContent == null)
{
System.Diagnostics.Debug.WriteLine($"Published content not found for ID: {node.Id}");
continue;
}
var culture = publishedContent.GetCultureFromDomains();
System.Diagnostics.Debug.WriteLine($"Content Culture: {culture}");
var event_id = publishedContent.Value<string>("event_id");
var title = publishedContent.Value<string>("title", culture: culture);
var description = publishedContent.Value<string>("shortDescription", culture: culture);
var location = publishedContent.Value<string>("location", culture: culture);
if (event_id == "")
{
var schedulesList = new List<Schedules>();
var scheduleBlocks = publishedContent.Value<IEnumerable<BlockListItem>>("schedules");
if (scheduleBlocks != null)
{
foreach (var block in scheduleBlocks.Select(x => x.Content))
{
System.Diagnostics.Debug.WriteLine($"totalCapacity ==> {block.Value<string>("totalCapacity")}");
var schedule = new Schedules
{
fromDateTime = block.Value<string>("fromDateTime"),
toDateTime = block.Value<string>("toDateTime"),
totalCapacity = block.Value<string>("totalCapacity"),
};
schedulesList.Add(schedule);
}
}
System.Diagnostics.Debug.WriteLine($"Content Id: {node.Id}, Title: {title}, Description: {description}, Schedules List: {schedulesList.Count}, Location: {location}, EventId: {event_id}");
var apiResponse = await CallExternalApiCreate(title, description, location, schedulesList, culture);
if (apiResponse?.@event != null)
{
content.SetValue("event_id", [email protected]);
content.SetValue("bookTicketUrl", apiResponse.registrationLink);
_contentService.Save(content);
System.Diagnostics.Debug.WriteLine($"Content with ID {node.Id} updated and event created.");
}
else
{
System.Diagnostics.Debug.WriteLine("API Not getting success.");
}
}
else
{
System.Diagnostics.Debug.WriteLine($"Content Id: {node.Id}, Title: {title}, Description: {description}, Location: {location}, EventId: {event_id}");
var apiResponse = await CallExternalApiUpdate(title, description, location, culture, event_id);
if (apiResponse?.@event != null)
{
//content.SetValue("event_id", [email protected], culture: culture);
//content.SetValue("bookTicketUrl", apiResponse.registrationLink, culture: culture);
System.Diagnostics.Debug.WriteLine($"Content with ID {node.Id} updated and event updated.");
}
else {
System.Diagnostics.Debug.WriteLine("API Not getting success.");
}
}
}
else
{
System.Diagnostics.Debug.WriteLine("Not a handpickedTour content item.");
}
}
}
How to get culture in published content in umbraco 13
Hi everyone,
I need to retrieve the culture from the published content. In the CMS, I have both English and Arabic, but the code below simply prints the English version when the content is published in Arabic. How can I get the specific culture when publishing content? Any ideas?
Full code
Hi Santhosh,
You can get the cultures from ILocalizationService. Please refer the documentation here
you don't have to pass in culture for your
instead you can just use IVariationContextAccessor and switch between culture variants. please refer the documentation here
Hope this helps
is working on a reply...