Liferay, reading from multiple language.properties file on same jsp - internationalization

I have a jsp file with the requirement of reading from the multiple language.properties file i.e with locales fr_CAN and other.Is it possible to read and display in same jsp file(Eg.based on the key in liferay-ui-message) by taking the value from these files with out changing the locale in sessions and with out using resource bundle.
For eg.
In Language.fr_CAN.properties
hello=Hello in canada french
and in In Language.en_CAN.properties
hello=Hello in canada english
Now in the Jsp file we have to display:
Say Hello...
Canada French : Hello in canada french
Canada English : Hello in canada English
Any help will be much appreciated..Thank you.

You can use <fmt:message/> tag to achieve what you need.
For example you can try following in your Jsp file:
<%# taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html>
<head>
<title>JSTL fmt:message Tag</title>
</head>
<body>
<fmt:requestEncoding value="UTF-8" />
<fmt:setLocale value="fr_CAN"/>
<fmt:setBundle basename="content.Language" />
<fmt:message key="say-hello" />
<fmt:message key="greetings" />
<fmt:message key="namaste" />
</body>
</html>
And in your Language.properties file:
say-hello=Hello in English
greetings=Greetings in English
namaste=Namaste in English
In Language.fr_CAN.properties
say-hello=Hello in canada french
greetings=Greetings in canada french
namaste=Namaste in canada french
and in Language.en_CAN.properties
hello=Hello in canada english
greetings=Greetings in canada english
namaste=Namaste in canada english
Here you can control the value to display in your jsp file by using <fmt:setLocale /> tag.
Simply you can give it a value of the locale like "en_US", "fr_FR" or "en_CAN" to control the value it takes from the properties files.So set it in the request and display in the jsp as:
<% Locale locale = LocaleUtil.fromLanguageId(request.getParameter("locale-name"))%>
<fmt:setLocale value="<%=locale%>"/>

You can call API instead of <liferay-ui:message /> tag:
<%= LanguageUtil.get(new Locale("fr", "CAN"), "your-message-key-here") %>
<%= LanguageUtil.get(new Locale("en", "CAN"), "your-message-key-here") %>

Related

JSTL: c:if Not Found in Build Path

I recently migrated an ANT project to Maven. After doing all the dependecnies and getting the project error free. I see this warning on in Eclipse:
The tag handler class for "c:if" (org.apache.taglibs.standard.tag.rt.core.IfTag) was not found on the Java Build Path
I see the same warnings for c:import, c:out c:set as well.
I do have the tag:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
added in the JSP. And I have the JSTL 1.2 dependency added in the pom file. Can you tell me how to get rid of these warnings?
I had the same error. To fix it, I just added and saved the following in my pom.xml.
<!-- https://mvnrepository.com/artifact/org.apache.taglibs/taglibs-standard-impl -->
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.1</version>
</dependency>
In some older Eclipse versions, you may need to modify the "<c:if..." line of code, remove a character or more, save it then put it back as it was and save the file again. This in some case get rid of the warning.
My code used <c:set and <c:if as the following example. No warning displays. Perhaps, you may need to update your Eclipse. My Eclipse is IDE for Enterprise Java Developers Version: 2019-09 R (4.13.0).
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<%# attribute name="normalPrice" fragment="true" %>
<%# attribute name="onSale" fragment="true" %>
<%# variable name-given="name" %>
<%# variable name-given="price" %>
<%# variable name-given="origPrice" %>
<%# variable name-given="salePrice" %>
<head>
<title>Tag Example</title>
</head>
<table border="1">
<tr>
<td>
<c:set var="name" value="Hand-held Color PDA"/>
<c:set var="price" value="$298.86"/>
<jsp:invoke fragment="normalPrice"/>
</td>
<td>
<c:set var="name" value="4-Pack 150 Watt Light Bulbs"/>
<c:set var="origPrice" value="$2.98"/>
<c:set var="salePrice" value="$2.32"/>
<jsp:invoke fragment="onSale"/>
</td>
<td>
<c:set var="name" value="Digital Cellular Phone"/>
<c:set var="price" value="$68.74"/>
<jsp:invoke fragment="normalPrice"/>
</td>
<td>
<c:set var="name" value="Baby Grand Piano"/>
<c:set var="price" value="$10,800.00"/>
<jsp:invoke fragment="normalPrice"/>
</td>
<td>
<c:set var="name" value="Luxury Car w/ Leather Seats"/>
<c:set var="origPrice" value="$23,980.00"/>
<c:set var="salePrice" value="$21,070.00"/>
<jsp:invoke fragment="onSale"/>
</td>
</tr>
</table>
<body>
<c:set var = "salary" scope = "session" value = "${2000*2}"/>
<c:if test = "${salary > 2000}">
<p>My salary is: <c:out value = "${salary}"/><p>
</c:if>
</body>
If the tag works actually fine, it is just a false negative from Eclipse. In that case you could force a new check editing the tag name and then setting it back to the right name.
So, for instance, click on your "c:if" tag, remove a single character and then re-enter it (exactly as it was before). Save it. In my case that was enough to get rid of the warning.

Output not visible jstl c:out not showing on browser

