Getting current published language/culture variant in ContentService events
Hi,
I have multi lingual site and it is implemented using language variants.
Is there a way to get which language variant or Culture is published in ContentService events?
ContentService events are implemented in custom component and when I'm trying to get culture using Thread.CurrentThread.CurrentCulture.Name it always gets 'en-Us'.
IContent in event only has properties with all cultures.
Is there a way to get this language variant when in ContentService events?
https://prnt.sc/u0ewfy
Don't think the accepted answer is an answer to the question raised, just tried it. Below are my findings, correct me if I'm wrong.
And feel free to point out a better solution to this.
The content.PublishedCultures gives back all cultures that were at some point in the past ever published, not just the one being Saved (or Published). And just gives an array of string. So if it has never been published before and you do a Save it's skipped.
And it doesn't help in determing which of the cultures has any changes in the Save event. Currently looking if I can find something that does.
The best solution that works that I have come up with is this (Still not very elegant when you have a large number of cultures or properties to monitor).
private void ContentService_Saving(Umbraco.Core.Services.IContentService sender, Umbraco.Core.Events.ContentSavingEventArgs e)
{
if (e.SavedEntities?.Count() == 0)
{
return;
}
foreach(var savedNode in e.SavedEntities)
{
if (savedNode.ContentType.Alias== "PUT YOUR ALIAS HERE")
{
// Now let's find the culture that is recently changes
var existingNode = sender.GetById(savedNode.Id);
foreach (var culture in savedNode.AvailableCultures)
{
string newTitle = savedNode.GetValue<string>("Title", culture);
string oldTitle = existingNode.GetValue<string>("Title", culture);
if (newTitle != oldTitle)
{
var dummy = $"There is a change in the title property for node {savedNode.Name} for culture {culture} ";
}
// Repeat this for every field that you want to monitor for changes in Save ...
}
}
}
}
And of course don't hardcode the "Put Your Alias here" and "Title" but use strongly typed objectmodel and then use GetModelPropertyType + ModelTypeAlias, but that's another discussion.
Hi Patrick, I agree with you. I think that the question was about how to retrieve the languages of a web page modified from the BO. Indeed, inside the concrete "IComponent" object, that you have created to intercept some BO events, you need the language culture to perform some "SetValue()" actions.
I said languages, because from the BO, for the single web page, you can save and publish more than a language per time.
To intercept for which languages the user performed an update in the BO, I used the method "event.IsSavingCulture(...)" as described in the "Saving" chapter, in the events reference manual.
Below, in the "savingCultures" list of strings you will get only the language code of the updated page, for example if you have a web page in two languages, and you will update only the Italian, and not the English, you will get a list of one "it-it" element.
Here the implementation:
// register our component with Umbraco using a Composer
[RuntimeLevel(MinLevel = RuntimeLevel.Run)]
public class WhenAPageIsSavedComposer : ComponentComposer<WhenAPageIsSavedComponent>
{
// nothing needed to be done here!
}
public class WhenAPageIsSavedComponent : IComponent
{
// initialize: runs once when Umbraco starts
public void Initialize()
{
// do something as Umbraco starts up
// for example subscribe to an event
// Event List: https://our.umbraco.com/documentation/reference/events/contentservice-events
// subscribe to content service saving event
ContentService.Saving += ContentService_Saving;
}
private void ContentService_Saving(IContentService sender, Core.Events.ContentSavingEventArgs e)
{
foreach (IContent savedEntity in e.SavedEntities)
{
List<string> savingCultures = savedEntity.AvailableCultures.Where(f => e.IsSavingCulture(savedEntity, f)).ToList();
//Cicle every modified culture
foreach (string savedCulture in savingCultures)
{
//Do your stuff HERE with "savedEntity" and "savedCulture"
}
}
}
// terminate: runs once when Umbraco stops
public void Terminate()
{
// do something when Umbraco terminates
ContentService.Saving -= ContentService_Saving;
}
}
Getting current published language/culture variant in ContentService events
Hi,
I have multi lingual site and it is implemented using language variants.
Is there a way to get which language variant or Culture is published in ContentService events?
ContentService events are implemented in custom component and when I'm trying to get culture using Thread.CurrentThread.CurrentCulture.Name it always gets 'en-Us'.
IContent in event only has properties with all cultures.
Is there a way to get this language variant when in ContentService events? https://prnt.sc/u0ewfy
Did you mean in saved and Published Events? you can use PublishedCultures
Don't think the accepted answer is an answer to the question raised, just tried it. Below are my findings, correct me if I'm wrong.
And feel free to point out a better solution to this.
The content.PublishedCultures gives back all cultures that were at some point in the past ever published, not just the one being Saved (or Published). And just gives an array of string. So if it has never been published before and you do a Save it's skipped.
And it doesn't help in determing which of the cultures has any changes in the Save event. Currently looking if I can find something that does.
The best solution that works that I have come up with is this (Still not very elegant when you have a large number of cultures or properties to monitor).
And of course don't hardcode the "Put Your Alias here" and "Title" but use strongly typed objectmodel and then use GetModelPropertyType + ModelTypeAlias, but that's another discussion.
Hi Patrick, I agree with you. I think that the question was about how to retrieve the languages of a web page modified from the BO. Indeed, inside the concrete "IComponent" object, that you have created to intercept some BO events, you need the language culture to perform some "SetValue()" actions.
I said languages, because from the BO, for the single web page, you can save and publish more than a language per time.
To intercept for which languages the user performed an update in the BO, I used the method "event.IsSavingCulture(...)" as described in the "Saving" chapter, in the events reference manual.
Below, in the "savingCultures" list of strings you will get only the language code of the updated page, for example if you have a web page in two languages, and you will update only the Italian, and not the English, you will get a list of one "it-it" element.
Here the implementation:
I hope that this can help other Umbraco users.
Thank you
is working on a reply...