Add Custom Language Capability To Liferay

Introduction

Liferay supports out-of-the-box around 50 languages. It’s also possible to add our own language. This post is about how you can customize the Liferay portal to add your own language support. After the customization, you will be able to see your language in the Liferay Control Panel settings.

Overall Steps:

First, identify the ISO 639-1 Code language code
Then identify the ISO 3166-1 alpha-2 two-letter country code.
Update web.xml
Update shielded-container-web.xml
Update portal-ext.properties
Prepare our language properties
Create an OSGi Module that picks our custom language properties file
At last change the language of the portal to verify our changes.

Step 1: Identify the Language Code

The first step is to identify the language code. I am considering the Assamese language for the demo purpose. The Assamese language is spoken in the northeast part of India. So I am going to search language code for Assamese. For this visit the below link and get the ISO 639-1 Code. I found the code for the Assamese language which is as.

https://www.loc.gov/standards/iso639-2/php/code_list.php

Step 2: Get the Country Code

For the demo purpose I have considered the Assamese Language and since this language is spoken in India I need to find out the country code for India. For this visit the below link and get the ISO 3166-1 alpha-2 two-letter country code. For India, the country code is IN.

https://www.iso.org/obp/ui/#search


as_IN

After finding both the code for language and country the language code looks like as_IN. The first part is the language code and the second part is the country code. This combined code will be used in the multiple Liferay files.


Step 3: Update web.xml

Path : …\tomcat-9.0.53\webapps\ROOT\WEB-INF\web.xml

After the tag <web-resource-collection> add the below contents

<url-pattern>/as/c/portal/protected</url-pattern>
<url-pattern>/as-IN/c/portal/protected</url-pattern>
<url-pattern>/as_IN/c/portal/protected</url-pattern>

Step 4: Update shielded-container-web.xml

Path : …\tomcat-9.0.53\webapps\ROOT\WEB-INF\shielded-container-web.xml

Add the below lines in the servlet-mapping section

         <servlet-mapping>
		<servlet-name>I18n Servlet</servlet-name>
		<url-pattern>/as/*</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>I18n Servlet</servlet-name>
		<url-pattern>/as-IN/*</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>I18n Servlet</servlet-name>
		<url-pattern>/as_IN/*</url-pattern>
	</servlet-mapping>

Step 5: Update portal-ext.properties

Liferay needs to know about our new language. This can be done by two entries. Update the below two properties key in portal-ext.properties

  • locales
  • locales.enabled
locales property
locales.enabled property in portal-ext.properties
locales.enabled property

Restart Liferay

After doing all the changes restart Liferay.


Step 6: Create an OSGi module

How Liferay will know from which language properties it will pick the keys? That’s the reason an OSGi module is required for it where our language properties have to be added. I am going to create an OSGi module and create a file Language_as_IN.properties file inside the module

In your component class, you need to extend ResourceBundle class and mention the custom language properties file.

/**
 * @author Hamidul
 */
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=as_IN" }, 
		service = ResourceBundle.class
)
public class AssameseLanguageHook extends ResourceBundle {

	ResourceBundle bundle = ResourceBundle.getBundle("content.Language_as_IN", 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();
	}

}

Important Notes:

What language properties key are you going to keep inside the Language_as_IN.properties file? Most probably the entire language properties key that is used in the portal. How do you know those keys?

Unzip the jar in the path tomcat-9.0.53\webapps\ROOT\WEB-INF\shielded-container-lib\portal-impl.jar and locate the language keys. Pick the key and translate it using some online translator services. You can use Google Translate free of cost.

Deploy the OSGi module and log into Liferay for the final step.

Step 7: Change the portal language

After all the customization, now we are ready to test it. For that, we need to change the portal language to Assamese which we have added as part of the customization. If all the steps are done perfectly then the new language will be available to switch in the portal. We can change the portal language directly using the URL as well as Liferay’s out-of-the-box language portlet. For example,

http://localhost:8080/as_IN/web/guest/home can be used as a quick way to change the language of the portal. Give attention to the language code after the port number.

Also, you can use Liferay’s portlet.

Summary:

This customization is tested on Liferay 7.4. You will see many un-resolved language keys after doing all the changes. In your OSGi module, the language properties should have all the keys that Liferay uses. To find out existing language keys, remember an important point. Liferay resource bundle can be at portal level and module level. All the portal-level language keys are available in the portal-impl.jar file. Alternatively, you can browse the Liferay source code as well.

The module-level resource bundles reside in the individual modules. Get the Liferay source code and the modules are available inside the ${liferay-source-folder}/modules/apps folder. Refer to resource bundles for all the available apps.

Download Resources

About The Author

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top
%d