Problem: When I open page view source this data is shown on browser
Hello there jsp
<c:forEach var="student" items="2abcnullmca">
<c:out value= ""/>
</c:forEach>
<c:out value= "abc" />
And on browser it does not show c:out value in spring I have used model.addAttribute() to show data
Actually I forgot to include jstl tag thanks for looking into it but.
Here is the Tag -
<%# taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>

How to get Xpath for the following?

I have a xml file like following
<topic>
<title>Abstract
</title>
<body>
<p>
abstract data
</p>
</body>
</topic>
<topic>
<title>Keywords</title>
<body>
<p>
keywords data
</p>
</body>
</topic>
I have to check if title is "Keywords" than show the <p>text in </p>.
can anyone help me to get the exact xpath for this?
Thanks in advance
Try this one and let me know the result:
//title[text()="Keywords"]/following::p
or
//topic[title[text()="Keywords"]]//p
//title[text()="Keywords"]/body/p
for text only
//title[text()="Keywords"]/body/p/text()
please avoid double slash "//" and following, it will travel all the P tag
Try this below xpath
//title[text()="Keywords"]/following::p
Explanation of xpath:- Start your xpath with <title> along with text method and move ahead to the <p> tag using the following keyword.

Simple wkhtmltopdf conversion with framesets creating empty pdf

We need to convert/provide our html-based in-app HelpSystem to an on-disc pdf for the client to view outside of the application.
I'm trying to use wkhtmltopdf with a very basic file (3 frames with links to simple .html files) but getting an empty .pdf when I run the following from the command line:
wkhtmltopdf "C:\Program Files (x86)\wkhtmltopdf\index.html" "c:\delme\test.pdf"
I know frames are somewhat deprecated but it’s what I’ve got to deal with. Are the frames causing the empty pdf?
Index.html:
<html>
<head>
<title>Help</title>
</head>
<frameset cols="28%, 72%">
<frameset rows="8%, 92%">
<frame noresize="noresize" src="Buttons.html" name="UPPERLEFT" />
<frame noresize="noresize" src="mytest2.html" name="LOWERLEFT" />
</frameset>
<frame noresize="noresize" src="mytest.html" name="RIGHT" />
</frameset>
</html>
mytest.html:
<html>
<body>
<p>
<b>This text is bold</b>
</p>
<p>
<strong>This text is strong</strong>
</p>
<p>
<em>This text is emphasized</em>
</p>
<p>
<i>This text is italic</i>
</p>
<p>
<small>This text is small</small>
</p>
<p>This is
<sub>subscript</sub> and
<sup>superscript</sup></p>
</body>
</html>
mytest2.html:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h2>The blockquote Element</h2>
<p>The blockquote element specifies a section that is quoted from another source.</p>
<p>Here is a quote from WWF's website:</p>
<blockquote cite="http://www.worldwildlife.org/who/index.html">For 50 years, WWF has been protecting the future of nature. The
world’s leading conservation organization, WWF works in 100 countries and is supported by 1.2 million members in the United
States and close to 5 million globally.</blockquote>
<p>
<b>Note:</b> Browsers usually indent blockquote elements.</p>
<h2>The q Element</h2>
<p>The q element defines a short quotation.</p>
<p>WWF's goal is to:
<q>Build a future where people live in harmony with nature.</q> We hope they succeed.</p>
<p>
<b>Note:</b> Browsers insert quotation marks around the q element.</p>
</body>
</html>
buttons.html:
![<html>
<body>
<center>
<table>
<tr>
<td>
<form method="link" action="mytest.html" target="LOWERLEFT">
<input type="submit" value="Contents" />
</form>
</td>
<td>
<form method="link" action="mytest2.html" target="LOWERLEFT">
<input type="submit" value="Index" />
</form>
</td>
</tr>
</table>
</center>
</body>
</html>][2]
Taken from the official wkhtmltopdf issues area from a code project member’s answer; emphasis is mine:
wkhtmltopdf calculates the TOC based on the H* (e.g. H1, H2 and so on)
tags in the supplied documents. It does not recurse into frames and
iframes.. It will nest dependend on the number, to make sure that it
does the right thing, it is good to make sure that you only have
tags under a tag and not for some k larger
then 1. 2000+ files sounds like a lot. You might run out of memory
while converting the output. If it does not work for you.. you could
try using the switch to dump the outline to a xml file, to see what it
would but into a TOC.

How to get index in jstl display table

I have display table like below
<display:table name="sample" id="sample" class="display-table" style="width:100%;">
</display:table>
Now I want to get index of loop like for first element 0 for second 1 and 2,3,4... go on. How it is possible with display table.
For just reference we can do like this in JSTL forEach loop with help of varStatus variable like below
<c:forEach items="${sample}" var="clm" varStatus="status">
${status.index}
</c:forEach>
So is this possible with display table?
display table tag implicitly exposes row number named id_rowNum where id is specified in display table tag.
In your scenario:
<display:table name="sample" id="sample" class="display-table" style="width:100%;">
<display:column title="Row Number" >
<c:out value="${sample_rowNum - 1}"/>
</display:column>
...
</display:table>
Also make sure to include core tag as:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
You can find more information about implicit objects of display table tag here.

Resources