With Internationalization, we are emphasizing that the web application should support multiple languages, and with Localization, we are stating that the text, date, or other fields should be presented in a form specific to a region.
For internationalization, first we need to specify the resource bundle definition under the application
tag in faces-config.xml
, as follows:
A resource bundle is a text file with the .properties
suffix that would contain locale-specific messages. So, the preceding definition states that the resource bundle messages_{localekey}.properties
file will reside under classpath, and the default value of localekey
is en
, which stands for English, and the supported locale is tr_TR
, which stands for Turkish. For projects structured by Maven, the messages_{localekey}.properties
file can be created under the src/main/resources
project path. The following image was made in the IntelliJ IDEA:
To showcase Internationalization, we will broadcast an information message via the FacesMessage
mechanism that will be displayed in PrimeFaces' growl component. We need two components—growl itself and a command button—to broadcast the message:
The addMessage
method of localizationBean
is as follows:
The preceding code uses the addInfoMessage
method, which is defined in the static MessageUtil
class as follows:
Localization of components, such as calendar
and schedule
, can be achieved by providing the locale
attribute. By default, locale information is retrieved from the view's locale, and it can be overridden by a string locale key or with a java.util.Locale
instance.
Components such as calendar
and schedule
use a shared PrimeFaces.locales
property to display labels. As stated before, PrimeFaces only provides English translations, so in order to localize the calendar, we need to put the corresponding locales into a JavaScript file and include the scripting file to the page.
The content for the German locale of the Primefaces.locales
property for calendar
would be as shown in the following code snippet. For the sake of the recipe, only the German locale definition is given and the Turkish locale definition is omitted; you can find it in the showcase application Here's the code snippet we talked about:
PrimeFaces.locales['de'] = {
closeText: 'Schließen',
prevText: 'Zurück',
nextText: 'Weiter',
monthNames: ['Januar', 'Februar', 'März', 'April', 'Mai',
'Juni', 'Juli', 'August', 'September', 'Oktober', 'November',
'Dezember'],
monthNamesShort: ['Jan', 'Feb', 'Mär', 'Apr', 'Mai', 'Jun',
'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dez'],
dayNames: ['Sonntag', 'Montag', 'Dienstag', 'Mittwoch',
'Donnerstag', 'Freitag', 'Samstag'],
dayNamesShort: ['Son', 'Mon', 'Die', 'Mit', 'Don', 'Fre',
'Sam'],
dayNamesMin: ['S', 'M', 'D', 'M ', 'D', 'F ', 'S'],
weekHeader: 'Woche',
FirstDay: 1,
isRTL: false,
showMonthAfterYear: false,
yearSuffix: '',
timeOnlyTitle: 'Nur Zeit',
timeText: 'Zeit',
hourText: 'Stunde',
minuteText: 'Minute',
secondText: 'Sekunde',
currentText: 'Aktuelles Datum',
ampm: false,
month: 'Monat',
week: 'Woche',
day: 'Tag',
allDayText: 'Ganzer Tag'
};
The definition of the calendar
components both with and without the locale
attribute would be as follows:
They will be rendered as follows:
For some components, localization could be accomplished by providing labels to the components via attributes, such as with p:selectBooleanButton
:
The msg
variable is the resource bundle variable that is defined in the resource bundle definition in the PrimeFaces configuration file. The English version of the bundle key definitions in the messages_en.properties
file that resides under the classpath would be as follows:
The PrimeFaces Cookbook Showcase application
This recipe is available in the demo web application on GitHub (https://github.com/ova2/primefaces-cookbook/tree/second-edition). Clone the project if you have not done it yet, explore the project structure, and build and deploy the WAR file on application servers compatible with Servlet 3.x, such as JBoss WildFly and Apache TomEE.
For the demos of this recipe, refer to the following:
- Internationalization is available at
http://localhost:8080/pf-cookbook/views/chapter1/internationalization.jsf
- Localization of the calendar component is available at
http://localhost:8080/pf-cookbook/views/chapter1/localization.jsf
- Localization with resources is available at
http://localhost:8080/pf-cookbook/views/chapter1/localizationWithResources.jsf
For already translated locales of the calendar, see http://code.google.com/p/primefaces/wiki/PrimeFacesLocales.
Right to left language support
In PrimeFaces, components such as accordionpanel
, datatable
, dialog
, fileupload
, schedule
, tabview
, and tree
offer right-to-left text direction support for languages such as Arabic, Hebrew, and so on. These components possess the dir
attribute that can either get the value ltr
(which is the default behavior with left-to-right text direction) or rtl
.