Liferay 7 Language Properties Hook

Liferay 7 Language Properties Hook tutorial will drive you on overriding liferay core language properties.

Types of language properties:

  • Liferay core language properties  : Application level properties which is accessible  to all modules:   “portal-impl/content/” folder
  • Liferay module language properties: Module level properties

In this tutorial,  we will see overriding Liferay DXP core language properties and  next tutorial will give you example to override liferay module language properties,

Liferay 7 Core Language Properties Hook:

    • Create liferay language module project.  In Eclipse: File -> New -> Liferay Module Project and select project template Name as “activator” and click on Finish. We will make the activator to component later.Liferay 7 Language Hook
    • Delete the generated package along with activator and create new  package: org.javasavvy.hook.language
    • edit bnd.bnd
      • Remove the bundle activator
      • Update the Bundle-Symbolic  Name to ” org.javasavvy.hook.language”
      • bnd.bnd file is:
        1. Bundle-Name: hooks-language
          Bundle-SymbolicName:org.javasavvy.hook.content.language
          Bundle-Version: 1.0.2
    • Edit the build.gradle and add below dependencies. Right click on project and select  -> Gradle ->Refresh Gradle Project to refresh the gradle dependencies.
      1. dependencies {
         compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "2.0.0"
         compileOnly group: "com.liferay.portal", name: "com.liferay.util.taglib", version: "2.0.0"
         compileOnly group: "javax.portlet", name: "portlet-api", version: "2.0"
         compileOnly group: "javax.servlet", name: "javax.servlet-api", version: "3.0.1"
         compileOnly group: "jstl", name: "jstl", version: "1.2"
         compileOnly group: "org.osgi", name: "org.osgi.compendium", version: "5.0.0"
         compile group: "org.osgi", name:"org.osgi.service.component.annotations", version:"1.3.0"
        }
    • Create the Component class: CustomLanguageComponent.java
    • Add the @Component annotation with below config:
      1. @Component(
         property = { "language.id=en_US" }, 
         service = ResourceBundle.class
        )
    • In the @Component annotation,provide language.id from below locales which ever you want to override. language.id does not take multple locales, need  to create new module again if you wish to override language properties for another locale. This is only limitation.
      1. locales=ar_SA,eu_ES,bg_BG,ca_AD,ca_ES,zh_CN,zh_TW,hr_HR,cs_CZ,da_DK,nl_NL,nl_BE,en_US,en_GB,en_AU,et_EE,
        fi_FI,fr_FR,fr_CA,gl_ES,de_DE,el_GR,iw_IL,hi_IN,hu_HU,in_ID,it_IT,ja_JP,ko_KR,lo_LA,lt_LT,nb_NO,fa_IR,
        pl_PL,pt_BR,pt_PT,ro_RO,ru_RU,sr_RS,sr_RS_latin,sl_SI,sk_SK,es_ES,sv_SE,tr_TR,uk_UA,vi_VN 
         sv_SE,tr_TR,uk_UA,vi_VN
    • Create the content folder in src/main/resource
    • Create Language.properties and Language_en.properties files
    • Project Structure is: 
    • CustomLanguageComponent.java:
      • This class need to extend ResourceBundle
      • Resource.getBundlle(“{foldername.Language}”)
      • package org.javasavvy.hook.language;
        
        import java.util.Enumeration;
        import java.util.ResourceBundle;
        import org.osgi.service.component.annotations.Component;
        import com.liferay.portal.kernel.language.UTF8Control;
        
        @Component(
         property = { "language.id=en_US" }, 
         service = ResourceBundle.class
        )
        public class CustomLanguageComponent extends ResourceBundle {
         
        ResourceBundle bundle = ResourceBundle.getBundle("content.Language", UTF8Control.INSTANCE);
        
         @Override
         protected Object handleGetObject(String key) {
         System.out.println("getting key"+key);
         return bundle.getObject(key);
         }
        
         @Override
         public Enumeration<String> getKeys() {
         return bundle.getKeys();
         }
        
        }
    • Authenticate with invalid credentials and you can see customized error message:

 

some times, you might get the below exception.  to fix this, move the Language.properties to  /src/main/resource/content folder.

java.util.MissingResourceException: Can’t find bundle for base name content.Language, locale en_US

 

Download Liferay DXP Language Hook Code

Leave a Comment

Your email address will not be published. Required fields are marked *