Identifying or Retrieving Specific Language of BlogPost Obje

Posted by tinglis on 11-Dec-2019 14:04

Hello,

I am currently subscribed to the IDataEvent and performing some actions when new blog posts are published. We have currently added translations to our posts and after retrieving the BlogPost object from the event, {var item = (BlogPost)manager.GetItemOrDefault(contentType, itemId);}, I am unable to identify the language of the post. There is a property in the BlogPost item, "AvailableLanguages":["","en","es"], but this property does not indicate the language of the content of the item I'm currently looking at.

Two options would be viable. 1) How am I able to identify the language of the BlogPost item? 2) How am I able to query for a specific language of the blog post?

Even though it appears the event.ItemId from IDataEvent is the same when either language is published, using manager.GetItemOrDefault(contentType, itemId); returns the language of the item that raised the event. I wish Sitefinity's documentation would have the structure of their objects. There has to be something that identifies the language.

Thanks for the help.

All Replies

Posted by jread on 18-Dec-2019 22:34

You can get at the translated fields by looking at var translations = post.PublishedTranslations; this will give a list of the published langues.  You then using the language code to access the fields translations.  item.Title["es'] or whatever the culture is.  If you want to use the manager to pull content in a specific language you need to change the culture of the context first to the language you want get the list then return it back to the original culture.

var cultureInfo = new CultureInfo(culture);
            var currentCulture = Thread.CurrentThread.CurrentUICulture;
            Thread.CurrentThread.CurrentUICulture = cultureInfo;

var posts = BlogsMaanger.GetManager().GetBlogPosts();//This should now retrieve the content items in the specificed culture

Thread.CurrentThread.CurrentUICulture;
            Thread.CurrentThread.CurrentUICulture = currentCulture;//set it back

Posted by tinglis on 19-Dec-2019 19:22

Thank you. All of this is good as well as getting me further on the right track. I was unaware that fields such as item.Title, Item.Content etc. were properties and not just strings. I can see another way of knowing what language I'm looking at is item.Content.CurrentLanguage.TwoLetterISOLanguageName.

This thread is closed