Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Edouard Perr
How are managed Language.properties in Portlets ?
December 28, 2012 4:19 AM
Answer

Edouard Perr

Rank: Junior Member

Posts: 30

Join Date: August 20, 2012

Recent Posts

Hello there,

I would like to know how are managed Language.properties (with Language_xx.properties) in portlets plugin ?
More specifically, I would like to know how encoding is managed :

The situation is that I access them with <liferay-ui:message ... /> without any encoding trouble. In French, special characters like "é à ô" are well displayed in my jsp.
After that, I need to access my language properties files in my processAction() method on my portlet in order to retrieve validation message, depending on the current locale. I chose ResourceBundle as I can give it the current locale, retrieved from the themeDisplay :
1ResourceBundle.getBundle("content/Language", themeDisplay.getLocale());

But the trouble here is ResourceBundle reads the props as ISO and not UTF-8.

So my question is : How do <liferay-ui:message> to access Language.properties, dynamically choose the right language and accept special characters ?
I would like to reproduce that in my processAction() method.

For now, I give the property keys to my JSP and I use <liferay-ui:message> to access the values with these keys.

Thanks for your help.
EP
Jelmer Kuperus
RE: How are managed Language.properties in Portlets ?
December 28, 2012 5:08 AM
Answer

Jelmer Kuperus

Rank: Liferay Legend

Posts: 1190

Join Date: March 10, 2010

Recent Posts

Use

1ThemeDisplay.translate
Edouard Perr
RE: How are managed Language.properties in Portlets ?
December 28, 2012 6:23 AM
Answer

Edouard Perr

Rank: Junior Member

Posts: 30

Join Date: August 20, 2012

Recent Posts

Translate only retrieve the key actually. Like LanguageUtil.get()
It only works from the jsp, not from portlet methods.

I would like to use these from portlet. Wht is the mechanism behind ? That is my question.
Jelmer Kuperus
RE: How are managed Language.properties in Portlets ?
December 28, 2012 7:17 AM
Answer

Jelmer Kuperus

Rank: Liferay Legend

Posts: 1190

Join Date: March 10, 2010

Recent Posts

I am not quite sure if i understand you correctly.

There are two ways to define resource keys for your own portlets

1) use a hook eg add a file called WEB-INF/liferay-hook.xml to your portlet that kind of looks like this

1<?xml version="1.0"?>
2<!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 6.1.0//EN" "http://www.liferay.com/dtd/liferay-hook_6_1_0.dtd">
3<hook>
4    <language-properties>content/Language.properties</language-properties>
5</hook>


The resourcebundles you reference here should be utf-8 encoded and liferay will read them by using

1PropertiesUtil.load(inputstream, StringPool.UTF8);


As far as i know using themeDisplay.translate should work for resource bundles you define in that way even from within actions etc.

2) Use the jsr286 way of defning resource keys in your WEB-INF/portlet.xml file

1<?xml version="1.0"?>
2<portlet-app version="2.0" xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd">
3
4    <portlet>
5        ....
6        <resource-bundle>content.Language</resource-bundle>
7    </portlet>
8</portlet-app>


The resource bundles you reference here should be ISO 8859-1 encoded. And if you want to use characters not defined in this charset you must use the appropriate unicode escape sequence. For example \u20AC to get the euro symbol

The standard way to get to this resourcebundle is via.

1ResourceBundle res = getPortletConfig() .getResourceBundle(locale);


But directly loading a resourcebundle from the classpath in the way you are doing should also work, but remember the file you are reading is ISO 8859-1 with escape sequences
Edouard Perr
RE: How are managed Language.properties in Portlets ?
December 28, 2012 7:44 AM
Answer

Edouard Perr

Rank: Junior Member

Posts: 30

Join Date: August 20, 2012

Recent Posts

Thanks for your answer.

I am sorry but I have some trouble to well-understand. With the first solution with the hook, you define only one <language-properties> tag for all languages_xx property files? So I suppose, translate() knows which language to select with theme display? Right?

For info, I only have the xml entry in my portlet.xml : <resource-bundle>content/Language</resource-bundle>
It sounds not sufficient and I should create the hook... To use themeDisplay.translate(), and more globally LanguageUtil I need to define this hook in a liferay-hook.xml ? Is it why the method themeDisplay.translate() does not find my files and so my values ?
If yes, why my jsp are able to read them correctly (with <liferay-ui:message>)?

Finaly, my solution works well... with ISO 8859-1, not well with UTF-8. I know for unicode, but I wish to have readable property files, so I would like to find a better solution than using unicode.

Sorry for being noob on that point!
Jelmer Kuperus
RE: How are managed Language.properties in Portlets ?
December 28, 2012 8:28 AM
Answer

Jelmer Kuperus

Rank: Liferay Legend

Posts: 1190

Join Date: March 10, 2010

Recent Posts

you define only one <language-properties> tag for all languages_xx property files?


yes

So I suppose, translate() knows which language to select with theme display?


Yes, themeDisplay knows about the locale of the user. You are already aware of this because your original code used themeDisplay.getLocale()

For info, I only have the xml entry in my portlet.xml : <resource-bundle>content/Language</resource-bundle>
It sounds not sufficient and I should create the hook...


It depends. Using only jsr286 api's ensures that your portlets are portable across portlet containers. Using the hook makes things slightly easier if you don't mind being tied to Liferay.

use themeDisplay.translate(), and more globally LanguageUtil I need to define this hook in a liferay-hook.xml ? Is it why the method themeDisplay.translate() does not find my files and so my values ?


Yes

If yes, why my jsp are able to read them correctly (with <liferay-ui:message>)?


To resolve a resource key

ThemeDisplay uses : LanguageUtil.get(locale, key)

MessageTag uses : LanguageUtil.get(pageContext, _key);

If you pass in pagecontext as an argument then LanguageUtil will look up the portletConfig and will use it to load resource keys you declared in your portlet.xml file
It is confusing but that is how it works

Finaly, my solution works well... with ISO 8859-1, not well with UTF-8. I know for unicode, but I wish to have readable property files, so I would like to find a better solution than using unicode.


If you use the hook then your resource bundles can be defined as utf-8, so you do not have to use weird escape sequences.

iso 8859-1 is a character set that contains only 256 different characters, that is why you need to resort to escape sequences when you want to use a chracter that is not part of the 256 characters that iso 8859-1 defines

utf-8 is a unicode encoding that let's you use many many different characters