Localized rich text editor support for custom plugin portlets
Internalization (I18N) Support for Liferay custom plugin content.
Sometime client has a requirement is like to support multilingual for custom modules.
In Requirement
In custom portlet has a one from which will submit with different-different languages.
Like Web content, Polls, Workflows in Liferay etc.
When users view the submitted details or Client wants to showcase the submitted information to users based on user’s locales then the display portlets will automatically rendered into user’s selected locale (language).
How to support Internalization (I18N) to text field & Text Editors in custom portlets?
· Liferay has implanted such functionality in Polls, Web contents, Pages creations time ...
· Through
<liferay-ui:input-localized
> we can achieve for custom portlet also.
Use below
mentioned step to use in custom portlet
1. In custom jsp’s file use below syntax for Textboxes and text Fields.
view.jsp
<%@page import="com.liferay.portal.kernel.util.LocaleUtil"%>
<%@page import="com.liferay.examples.localizededitor.PostPortlet"%>
<%@page import="com.liferay.portal.kernel.dao.orm.QueryUtil"%>
<%@page import="com.liferay.portal.kernel.search.QueryConfig"%>
<%@page import="com.liferay.portal.kernel.dao.orm.QueryPos"%>
<%@page import="java.util.List"%>
<%@page import="com.liferay.examples.localizededitor.service.EntityLocalServiceUtil"%>
<%@page import="com.liferay.examples.localizededitor.model.Entity"%>
<%@page import="com.liferay.portal.kernel.util.ParamUtil"%>
<%@page import="com.liferay.portal.kernel.util.GetterUtil"%>
<%@page import="com.liferay.portal.kernel.util.Validator"%>
<%@page import="com.liferay.portal.kernel.util.LocalizationUtil"%>
<%@page import="java.util.Locale"%>
<%@page import="java.util.Map"%>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects/>
<ul>
<%
List<Entity> entities = EntityLocalServiceUtil.getEntity(0, QueryUtil.ALL_POS);
for (Entity curEntity : entities) {
%>
<portlet:renderURL var="entityURL">
<portlet:param name="entityId" value="<%= String.valueOf(curEntity.getEntityId()) %>"/>
</portlet:renderURL>
<li>
<a href="<%= entityURL %>"><%= curEntity.getTitle(LocaleUtil.getDefault()) %></a>
</li>
<%}%>
</ul>
<%
long entityId = ParamUtil.getLong(renderRequest, "entityId");
Entity entity = null;
if (entityId > 0) {
entity = EntityLocalServiceUtil.getEntityId(entityId);
}%>
<portlet:actionURL name="saveEntity" var="saveEntityURL" />
<aui:form action="<%= saveEntityURL %>" method="post">
<aui:fieldset>
<aui:model-context model="<%= Entity.class %>" bean="<%= entity %>" />
<aui:input name="entityTitle" />
<aui:input name="entityContent" />
<aui:button name="submit" type="submit" />
</aui:fieldset>
</aui:form>
|
2. Add below mentioned extra attributes into entity’s columns of service.xml for support to store localized data into DB.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.1.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_1_0.dtd">
<service-builder package-path="com.liferayone.example.richtexteditor">
<author>Sachin</author>
<namespace>I18N</namespace>
<entity name="Entity" local-service="true" remote-service="false" table="Entity">
<column name="entityId" type="long" primary="true" />
<column name="entityTitle" type="String" localized="true" />
<column name="entityContent" type="String" localized="true" />
</entity>
</service-builder>
|
3. Build the sevice.xml file.
4. Add below mentioned attributes to portlet-model-hit.xml file for specified columns.
<?xml version="1.0"?>
<model-hints>
<model name=" com.liferayone.example.richtexteditor.model.Entity"
<field name="entityId" type="long" />
<field name=" entityTitle " type="String" localized="true" />
<field name=" entityContent" type="String" localized="true">
<hint-collection name="EDITOR" />
</field>
</model>
</model-hints>
|
5. For storing data into DB use below code in action class.
In ActionController.java classes.
public void saveEntity(ActionRequest request, ActionResponse response)
throws PortalException, SystemException {
Map<Locale, String> title =
LocalizationUtil.getLocalizationMap(request, "entityTitle");
Map<Locale, String> content =
LocalizationUtil.getLocalizationMap(request, "entityContent");
long entityId = CounterLocalServiceUtil.increment();
Entity entity = EntityLocalServiceUtil.create(entityId );
entity.setEntityTitleMap(title);
entity.setEntityContentMap(content);
EntityLocalServiceUtil.addEntity(entity);
}
|
Comments