Usage of LanguageUtil class in Liferay

November 12, 2015 by Sandeep Bhardwaj | Tags:


For fetching properties based on locale from properties file in liferay is easy. Use get() method of com.liferay.portal.kernel.language.LanguageUtil class of liferay. It will automatically pick the correct locale file and return the property value.

There are 6 overloaded get methods in LanguageUtil class.

get(Locale locale, String key): String  LanguageUtil  
get(Locale locale, String key, String defaultValue) String  LanguageUtil  
get(PageContext pageContext, String key) String  LanguageUtil  
get(PageContext pageContext, String key, String defaultValue) String  LanguageUtil  
get(PortletConfig portletConfig, Locale locale, String key) String  LanguageUtil  
get(PortletConfig portletConfig, Locale locale, String key, String defaultValue) String  LanguageUtil

Usage

<%@page import="com.liferay.portal.kernel.language.LanguageUtil"%>  
<%  
String propertyValue=LanguageUtil.get(pageContext, "property_name", new String("NOT_FOUND"));  
%>  

Argument of get method :-

1st: page context.
2nd: property name whose value to be fetched.
3rd: default value return by get method, if property not found.