Getting Liferay JournalArticle localized content in Java

I thought my adventures in Liferay would end at a hook and CKEditor, however I had to do some display of JournalArticles. And again, this was not an easy thing.

But I have found a solution. To get a Liferay JournalArticle’s content as XML, you can call

article.getContentByLocale("en-US");

However this returns you the content in XML — and this is a bit hard to display. The suggested solution is to use JournalContentUtil provided by Liferay. It has a method getContent where you set the right variables you get your content — and only the content.

String content = JournalContentUtil.getContent(groupId, articleId, templateId, themeDisplay.getLocale().toString(), themeDisplay);

I won’t go into detail which parameters to set and how to get them. There are some questions around on the internet (at the Liferay Forum too) where you can take a look at and get some information.

However this is not working as you might expect. If you have content in only one language: go along and use the suggested method (and look for information on the internet).

If you have translations in other languages, then it is not working. You can add a language attribute but it does not help either: you get the content in the main language back (or the language of the user logged in) — and it is a problem if you want to display the contents in another language.

Background

I needed to display the content in a foreign language because we migrated a portal from an old portal-software to Liferay. And in this old portal if you translated a site it was added as a separate page — so after moving to Liferay all the translated content were available multiple times.

For this we created a merging portlet where one of the features was to display the content behind the JournalArticles.

Solution

The suggested solution did not work. So I ended up with getting the XML content and extract the real content from it.
It was a simple solution:

final Document document = SAXReaderUtil.read(article.getContentByLocale("de_DE"));
final Node node = document.selectSingleNode("/root/static-content");

content = node.getText();

As you can see, the real solution is to get the content in the translated language as XML and extract the translated content block.

Naturally you can display the contents in the default language too. For this you should use


<span class="kw3">final Document</span> document = SAXReaderUtil.<span class="me1">read</span><span class="br0">(</span>article.<span class="me1">getDefaultLocale</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span>;

If you display the contents in a JSF page’s outputText field you should disable escaping and you get the formatted content displayed.

<h:outputText id=”content” escape=”false” value=”#{treeBasicView.selectedDocument.content}”/>

I hope my article helps you a bit if you stuck on how to display the content of a Liferay article in your browser.

Editing the content

Naturally, the solution of getting the content enables you to edit it (or extract the content and edit the content prior displaying). But this would be another article on its own — but I do not plan to write about it. If you would like to read about how to manipulate Liferay content, tell me and I will provide an article about this topic.

Advertisements

One thought on “Getting Liferay JournalArticle localized content in Java

  1. Pingback: Getting Liferay JournalArticle localized content in Java | Dinesh Ram Kali.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s