Liferay集成外部Portlet

     最近想好好玩Liferay,遇到问题还真是不少,之前一直在ext环境中做开发,但是现在发现开发Liferay的兄弟们太卖命了,几天不注意就来个小升级,我实在是没法跟上,想尝尝鲜可是我的ext中的东西不好更新啊,苦恼,困惑。
这些天想着不能用这个方式开发了,应该采用与Liferay松耦合的开发方式。Liferay提供了一个Plugins的SDK,倒是挺好,省掉一些配置工作,但是还是和Liferay有一腿。今天在LiferayPedia上看到这个文章,摘下来,没有仔细读,等把手头的方案写完得抽空好好看看。

Localization of Portlets Outside of Liferay

From LiferayPedia

Retrieved from "http://wiki.liferay.com/index.php/Localization_of_Portlets_Outside_of_Liferay"

Jump to: navigation, search

Categories Development

This wiki will explain how to localize non-Lifray dependent portlets outside of Liferay. You can use this method regardless of what framework you are using to build your portlets.

Some benefits of using resource bundles is efficiency in maintaining message bundles and the ability to localize or internationalize your messages from a centralized location. The centralized location is actually in your Language.properties files.

Localizing your portlets will require these steps:

Contents

[hide]

[edit]

Create Message Bundles

Language files are .properties files.

For example:

hello-world=Hello World
title-error=Please Enter A Valid Title Name

These files must be located in the classpath accessible by your portlet. A common location is within the portlet's WEB-INF/classes folder. The default message bundle is Language.properties, but can have any name, provided it is fully qualified (see Step 2 for details on this).

e.g.

WEB-INF/classes/com/my/portlets/p1/Resource.properties

Your foreign language files will be named with the following syntax:

Language_*.properties

So if you were creating a Spanish message bundle, you would name it Language_es.properties. Sometimes a language will have different dialects from different regions. For example Chinese, you could have Chinese from China or Taiwan. In this case you would name your files Language_zh_CN.properties and Language_zh_TW.properties.

Had you decided to use a different name for your bundles, the above would be:

WEB-INF/classes/com/my/portlets/p1/Resource.properties
WEB-INF/classes/com/my/portlets/p1/Resource_es.properties
WEB-INF/classes/com/my/portlets/p1/Resource_zh_CN.properties
WEB-INF/classes/com/my/portlets/p1/Resource_zh_TW.properties

[edit]

Add More Locales

The language and country code combinations, or "locales" are found in portal.properties (Languages and Time Zones section). If you want to add more locales, you would need to look up the proper codes. Here are good places to start: http://ftp.ics.uci.edu/pub/ietf/http/related/iso639.txt http://userpage.chemie.fu-berlin.de/diverse/doc/ISO_3166.html

[edit]

Non-Unicode characters

Java can only read Unicode characters or latin1 characters so what do you do when you have non-unicode characters such as Arabic or Japanese ? One way of dealing with this is to write a Language_*.properties.native file that uses the native language in the value. Afterwards, convert the native file using a native to ascii converter. A popular one is ascii2converter. http://java.sun.com/j2se/1.5.0/docs/tooldocs/solaris/native2ascii.html

[edit]

Configuring your portlets to load their language bundles

In your portlet.xml, define your <resource-bundle> as you created it above.

e.g.

<resource-bundle>com.my.portlets.p1.Resource</resource-bundle>

Also in portlet.xml, define a <supported-locale> for each language you want to support. Example for Spanish and Chinese from China or Taiwan:

<supported-locale>es</supported-locale>
<supported-locale>zh_CN</supported-locale>
<supported-locale>zh_TW</supported-locale>

The end result for several portlets could be something like:

 <portlet-app ...>
   <portlet>
     <portlet-name>p1</portlet-name>
     ...
     <supported-locale>es</supported-locale>
     <supported-locale>zh_CN</supported-locale>
     <supported-locale>zh_TW</supported-locale>
     <resource-bundle>com.my.portlets.p1.Resource</resource-bundle>
     ...
   </portlet>
   <portlet>
     <portlet-name>p2</portlet-name>
     ...
     <supported-locale>es</supported-locale>
     <supported-locale>zh_CN</supported-locale>
     <supported-locale>zh_TW</supported-locale>
     <resource-bundle>com.my.portlets.p2.Resource</resource-bundle>
     ...
   </portlet>
   ...
   <portlet>
     <portlet-name>pN</portlet-name>
     ...
     <supported-locale>es</supported-locale>
     <supported-locale>zh_CN</supported-locale>
     <supported-locale>zh_TW</supported-locale>
     <resource-bundle>com.my.portlets.pN.Resource</resource-bundle>
     ...
   </portlet>
 </portlet-app>

