Cookies help us deliver our services.

By using our services, you agree to our use of cookies. Learn more

I understand

In a previous article (Text localization with Javascript) we have seen how to localize text with Javascript, with the limitation that the translations were the portal ones and not the portlet ones.

Let see how to localize text inside a custom portlet Java class.

During any processing step within a portlet (namely the Java class of the portlet) you can have the need to create some localized messages to be viewed in page or in a log file.

We have already seen how to use the class LanguageUtil provided by Liferay:

LanguageUtil.get(pageContext, "sample-text");

But in the Java class the page context is not available...

The problem is easily solved using another method of the LanguageUtil class with which you can localize both portal messages and custom portlet messages:

ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
// Using only locale you can get portal's translations String text1 = LanguageUtil.get(themeDisplay.getLocale(), "no-entries-were-found");
// Using portlet configuration you can get custom portlet's translations String text2 = LanguageUtil.get(getPortletConfig(), themeDisplay.getLocale(), "this-is-a-custom-message");