In the previous tutorial about Internationalization (i18n) in liferay we use Language portlet to change the language .But what if we don't want to use language portlet.Today we create portlet that change language by using Locale means if our website open in France that all website change to french and if our website open in U.S.A than all content change to English. So lets start this Step by Step:-
Step 1:-Create portlet with properties file
Just create a same portlet as discuss in Internationalization (i18n) in liferay and create a Language_fr.properties file for french and Language.properties file for English in content folder.And paste this Content:-
Language.properties
welcome_message =Welcome to Liferay is Easy
Language_fr.properties
welcome_message =Welcome to Liferay is Easy in French
Note:- I don't know how to write this in french so i just change the message.
Step 2:-Change the controller
Inside open your controller and paste this:-
Demo.java
Step 1:-Create portlet with properties file
Just create a same portlet as discuss in Internationalization (i18n) in liferay and create a Language_fr.properties file for french and Language.properties file for English in content folder.And paste this Content:-
Language.properties
welcome_message =Welcome to Liferay is Easy
welcome_message =Welcome to Liferay is Easy in French
Note:- I don't know how to write this in french so i just change the message.
Step 2:-Change the controller
Inside open your controller and paste this:-
Demo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.demo; | |
import java.io.IOException; | |
import java.util.Locale; | |
import javax.portlet.PortletConfig; | |
import javax.portlet.PortletException; | |
import javax.portlet.RenderRequest; | |
import javax.portlet.RenderResponse; | |
import com.liferay.portal.kernel.language.LanguageUtil; | |
import com.liferay.portal.kernel.util.JavaConstants; | |
import com.liferay.util.bridges.mvc.MVCPortlet; | |
public class Demo extends MVCPortlet { | |
@Override | |
public void doView(RenderRequest renderRequest,RenderResponse renderResponse) throws IOException, PortletException { | |
Locale defaultLocale = Locale.getDefault(); | |
PortletConfig portletConfig = (PortletConfig) renderRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG); | |
String message = LanguageUtil.get(portletConfig, defaultLocale, "welcome_message"); | |
System.out.println("Message=>"+message); | |
renderRequest.setAttribute("message", message); | |
super.doView(renderRequest, renderResponse); | |
} | |
} |
Explanation:-
Here we get the default locale of the machine which we set when we install our Operating System and than get the message from properties file using this default locale. And Finally set this to request so that we can use this on jsp.
Step 3:-Change the view
open view.jsp and paste this:-
view.jsp
That's it you can deploy and check the output.You can check out video tutorial for the same.
Step 3:-Change the view
open view.jsp and paste this:-
view.jsp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> | |
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %> | |
<portlet:defineObjects /> | |
${message} |
That's it you can deploy and check the output.You can check out video tutorial for the same.
No comments:
Post a Comment