5 min read

Customizing page management with more features

The Ext Manage Pages portlet not only clones the out of the box Manage Pages portlet, but it also extends the model and service — supporting customized data, for example, Keywords. We can make these Keywords localized too.

 

Adding localized feature

Liferay portal is designed to handle as many languages as you want to support. By default, it supports up to 22 languages. When a page is loading, the portal will detect the language, pull up the corresponding language file, and display the text in the correct language.

We want the Keywords to be localized too. For example, the default language is English (United States) and the localized language is Deutsch (Deutschland). Thus, you have the ability to enter not only the Name and HTML Title in German, but also the Keywords in German.

As shown in the following screenshot, when you change the language of the page in German using the language portlet, you will see the entire web site changed to German, including the portlet title and input fields. For example, the title of the portlet now has the Ext Seiteneinstellungen value and the Keywords now become Schlüsselwörter.

How do we implement this feature? In other words, how do we customize the language display in the page management? Let’s add the localized feature for the Ext Manage Pages portlet.

Liferay Portal 5.2 Systems Development

Extending model for locale

First of all, we need to extend the model and to implement that model in order to support the localized feature. For the ExtLayout model, let’s add the locale method first.

  1. Locate the ExtLayout.java file from the com.ext.portlet.layout.model package in the /ext/ext-service/src folder, and open it.
  2. Add the following lines before the line } in ExtLayout.java and save it:
      public String getKeywords(Locale locale);
      public String getKeywords(String localeLanguageId);
      public String getKeywords(Locale locale,
      boolean useDefault);
      public String getKeywords(String localeLanguageId,
      boolean useDefault);
      public void setKeywords(String keywords, Locale locale);

As shown in the code above, it adds getting and setting methods for the Keywords field with locale features. Now let’s add the implementation for the ExtLayout model:

  1. Locate the ExtLayoutImpl.java file from the com.ext.portlet.layout.model.impl package in the /ext/ext-impl/src folder and open it.
  2. Add the following lines before the last } in ExtLayoutImpl.java file and save it:
      public String getKeywords(Locale locale) {
      String localeLanguageId = LocaleUtil.toLanguageId(locale);
      return getKeywords(localeLanguageId);
      }
      public String getKeywords(String localeLanguageId) {
      return LocalizationUtil.getLocalization(getKeywords(),
      localeLanguageId);
      }
      public String getKeywords(Locale locale, boolean useDefault) {
      String localeLanguageId = LocaleUtil.toLanguageId(locale);
      return getKeywords(localeLanguageId, useDefault);
      }
      public String getKeywords(String localeLanguageId, boolean
      useDefault) {
      return LocalizationUtil.getLocalization( getKeywords(),
      localeLanguageId, useDefault);
      }
      public void setKeywords(String keywords, Locale locale) {
      String localeLanguageId = LocaleUtil.toLanguageId(locale);
      if (Validator.isNotNull(keywords)) {
      setKeywords(LocalizationUtil.updateLocalization(
      getKeywords(), "keywords", keywords, localeLanguageId));
      }
      else {
      setKeywords(LocalizationUtil.removeLocalization(
      getKeywords(), "keywords", localeLanguageId));
      }
      }

As shown in the code above, it adds implementation for get and set methods of the ExtLayout model.

Customizing language properties

Language files have locale-specific definitions. By default, Language.properties (at /portal/portal-impl/src/content) contains English phrase variations further defined for United States, while Language_de.properties (at /portal/portal-impl/src/content) contains German phrase variations further defined for Germany. In Ext, Language-ext.properties (available at /ext/ext-impl/src/content) contains English phrase variations further defined for United States, while Language-ext_de.properties (should be available at /ext/ext-impl/src/content) contains German phrase variations further defined for Germany.

First, let’s add a message in Language-ext.properties, by using the following steps:

  1. Locate the Language-ext.properties file in the /ext/ext-impl/src/content folder and open it.
  2. Add the following line after the line view-reports=View Reports for Books and save it.
      keywords=Keywords

This code specifies the keywords message key with a Keywords value in English:

Then we need to add German language feature in Language-ext_de.properties as follows:

  1. Create a language file Language-ext_de.properties in the /ext/ext-impl/src/content folder and open it.
  2. Add the following lines at the beginning and save it:
    ## Portlet names
    javax.portlet.title.EXT_1=Berichte
    javax.portlet.title.jsp_portlet=JSP Portlet
    javax.portlet.title.book_reports=Berichte für das Buch
    javax.portlet.title.extLayoutManagement=Ext Seiteneinstellungen
    javax.portlet.title.extCommunities=Ext Communities
    ## Messages
    view-reports=Ansicht-Berichte für Bücher
    keywords=Schlüsselwörter
    ## Category titles
    category.book=Buch
    ## Model resources
    model.resource.com.ext.portlet.reports.model.ReportsEntry= Buch ## Action names
    action.ADD_BOOK=Fügen Sie Buch hinzu

As shown in the code above, it specifies the same keys as that of Language-ext.properties. But all the keys’ values were specified in German instead of English. For example, the message keywords has a Schlüsselwörter value in German.

In addition, you can set German as the default language and Germany as the default country if it is required. Here are the simple steps to do so:

  1. Locate the system-ext.properties file in the /ext/ext-impl/src folder and open it.
  2. Add the following lines at the end of system-ext.properties and save it:
      user.country=DE
      user.language=de

The code above sets the default locale — the language German (Deutsch) and the country Germany (Deutschland). In general, there are many language files, for example Language-ext.properties and Language-ext_de.properties, and some language files would overwrite others in runtime loading. For example, Languageext_de.properties will overwrite Language-ext.properties when the language is set as German.

These are the three simple rules which indicate the priorities of these language files:

  1. The ext versions take precedence over the non-ext versions.
  2. The language-specific versions, for example _de, take precedence over the non language-specific versions.
  3. The location-specific versions, such as -ext_de, take precedence over the non location-specific versions.

For instance, the following is a ranking from bottom to top for the German language:

  1. Language-ext_de.properties
  2. Language_de.properties
  3. Language-ext.properties
  4. Language.properties

LEAVE A REPLY

Please enter your comment!
Please enter your name here