Localizing Your Application

Liferay’s localization framework helps you create and use localized messages in minutes. You create your messages in a default properties file called Language.properties and localize them in properties files that use the convention Language_xx.properties, where xx is the locale code. After deploying your app, the messages are available to your templates. Liferay’s JSP tags, such as <liferay-ui:message .../> display them in the user’s current locale automatically, without requiring you to access ResourceBundle or Locale objects explicitly. Here are the steps:

  1. Create a default language properties file called Language.properties in your project’s resource bundle folder.

    Project TypeResource Bundle Folder
    Bean Portletsrc/main/resources/content/
    JSF Portletsrc/main/resources/
    Liferay MVC Portletsrc/main/resources/content/
    PortletMVC4Spring Portletsrc/main/java/resources/content/
    Angular Widgetfeatures/localization/
    React Widgetfeatures/localization/
    Vue.js Widgetfeatures/localization/
  2. Specify your language properties (language keys) using key/value pairs. For example, create a friendly greeting.

    howdy-partner=Howdy, Partner!
    
  3. Configure your app’s resource bundle and the locales you’re supporting. The locales your Liferay DXP instance supports are specified in its portal properties file (here are Liferay DXP’s default locales. For example, these configurations support translations for English and Spanish locales:

    @PortletConfiguration class annotation: Can be used in Portlet 3.0 portlets such as Bean Portlets.

    @PortletConfiguration (
        ...
        resourceBundle="content.Language",
        supportedLocales = {"en", "es"}
    )
    

    Portlet descriptor portlet.xml: Can be used in any portlet WAR.

    <portlet>
    ...
    <supported-locale>en</supported-locale>
    <supported-locale>es</supported-locale>
    <resource-bundle>content.Language</resource-bundle>
    ...
    </portlet>
    

    @Component class annotation: Can be used in a portlet module such as a Liferay MVC Portlet.

    @Component (
        ...
        property = {
            ...
            "javax.portlet.supported-locale=en",
            "javax.portlet.supported-locale=es",
            "javax.portlet.resource-bundle=content.Language"
        }
    )
    
  4. Create language properties for a locale. For demonstration purposes, create one manually. Automatically generating translations is discussed later.

    For example, create a Spanish translation by copying Language.properties to Language_es.properties and translating the property values to Spanish.

    howdy-partner=Hola, Compañero!
    
  5. In your front-end template code, use the language property. For example, a JSP could use the howdy-partner property via the <liferay-ui:message /> tag.

    <%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %> 
    ...
    <liferay-ui:message key="howdy-partner" />
    ...
    
  6. Deploy your application and view it in different locales. For example, you could view the app locally in Spanish by specifying the es locale code in the URL (e.g., http://localhost:8080/es/...).

Congratulations on a great start to localizing your application!

Next, you can explore generating translations automatically or create a language module for using language keys across applications.

Automatically Generating Translations

Using Language Modules

Using Liferay DXP’s Language Settings

« Introduction to LocalizationUsing Liferay's Localization Settings »
この記事は役に立ちましたか?
0人中0人がこの記事が役に立ったと言っています