Languagedisplay customization

Understanding Languages in Liferay Portal #

Liferay is designed to handle as many languages as you want to support. This is done by pulling out all language specific text and storing them in language.properties files. This way, when a page loads, Liferay will detect the language, pull up the corresponding language file and display the text in the correct language.

With this you can:

  • Easily support as many (or as few) languages as desire
  • Have a central location for multiple languages
  • Change the way a certain word is translated. If you wanted, you could effectively rename the "message boards" portlet to now be called "forums".

Customizations in portal.properties #

Liferay has many properties that can be easily configured to customize the portal. These can be found in the portal.properties file located portal/portal-impl/classes/portal.properties. To modify these values, create an ext version of this file which only contains the values you want to override. This ext version should be placed here: /ext/ext-impl/classes/portal-ext.properties

Setting a default Language and default Country #

You'll find the below setting in your system.properties file.

 #
 # Set the default language.
 #
 user.country=TR
 user.language=tr

Setting up a unique URL for different languages #

Liferay allows you to provide a unique URL for a specific language via our I18nServlet.

Examples are:

LanguageUnique URL
Chinese simplified http://www.liferay.com/zh_CN/web/guest/home
German http://www.liferay.com/de/web/guest/home

The mappings for this is done in web.xml:

<servlet-mapping> 
<servlet-name>I18nServlet</servlet-name> 
<url-pattern>/de/*</url-pattern> 
</servlet-mapping> 
<servlet-mapping> 
<servlet-name>I18nServlet</servlet-name> 
<url-pattern>/zh_CN/*</url-pattern> 
</servlet-mapping> 
<servlet-mapping> 
<servlet-name>I18nServlet</servlet-name> 
<url-pattern>/zh_TW/*</url-pattern> 
</servlet-mapping> 

This functionality would then allow you to point specific domains to a specific language of a site. (i.e. www.liferay.cn -> Chinese version of www.liferay.com)

Removing unwanted language #

By default, Liferay supports all the follow languages (excerpt from portal.properties of Liferay 4.3.3):

    locales=ar_SA,ca_AD,ca_ES,zh_CN,zh_TW,cs_CZ,nl_NL,en_US,fi_FI,fr_FR,de_DE,el_GR,hu_HU,it_IT,ja_JP,ko_KR,fa_IR,pt_BR,ru_RU,es_ES,sv_SE,tr_TR,vi_VN

If we only want to support English, German and Spanish, we simply remove the unwanted locales so that our locales value looks like this:

   locales=en_US,de_DE,es_ES   

Customizations in language.properties and other language files #

Priority of Language files #

There are many language files and every language file overwrites another language file, what is the priority of these language files?

There are three simple rules:

  • -ext versions take precedence over the non -ext versions
  • language specific versions take precedence over the non language specific versions
  • location specific versions take precedence over the non location specific versions

For English, here is a ranking

  1. Language-ext_en_US.properties
  2. Language_en_US.properties
  3. Language-ext_en.properties
  4. Language_en.properties
  5. Language-ext.properties
  6. Language.properties

Language/display information is defined in /portal/portal-ejb/classes/content/Language.properties, as well as various other Language_.properties files for foreign languages. Language.properties is the primary and "default" language definition file, but definitions in that file may be overriden by language specific definitions. For example, the file Language_en.properties contains the english versions of most of the definitions. The file Language_fr.properties contains the french version, etc.

Language files can further have locale specific definitions. Language_en_US.properties (if it exists), contains english phrase variations further defined for the United States. Other languages can also have locale specific definitions.

Entries in the language files can be re-defined on a deployment by deployment basis by overriding -ext.properties files stored in the /ext/ext-ejb/classes/content/ directory. /ext/ext-ejb/classes/content/Language-ext.properties, for example, overrides and/or extends the definitions found in /portal/portal-ejb/classes/content/Language.properties.

Important Notes #

  1. The locale specific definitions takes precedence, followed by language specific, followed by the default. Since most of the entries in Language.properties have language specific entries in Language_xx.properties, those will always take precedence. To override entries in Language_en.properties, for example, you would put your new entries in /ext/ext-ejb/classes/content/Language-ext_en.properties. As of Liferay 4.1.2, few locale specific files exist (there is no locale specific variations of the english file for example). The HIGHEST precedenced file for the United States, for example, is the extension to Language_en_US.properties, which would be /ext/ext-ejb/classes/content/Language-ext_en_US.properties. If you find your changes are not appearing, try putting them in that file.
  2. If there is more than one locale for the same language defined in the locales property, the first one in the list will be used when a translation is requested in another locale of the same language but a translated value cannot be found. For example, if there are two locales such us pt_BR and pt_PT (in this order), any key not found in pt_PT will be looked for in pt_BR.

Changing existing language entries #

Example: Changing the text of an existing portlet in Liferay from "Message Boards" to "Forums" #

Liferay currently displays "Message Boards" because of the entry in:

portal\portal-ejb\classes\content\Language_en.properties

javax.portlet.title.19=Message Boards

To change this, modify (or create) your Language-ext_en.properties file and add the entry:

ext\ext-ejb\classes\content\Language-ext_en.properties

javax.portlet.title.19=Forums

Creating new language entries #

For newly created portlets, it is likely that you will have new language specific text.

Example: Changing the title of a new portlet from "javax.portlet.title.EXT_1" to "Reports". #

Modify your Language-ext.properties file and add the entry:

ext\ext-ejb\classes\content\Language-ext.properties

javax.portlet.title.EXT_1=Reports

Displaying multiple languages in your JSP files #

So now that we know how all these languages are stored and how to overwrite them, the next question we should be asking is.. "how do we display the different languages in our JSP pages?"

You will find this line in the English portal_en.properties file:

use-ldap-password-policy=Use LDAP Password Policy

and this line in the Spanish portal_es.properties file:

use-ldap-password-policy=Utilice la politica de contraseñas de LDAP

To display this text, you would add this line to your jsp:

<liferay-ui:message key="use-ldap-password-policy" />

If you user has chosen to see the Spanish version on your site, it will display Spanish instead of English.

Using the LanguageUtil helper #

The LanguageUtil class can also be used in JSP files to provide a dynamic translation of certain language keys. This class is useful when there are multiple dynamic parameters that needs to be translated:

<%= LanguageUtil.format(pageContext, "back-to-x", pageTitle); %>

The above statement will show the label to return back to a page, with the page title included as an argument. It is also possible to add multiple arguments by passing in an Object array.

Messages that contain HTML syntax need another parameter of type LanguageWrapper, instead of the argument(s) of type Object:

<%= LanguageUtil.format(pageContext, "back-to-x", new LanguageWrapper("<b>", pageTitle, "</b>")); %>

Displaying multiple languages in your Java files #

The LanguageUtil class in Liferay can be used to provide a programmatic translation of certain language keys.

Frequently Asked Questions #

Note: You may have initially found the entry in "Language.properties" and so you may think that you would just need to override that value by editing "Language-ext.properties". However, that will not change anything because all values in "Language.properties" are also in "Language_en.properties" and locale specific definitions take precedence. Therefore, "Language_en.properties" will override any changes you made in "Language-ext.properties". That is why we edited the "Language-ext_en.properties" file.

You edit the same files to change Language Settings, Portlet Titles, Category Titles, Action Names, Messages and any other text that is language dependant.

Related Articles #

The page's current languageId can be pulled from themeDisplay.getLanguageId(), for example

0 Anexos
139704 Visualizações
Média (5 Votos)
A média da avaliação é 4.4 estrelas de 5.
Comentários
Respostas do tópico Autor Data
Thanks for excellent article! kuhajeyan gunaratnam 22 de Novembro de 2008 03:48
Yes, is a excellent article, but can anyone... Marcos González Castro 27 de Janeiro de 2009 01:38
How do I combine "virtual host" on a community... Benny Meyns 20 de Fevereiro de 2009 05:54
And just in case you have the same problems... Sander Bos 18 de Junho de 2009 05:46
In lifreray 5.2.3 the language_ext.properties... Faris Abdulla 9 de Outubro de 2009 03:32
This was exactly what I was looking for... Thanks! Ray Seaward 22 de Setembro de 2010 08:22
"And just in case you have the same problems... Ray Seaward 22 de Setembro de 2010 08:23
Thanks for pointing out that location! That was... Marcus Howarth 21 de Outubro de 2014 06:58
I'm using Liferay 5.2. I have change the... MohdFaizul Sulaiman 29 de Julho de 2010 19:00
I'm using Liferay 6.0.2.and i have the same... Miller Huang 11 de Agosto de 2010 02:05
Use the Liferay IDE it really helps you figure... Flavel --------- 24 de Setembro de 2010 06:13
Can you please confirm that for JSP pages the... Dave Weitzel 29 de Abril de 2011 12:58
How to change the Language only for Calendar... Manikandan S 2 de Junho de 2011 02:26
I am still frustrated by Language text... Dave Weitzel 19 de Agosto de 2011 13:46
Useful stuff Thank you. Regards, Vishal Vishal Panchal 23 de Novembro de 2012 21:11
I struggled with internationalization of... Nish K 22 de Janeiro de 2015 04:19
The content-portal folder has to be created... Nish K 22 de Janeiro de 2015 04:22

Thanks for excellent article!
Postado em 22/11/08 03:48.
Yes, is a excellent article, but can anyone tell me how to display a text using Velocity instead of JSP.

I mean: What is the Velocity version on this sentence?

<liferay-ui:message key="use-ldap-password-policy" />
Postado em 27/01/09 01:38.
How do I combine "virtual host" on a community together with this "I18nServlet"-behaviour?

e.g.: "www.birds.com" is a virtual host for a community "birds" on "www.animals.com/web/birds/"

To get direct url to the dutch version of the birds-community, I can use:
"www.animals.com/nl/birds/"

But how can I get a direct (shorter) URL using the virtual host?
"www.birds.com/nl/" doesn't work out.
(The main purpose is to get SEO-friendly URL's)
Postado em 20/02/09 05:54.
And just in case you have the same problems with finding the folder to put this in as I had, if you want to do this in a deployment you should use .....ROOT\WEB-INF\classes\content\Language-ext_en.properties
(so in a to be created subfolder 'content' under the directory where portal-ext.properties resides)
Postado em 18/06/09 05:46.
In lifreray 5.2.3 the language_ext.properties not overriding language.properties.
Postado em 09/10/09 03:32 em resposta a Sander Bos.
I'm using Liferay 5.2. I have change the portal-ext.properties to include only 3 locales but the Language Portlet still showing all. The tomcat log shown that it read the correct portal-ext file.
However, when I point it to an empty database, it's working.
Postado em 29/07/10 19:00.
I'm using Liferay 6.0.2.and i have the same problem as you.i do not know why.
Postado em 11/08/10 02:05 em resposta a MohdFaizul Sulaiman.
This was exactly what I was looking for... Thanks!
Postado em 22/09/10 08:22 em resposta a Sander Bos.
"And just in case you have the same problems with finding the folder to put this in as I had, if you want to do this in a deployment you should use .....ROOT\WEB-INF\classes\content\Language-ext_en.properties
(so in a to be created subfolder 'content' under the directory where portal-ext.properties resides)"

This was exactly what I was looking for... Thanks!
Postado em 22/09/10 08:23 em resposta a Sander Bos.
Use the Liferay IDE it really helps you figure out what files should belong where. I was missing a liferay-hook.xml (http://www.liferay.com/dtd/liferay-hook_6_0_0.dtd) file in my WEB-INF which this document does not mention at all.

Also I am on Liferay version 6.0.5
Postado em 24/09/10 06:13 em resposta a huang Miller min.
Can you please confirm that for JSP pages the file is called portal_en.properties as written in your article? I think that is a mistake?

I am actually using Language_en.properties (deployed into <hook>/WEB-INF/classes/content) and identified in liferay-hook.xml as:
<language-properties>content/Language_en.properties</language-properties>

but am not getting tags such as <aui:input inlineLabel="left" label="hide-tabs" name="hidetabs">
to actually display my text instead of hide-tabs
I have
hide-tabs
hideTabs
hidetabs

all defined and nothing is changing the label from hide-tabs

so wondering if all the other people have it wrong and should be using portal_en.properties?
Using 6.0.6
Postado em 29/04/11 12:58.
How to change the Language only for Calendar Portlet?
Postado em 02/06/11 02:26.
I am still frustrated by Language text localization. This time I am writing a new portlet and including a number of language files in WEB-INF/src/content/Language_xx.properties files. I am tyring to localize the portal title as shown in the portlet title area and in control panel display (its an admin portlet). .
The portlet name is (defined in liferay-portlet.xml) "theme-administration".
I have tried setting:
javax.portlet.title.theme-administration
javax.portlet.title.themeadminis­tration
javax.portlet.title.themeadministration_WAR_themeadministrationportlet

is there any other combination to try?
perhaps this is why so many non-liferay portlets have no language translations.

I have also got a site where the en_US locale displays non English language keys.

How does one force a particular (or all) Language file to be "re-read"?
Postado em 19/08/11 13:46 em resposta a Mani kandan.
Useful stuff
Thank you.

Regards,
Vishal
Postado em 23/11/12 21:11.
Thanks for pointing out that location! That was the only way I could get changes to take effect across the control panel.

I now have a Language-ext_en_GB.properties under
<liferay>/<tomcat>/webapps/ROOT/WEB-INF/classes/content/

with all the correct spellings of Organisation!
Postado em 21/10/14 06:58 em resposta a Sander Bos.
I struggled with internationalization of placeholder and label in my aui:input tag. I finally solved it by using placeholder='<%= LanguageUtil.get(themeDisplay.getLocale(), "phrase-to-be-translated") %>'. The translations have to be published to liferay via a hook so that LanguageUtil can find them. LanguageUtil doesn't read the Language.properties files in portlets. To publish i18n data to liferay, create a content-portal folder and put your Language.properties data into it. Create a liferay-hook.xml file if it doesn't already exist and add
<language-properties>
content-portal/Language.properties
</language-properties> . Deploy your plugin.
Postado em 22/01/15 04:19.
The content-portal folder has to be created within docroot/WEB-INF/src as the content folder.
Postado em 22/01/15 04:22 em resposta a Nish K.