Documentation

Sharing Localized Messages

As you work on an application you might have multiple modules, each of which has its own language keys. Instead of maintaining various language properties files in different places, consolidate them into one place. This example project demonstrates how language keys can be shared across different modules.

Run the Tutorial Code

  1. Start Liferay DXP. If you don’t already have a docker container, use

    docker run -it -m 8g -p 8080:8080 liferay/dxp:7.3.10-dxp-1
    

    If you’re running a different Liferay Portal version or Liferay DXP, adjust the above command accordingly.

  2. Download and unzip Sharing Localized Messages.

    curl https://learn.liferay.com/dxp/latest/en/developing-applications/developing-a-java-web-application/using-mvc/liferay-u8t2.zip -O
    
    unzip liferay-u8t2.zip
    
  3. From the module root, build and deploy.

    ./gradlew deploy -Ddeploy.docker.container.id=$(docker ps -lq)
    

    Note

    This command is the same as copying the deployed jars to /opt/liferay/osgi/modules on the Docker container.

  4. Confirm the deployment in the Liferay Docker container console.

    STARTED com.acme.u8t2.impl_1.0.0 [1650]
    STARTED com.acme.u8t2.web_1.0.0 [1651]
    
  5. Verify that the example module is working. Open your browser to https://localhost:8080

  6. Add the U8T2 Portlet to a page. You can find the example portlet under Sample Widgets.

    Add the U8T2 Portlet to a page.

    You see the welcome message header and a list of colors. Note that the language keys for colors come from the shared language keys. The language key colors comes from Liferay’s global language keys.

  7. This example project also includes locales for Portuguese and Japanese. For example, use the language selector to select Brazilian Portuguese or Japanese to see the welcome message and list of colors.

    The example shows locales for Portuguese and Japanese.

Now it’s time to learn how it works.

Create the Language Properties File

Create a separate project module to hold all the shared language keys. In the example project, the shared keys are in the Acme U8T2 Impl module.

Create a Language.properties file and add it to the module’s src/main/resources/content folder. In the file define the keys you want to share with the other modules.

The example project has a list of six colors used by the Acme U8T2 Web module:

blue=Blue
green=Green
orange=Orange
purple=Purple
red=Red
yellow=Yellow 

Language property files for other locales can also be included in the folder. For example, to include language keys for Japanese, add a Language_ja.properties file to the folder.

Add the bnd Instruction

For each module that you want to share language keys with, you must specify the resource in the bnd header.

Bundle-Name: Acme U8T2 Web
Bundle-SymbolicName: com.acme.u8t2.web
Bundle-Version: 1.0.0
-liferay-aggregate-resource-bundles: com.acme.u8t2.impl

The example project has a web portlet that uses the color language keys from Acme U8T2 Impl. In the bnd.bnd file of the Acme U8T2 Web module, the resource bundle is specified:

-liferay-aggregate-resource-bundles: com.acme.u8t2.impl

Note that you can still choose to place some language keys in an individual module. For example, the welcome message in the example project comes from the Acme U8T2 Web module’s language keys and not the shared keys of Acme U8T2 Impl. Language keys of an individual module take priority over any shared keys specified by -liferay-aggregate-resource-bundles.