Liferay provides its ability to support multiple languages. It currently supports about 40 languages out of the box. In this blog we will see How to add a new language in Liferay 7. We will add “Gujarati” language in our example.
Environment Requirement
- JDK 8
- Eclipse
- Liferay Portal
1) Web.xml (“${LIFERAY_HOME}\tomcat\webapps\ROOT\WEB-INF”)
Add new language in Liferay, it requires a servlet-mapping entry in web.xml. So we are modifying Liferay Portal’s web.xml file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <servlet-mapping> <servlet-name>I18n Servlet</servlet-name> <url-pattern>/gu/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>I18n Servlet</servlet-name> <url-pattern>/gu_IN/*</url-pattern> </servlet-mapping> <security-constraint> <web-resource-collection> <web-resource-name>/c/portal/protected</web-resource-name> <url-pattern>/gu/c/portal/protected</url-pattern> <url-pattern>/gu_IN/c/portal/protected</url-pattern> </web-resource-collection> </security-constraint> |
This step will activate the “Gujarati” language in Liferay.
2) Portal-ext.properties
We have configured servlet-mapping entry. Now we need to configure ‘locales’ property in portal-ext.properties.
1 2 3 | locales=ar_SA,eu_ES,bg_BG,ca_AD,ca_ES,zh_CN,sl_SI,sk_SK,es_ES,sv_SE,tr_TR,uk_UA,vi_VN,gu_IN |
By configuring the above property “Gujarati” language will be available in the Liferay portal.
- Restart Liferay server
- Go to the Control Panel → Configuration → Instance Settings.
- Click on “Localization”.
- Move Gujarati language from available to current and click on save.
- Select Gujarati language as default language and click on save. Now our LIferay Portal’s default language is Gujarati.
3) Create resourceBundle module
Resource bundle module provides a way to add custom language key/value in Liferay Portal. You can create a resource bundle module for an existing language or custom language that you have added. Here we will create a resource bundle for ‘Gujarati’ language that we have added into the portal.
- Create module project and create class which extends ResourceBundle. Please take a look at the code below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | @Component( immediate = true, property = { "language.id=gu_IN" }, service = ResourceBundle.class ) public class GuInResourceBundle extends ResourceBundle { private final ResourceBundle _resourceBundle = ResourceBundle.getBundle( "content.language_gu_IN", UTF8Control.INSTANCE); @Override public Enumeration<String> getKeys() { return _resourceBundle.getKeys(); } @Override protected Object handleGetObject(String key) { return _resourceBundle.getObject(key); } } |
- Create “content” folder in “src/main/resources”.
- Create “language_gu_IN.properties” file in the content folder.
4) Add below language key/value in “language_gu_IN.properties”. Here we are adding gujarati translations for sign in portlet.
1 2 3 4 5 6 7 8 9 10 11 12 13 | password =\u0AAA\u0ABE\u0AB8\u0AB5\u0AB0\u0ACD\u0AA1 email-address=\u0A88 - \u0AAE\u0AC7\u0A88\u0AB2 \u0AB8\u0AB0\u0AA8\u0ABE\u0AAE\u0AC1\u0A82 remember-me =\u0AAE\u0AA8\u0AC7 \u0AAF\u0ABE\u0AA6 \u0AB0\u0ABE\u0A96\u0ACB sign-in =\u0AB8\u0ABE\u0A87\u0AA8 \u0A87\u0AA8 create-account =\u0A96\u0ABE\u0AA4\u0AC1\u0A82 \u0AAC\u0AA8\u0ABE\u0AB5\u0ACB forgot-password =\u0AAA\u0ABE\u0AB8\u0AB5\u0AB0\u0ACD\u0AA1 \u0AAD\u0AC2\u0AB2\u0AC0 \u0A97\u0AAF\u0ABE\u0A82 \u0A9B\u0ACB? |
Module project structure should like below image :
You have one of the greatest internet websites.
Wow, gorgeous portal. Thnx …
Thanks, this website is really helpful.