Menu

Liferay 6.2: Multi-language problems

Multi-language
Vietnam version
In the wide world, if you want to introduce your application, your program have to multi-language. Fortunately, Liferay framework supported this feature and provided easiest ways to do. I will introduce how to using multi-language in a portlet. Let's go.


Firstly, I will show you where Liferay Framework stored language translations. Portal language translation stored in portal-impl.jar and this lib was saved at Liferay-portal/tomcat-x.y.zz/webapps/ROOT/WEB-INF/lib folder. This folder contained many jar files. In portal-impl.jar, double-click on the content folder. Now, you can see many *.properties file. Let's me explain the meaning of them, Language.properties is the default language file, it included a list of English translation. Language_xy.properties file is the translation of country which has the code is xy. For example, Vietnamese translation has been stored in Language_vi.properties file.
Secondly, the portlet language translation will be stored in your portlets. You can set up the place to save the files at creating time. 
Creating portlet and setup language translation
If you didn't tick on Create resource bundle file, you can do that by adding resource-bundle tag in portlet.xml file. For example, you add content.Language tag in portlet.xml file that mean your portlet translations saved in folder Project Name/docroot/src/content/Language_xx.properties. And now, you can create any translation for language and save at Project Name/docroot/src/content folder.

The format of translation in any *.properties file is origin-sentence=translation-sentence or origin-sentence-with-parameter-x=translation-sentence-with-parameter-{0}. In origin sentence only includes normal character and _ (underscore), . (dot), - (minus). In translation sentence contains any character and parameters symbol ({0}, {1}...). They are some example properties files.

After creating a properties file, I show you how to use your translation file.

Front-end

There are two parts what we need to consider. That is multi-language on *.jsp file and javascript.

On JSP file

In the properties file, you have some translation (see example). For example: student-code, student-name, student-birthday, student-gender, student-address, student-hello-x. So, how to show this translation on JSP file? Liferay Framework provided some easy ways.
  1. Using liferay-ui:message tag. This is the simplest way. If your translation without parameter, you only write <liferay-ui:message key="origin-sentence"></liferay-ui:message>. After Deploying your portlet and refreshing your portal, you can see translation sentence on your screen. If your sentence contains parameter(s), your can put a value of parameter(s) into arguments attribute.
  2. Using LanguageUtilUtil class: LanguageUtil had two useful methods. the get method is used in the no-parameter case and format method is applied in parameters case.
  3. Another tag: Liferay provided some tags which can translate the translations. This tags usually has label or message attribute. For example, will show the translation of the student code.

On javascript

AUI script is a strong javascript tool. In a web application, you always using javascript to done some tasks. Liferay Framework supplied a good tool to resolve some problems about multi-language. That is:AUI script is a strong javascript tool. In a web application, you always using javascript to done some tasks.
Liferay.Language.get(...);

This method will help you to get the translation base on your current language. If your translation has no parameter, you only call:
Liferay.Language.get('student-name');
This call will return a translated sentence for you. And if your translation has some parameters, you will call about function with two parameters as:
Liferay.Language.get('student-hello-x', ['Peter']);
As you see, the second parameter is an array. The number of elements in this array equals with the parameters in your translation.

Back-end

Generally, you don't often use multi-language in back-end part. You only implement algorithms, data structure, and integrated with a database. In special cases, using LanguageUtil class is the best choose. In my experience, you only using format and get method with the first parameter is an object of PortletConfig class. You can create an object of this class by easy way as:
PortletConfig portletConfig = (PortletConfig) renderRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);

Summary

There are many issues about multi-language in real applications. This post only helps you in some simple case. And the most important, you need setup your portal and your account language to get a good result.
Click here to download full source.
As you knew, my English not very well. If you have any feedback content of this post and my language, let me know at chingovan@gmail.com. Thanks!

2 nhận xét:

  1. Hi! If you're looking for an online tool to manage the translation of .properties files, you can check out the localization platform https://poeditor.com it's really useful for collaborative translation work.

    Trả lờiXóa