Liferay 7: Global Resource Bundle
Global Resource Bundle for Liferay 7
Overview
Liferay Resource Bundle is used for messages translation on the portal. Each module may have it’s own Resource Bundle. This article explains how to create a “global” one, with translations available for any other module, including the Liferay core modules.
Implementation
1. Create a module with the following structure:
2. Define dependencies in build.grade:
dependencies {
compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel"
compileOnly group: "com.liferay.portal", name: "com.liferay.util.taglib"
compileOnly group: "javax.portlet", name: "portlet-api"
compileOnly group: "javax.servlet", name: "javax.servlet-api"
compileOnly group: "jstl", name: "jstl"
compileOnly group: "org.osgi", name: "osgi.cmpn"
}
3. Create a bundle descriptor bnd.bdn:
Bundle-Name: LifeDev Global Resource Bundle
Bundle-SymbolicName: com.lifedev.global.resource.bundle
Bundle-Version: 1.0.0
4. Create Language_xx_XX.properties file in resources/content folder for each locale, which needs translation. Sample:
Language_en_US.properties:
hello-world=Hello, World
Language_de_DE.properties:
hello-world=Hallo, Welt
5. For each locale create a XxxResourceBundle class, sample:
EnglishResourceBundle:
package com.lifedev.global.resource.bundle;
import com.liferay.portal.kernel.language.UTF8Control;
import org.osgi.service.component.annotations.Component;
import java.util.Enumeration;
import java.util.ResourceBundle;
@Component(
property = {"language.id=en_US"},
service = ResourceBundle.class
)
public class EnglishResourceBundle extends ResourceBundle {
@Override
protected Object handleGetObject(String key) {
return _resourceBundle.getObject(key);
}
@Override
public Enumeration<String> getKeys() {
return _resourceBundle.getKeys();
}
private final ResourceBundle _resourceBundle = ResourceBundle.getBundle(
"content.Language_en_US", UTF8Control.INSTANCE);
}
GermanResourceBundle:
package com.lifedev.global.resource.bundle;
import com.liferay.portal.kernel.language.UTF8Control;
import org.osgi.service.component.annotations.Component;
import java.util.Enumeration;
import java.util.ResourceBundle;
@Component(
property = {"language.id=de_DE"},
service = ResourceBundle.class
)
public class GermanResourceBundle extends ResourceBundle {
@Override
protected Object handleGetObject(String key) {
return _resourceBundle.getObject(key);
}
@Override
public Enumeration<String> getKeys() {
return _resourceBundle.getKeys();
}
private final ResourceBundle _resourceBundle = ResourceBundle.getBundle(
"content.Language_de_DE", UTF8Control.INSTANCE);
}
6. Deploy the module.
7. Now you can use the message keys defined in this Global Resource Bundle in any other module, or you can override the Liferay default translations.
Enjoy 😏
Comments
Post a Comment