Language.properties file in Liferay - java

I want to support multiple languages for my portlet application. So I:
Created content package in src folder.
Created Language.properties file with string Book-Name=Book Name
Paste this line
<supported-locale>es</supported-locale>
<resource-bundle>content/Language</resource-bundle>
in portlet.xml file.
So could you please tell me why I still have Book-Name here?!
<liferay-ui:search-container>
<liferay-ui:search-container-results results="${bookListArray}" />
<liferay-ui:search-container-row className="com.softwerke.model.Book" modelVar="aBook">
<liferay-ui:search-container-column-text property="bookName" name="Book-Name"/>
<liferay-ui:search-container-column-text property="bookDescription" name="Description"/>
<liferay-ui:search-container-column-jsp path="/html/action.jsp" align="right" />
</liferay-ui:search-container-row>
<liferay-ui:search-iterator />
</liferay-ui:search-container>
UPDATE
This:
<liferay-ui:search-container-column-text property="bookName" name="${bookName}" />
....
<c:set var="bookName"> <liferay-ui:message key="book-Name" /> </c:set>
does NOT work too

You are not using it at all.
The name="Book-Name" in this line
<liferay-ui:search-container-column-text property="bookName" name="Book-Name"/>
adds name property to this html component with valye defined inside the quotation marks to make this value the one defined in the properties file you have to use <liferay-ui:message /> tag in your case it would be:
:
<liferay-ui:search-container-column-text property="bookName" name="<liferay-ui:message key="Book-Name" />"/>
Also it is not relevant but a dev practice that the language key is all lower case.

Related

What is the Struts2 equivalent for <bean:message> tag with property attribute in Struts1? For example:<bean:message name="user" property="label" />

In Struts 1 the <bean:message> tag is usually used with the key attribute in order to get the message from the properties file by specifying the message-key in the key attribute.
And in struts 2 we do that by using either <s:text> or <s:property> tags.
Consider the below example:
In struts 1,
<bean:message key="user.name.required"/>
The equivalent in Struts 2 is,
<s:text name="user.name.required" /> or
<s:property value="getText('user.name.required')"/> where,
user.name.required = Name is required
is the content of the properties file.
But I'm confused with a different scenario.
<bean:message name="user" property="label" />
for this, I tried <s:property value="getText('label')"/>
but the value returned is the string "label" instead of the message string "User data is missing"
and I also tried <s:property value="label"/>
But the value returned is message key i.e. "user.data.missing" instead of the message string "User data is missing"
<bean:message name="user" property="label" /> in Struts 1 returns the message User data is missing.
where,
user.data.missing=User data is missing
is the content of the properties file.
My question is how do I achieve this in Struts 2?
Try the following code
<s:property value="getText(label)"/>
where label is a variable in the value stack which contains a message key i.e. "user.data.missing" .

Thymeleaf #strings.replace with regex and th:fragment

I have persisted entity with field text. Inside text I have part to replace. myEntity.text:
some text
twoImages[12_v13.PNG, 10_v6.PNG]
text text text
twoImages[12_v13.PNG, 10_v6.PNG]
<h1>And HTML</h1>
So in view I want to use something like th:utext. But with replaced images blocks with th:fragment (~15 lines per fragment).
Output should be like this:
some text
<img .. 12_v13.PNG />
<br />
<img .. 12_v13.PNG />
<additional html />
text text text
<img .. 12_v13.PNG />
<br />
<img .. 12_v13.PNG />
<additional html />
<h1>And HTML</h1>
How to realize this with Thymeleaf?
If the entire text of the field is a single string, you will have to parse it using regex matcher or some generated parser. Say [12_v13.PNG, 10_v6.PNG] has to be parsed e.g. with
\\[([^,]),\\s([^\\]]*)]
The first group will give 12_v13.PNG, the second one - 10_v6.PNG.
You need to provide the path to each of the images in src property of img tag.
You can achieve the result this way:
<img th:src="#{/images/test.png}" />
It is implied, that /images folder is within webapp folder of your project.
<img th:src="#{/resources/images/Picture.png}" />
Output as:
<img src="/resources/image/Picture.png" />
When you hit http://localhost:8080/myapp/resources/images/Picture.png in you browser then you should be able to access the image for the above syntax to work.
This link is useful: Standard URL Syntax
Please have a look at my thymeleaf demo project: demo project (the folder with templates will get opened) You'll find data on project structure and examples of thymeleaf templates.

