Language files contain translations of your application’s user interface messages. But you can also override the default language keys globally and in other applications (including your own). Here are the steps for overriding language keys:
- Determine the language keys to override
- Override the keys in a new language properties file
- Create a Resource Bundle service component
Determine the language keys to override
So how do you find global language keys? They’re in the
Language[xx_XX].properties
files in the source code or your bundle.
-
From the source:
/portal-impl/src/content/Language[xx_XX].properties
-
From a bundle:
portal-impl.jar
All language properties files contain properties you can override, like the language settings properties:
##
## Language settings
##
...
lang.user.name.field.names=prefix,first-name,middle-name,last-name,suffix
lang.user.name.prefix.values=Dr,Mr,Ms,Mrs
lang.user.name.required.field.names=last-name
lang.user.name.suffix.values=II,III,IV,Jr,Phd,Sr
...
There are also many simple keys you can override to update default messages and labels.
##
## Category titles
##
category.admin=Admin
category.alfresco=Alfresco
category.christianity=Christianity
category.cms=Content Management
...
For example, Figure 1 shows a button that uses Liferay’s publish
default
language key.
`publish=Publish`
Next, you’ll learn how to override this key.
Override the keys in a new language properties file
Once you know the keys to override, create a language properties file for the
locale you want (or the default Language.properties
file) in your module’s
src/main/resources/content
folder. In your file, define the keys your way. For
example, you could override the publish
key.
publish=Publish Override
To enable your change, you must create a resource bundle service component to reference your language file.
Create a Resource Bundle service component
In your module, create a class that extends java.util.ResourceBundle
for the
locale you’re overriding. Here’s an example resource bundle class for the
en_US
locale:
@Component(
property = { "language.id=en_US" },
service = ResourceBundle.class
)
public class MyEnUsResourceBundle 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);
}
The class’s _resourceBundle
field is assigned a ResourceBundle
. The call to
ResourceBundle.getBundle
needs two parameters. The content.Language_en_US
parameter is the language file’s qualified name with respect to the module’s
src/main/resources
folder. The second parameter is a control
that sets the
language syntax of the resource bundle. To use language syntax identical to
Liferay’s syntax, import Liferay’s
com.liferay.portal.kernel.language.UTF8Control
class and set the second
parameter to UTF8Control.INSTANCE
.
The class’s @Component
annotation declares it an OSGi ResourceBundle
service
component. It’s language.id
property designates it for the en_US
locale.
@Component(
property = { "language.id=en_US" },
service = ResourceBundle.class
)
The class overrides these methods:
-
handleGetObject
: Looks up the key in the module’s resource bundle (which is based on the module’s language properties file) and returns the key’s value as anObject
. -
getKeys
: Returns anEnumeration
of the resource bundle’s keys.
Your resource bundle service component redirects the default language keys to your module’s language key overrides.
Important: If your module uses language keys from another module and overrides any of that other module’s keys, make sure to use OSGi headers to specify the capabilities your module requires and provides. This lets you prioritize resource bundles from the modules.
To see your Liferay language key overrides in action, deploy your module and visit the portlets and pages that use the keys.
That’s all there is to overriding Liferay’s language keys.