[edit]

Loading your language bundles in your pages

[edit]

Portlet Standard Way

The standard way of loading your language bundles in portlets is by accessing a couple of objects in the portlet context:

<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<%@ page import="java.util.Locale" %>
<%@ page import="java.util.ResourceBundle" %>
<portlet:defineObjects />
<%
// renderRequest and portletConfig are objects which, accoring to the
// portlet spec, are required to be in context. So they should just be 
// there for you to use.
Locale locale = renderRequest.getLocale();
ResourceBundle res = portletConfig.getResourceBundle(locale);
%>

This method doesn't require you to know the actual name of the bundle. The portlet container should take care of setting that up for you (provided you configured your portlet.xml properly)

[edit]

Utilize java.util.ResourceBundle

We can use the ResourceBundle to select which Language.properties files to use. The ResouceBundle requires two parameters to be passed including the locale codes; you could use the Locale class to retrieve the current locale.

Import these classes in your jsp:

<%@ page import="java.util.ResourceBundle" %>
<%@ page import="java.util.Locale" %>

Also include the following in your jsp:

<% 
 Locale locale = (Locale)request.getSession().getAttribute("org.apache.struts.action.LOCALE");
 String language = locale.getLanguage();
 String country = locale.getCountry();
 // Remember, "Language" is the fully qualified name of a bundle in the classpath
 //
 // e.g.    WEB-INF/classes/Language_*.properties
 ResourceBundle res = ResourceBundle.getBundle("Language", new Locale(language, country));
%>

In the above snippet you can see that we request a Locale object, which we then use to get the Language and Country codes. Those codes are then passed in as parameters in ResourceBundle.getBundle(String, new Locale(String, String)) to identify which bundle to use.

[edit]

Localize Messages

Use your messages with the following syntax:

res.getString("message-key")

If you have a hello world button, instead of making the button

<input type="button" value="Hello World" />

make it

<input type="button" value="<%= res.getString("hello-world")" %>" />

Now you are set to use your localized/internationalized messages. When you change the Locale via the Language portlet, your localization should appear!

[edit]

Titles and Category

[edit]

Portlet Title

There are three standard keys defined specifically for the localization of portlet info, including the title.

javax.portlet.title=My Portlet
javax.portlet.short-title=MyPortlet
javax.portlet.keywords=My,Portlet

Include these in your language bundles to localize those values and the portlet titles, displayed throughtout the portal (including the "Add Content" menu), will be localized.

[edit]

(*4.3) Portlet Category/"Add Content" Menu

Suppose you create a new portlet category in which to group your portlets, and you configure the liferay-display.xml as such;

e.g.

 <display>
   <category name="my.new.category">
     <category name="my.new.sub-category">
       <portlet id="portlet_A" />
       <portlet id="portlet_B" />
     </category>
     <portlet id="portlet_C" />
     <portlet id="portlet_D" />
   </category>
 </display>

All you need to do to have the appropriate name appear in the "Add Content" menu of the portal is to add the keys to your localization files. For the above two categories, include the following in your language bundles:

my.new.category=This is My Category
my.new.sub-category=My Sub-Category

That's it.

Note: If for example you have the four portlets above defined in your portlet.xml, and each one has it's own language bundle, the first bundle found to contain the keys will be used. So to be safe, just copy the category keys to each bundle.

[edit]

Struts

If using struts, in struts-config.xml, define the <message-resource>. If your files are in WEB-INF/classes/abc/123/Language.properties you would do this:

<message-resources parameter="abc.123.Language" />

[edit]

Note: One WAR with multiple portlets

Remember, if you have more than one portlet in your war, they won't be able to share the same resource bundle. What does this mean? It means you can't do this (as is done in the portal war):

javax.portlet.title.xxxx=TEST

You can only do:

javax.portlet.title=TEST

Use the method suggested above to separate your portlet resource bundles.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值