GWT Locale does work in address bar, does not work in meta

I have a gwt portlet with (for now) localization in English (default) and Dutch.
If I put ?locale=nl& in the address bar, I get nice Dutch texts. However, if I put <meta name="gwt:property" content="locale=nl" > in the head of my html file, without any reference to locale in the address bar, I get the English texts. When I look into the rendered html source, I can see the <meta> tag. According to the documentation it should give Dutch texts.
Does anybody have a suggestion about where I should look to fix this?
You could set fallback property value for locale in your module's .gwt.xml which will be used as default
<set-property-fallback name="locale" value="nl" />
Then if you need to change localization (e.g. to English) set correct locale URL parameter when loading portlet.
Also remember to set these properties in you module's .gwt.xml
<extend-property name="locale" values="nl" />
<extend-property name="locale" values="en" />
I found it, I put my meta tag at the end of my part, so it was after the tag. I assumed the head was loaded completely before anything was done. Stupid me.
Remember to include your meta tag in the head but BEFORE the js script containing you GWT module code is called.

JSTL fmt:message and resource bundle

I want to set the "dir" property of my table from resource bundle based on the locale.
Here is snippet:
<fmt:setBundle basename="class.path.to.resource.bundle"/>
<table align=center class="" dir=<fmt:message key="registration.direction"/>>
When the page renders I get this:
<table align=center dir=???registration.direction???>
I have two resource bundles for english and arabic.
registration.direction = ltr -> English
registration.direction = rtl -> Arabic
Please tell what I am doing wrong? The dir should have "ltr" or "rtl" depending on the locale.
Thanks
BR
SC
two things
1) I would add a variable to store the message result in
<fmt:message key="registration.direction" var="direction" />
then
2) I would do the following with your code
<fmt:setBundle basename="class.path.to.resource.bundle"/>
<fmt:message key="registration.direction" var="direction" />
<table align=center class="" dir="${direction}">
Now as far as your resource bundles, typically You should have the following structure for your resource bundles
/foo/bar/MyResourceBundle.properties
/foo/bar/MyResourceBundle_en.properties
/foo/bar/MyResourceBundle_en_US.properties
/foo/bar/MyResourceBundle_<lang>[_COUNTRY[_VAR]].properties
If your bundle is not structured in this fashion that might be some of your problem.
Make sure that all keys that are expected to be available are defined in MyResourceBundle with reasonable defaults.
I'm amending this answer as I'm not sure if my comment got lost in a hide function.
With the fact that you are using Struts 2, I'm under the impression that you're using the i18n interceptor. The interceptor will store the current locale in the sesion variable named WW_TRANS_I18N_LOCALE. As such you should be able to get to it and set the locale for the JSTL tags by using the following:
<fmt:setLocale scope="session" value="${sessionScope.WW_TRANS_I18N_LOCALE}" />
Hope that works for you.

How do we get the absolute path to the applications root directory using Spring?

I have an app that may run at http://serverA/m/ or http://serverA/mobile/. I have a shared header with a search form that needs to go to http://serverA/installationName/search.
However, if I use <form action="/search"> it goes to the root of the server, not the tomcat application.
If I use <form action="search"> it goes to a path relative to the current page. (i.e http://serverA/m/someOtherPage/search
I've tried <c:url value="search"> and <c:url value="/search"> but neither of them seem to work.
In intelliJ, <c:url value="/search"> gives me "Cannot resolve controller URL /search" even though I have a controller defined with #RequestMapping("/search")
<form action="<c:url value="/search" />" />
Using <c:url> is the way. Ignore what the IDE tells you. They are not good at that. Just try to run it.
Bozho is right. I have used HTML BASE tag too:
<base href="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}/" />
If you can put this tag in a few places (ideally in only one JSP) you can get your code cleaner.
You can (apart from other responders hints) also use Spring JSP tag (spring:url) which is modeled after the JSTL c:url tag (see Bozhos reply). The tld reference:
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/spring.tld.html#spring.tld.url
And the bottom of this mvc:resources block for an example use:
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-static-resources
you will not be able to imbed the c:url tag directly in the attribute, if your form tag is a jsp tag (perhaps, <sf:form>).
In that situation I do the following:
<c:url var="someName" value="some uri value"/>
<sf:form path="${someName}" ...>

Categories