`
kevin.hwq
  • 浏览: 61341 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

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.

评论
1 楼 010lead 2008-08-06  
你好黄,关于Liferay问题,可以邮件 liferaychina@gmail.com

相关推荐

    liferay portlet 开发实例

    Liferay Portlet开发不仅涉及编码技术,还涵盖了从数据模型设计、服务接口定义、业务逻辑实现到集成部署的完整流程。通过深入理解这些核心概念和实践,开发者可以更加高效地构建出符合企业需求的定制化应用,充分...

    Liferay portlet 工程示例代码

    Liferay Portal是一款开源的企业级门户系统,它允许用户创建、管理和集成各种Web应用程序,而portlet就是这些应用的基本构建块。下面将详细阐述Liferay Portlet开发的相关知识点。 1. **Liferay Portal简介** ...

    liferay6.2开发指南.pdf

    Liferay 6.2 是一个开源的企业级门户平台,它提供了丰富的功能和高度可扩展性,适合构建企业内部或外部的协作、信息共享和业务应用集成的网站。本开发指南将帮助开发者深入理解 Liferay 的核心概念和开发流程,以便...

    portlet规范介绍

    此外,Portlet的灵活性使其能够适应多样化的业务需求,无论是集成外部数据源、提供实时更新还是支持多语言环境,都能够得心应手。 综上所述,Portlet规范不仅为门户应用提供了标准化的构建方案,还促进了组件间的...

    Liferay开发文档整理.pdf

    Liferay 是一个功能强大的企业级门户解决方案,提供了一个完整的平台,用于构建企业内部网(intranet)、企业外部网(extranet)和Web网站。其核心功能包括内容管理、用户管理和企业级应用程序集成等。Liferay提供一...

    在liferay中整合GWT,附例子

    Liferay是一款开放源码的企业级Portal平台,支持内容管理、工作流、社交网络等功能,广泛用于构建企业内部或外部的协作平台。通过Liferay的插件架构,可以轻松地扩展其功能,包括集成GWT应用。 以下是整合GWT到...

    liferay教程

    Liferay是一款开源的企业级门户平台,它提供了丰富的功能和高度可定制性,广泛应用于构建企业内部和外部的网站、社区以及工作流程系统。本教程专为初学者设计,旨在帮助新接触Liferay的用户快速理解和掌握这个强大的...

    liferay5.1.2文档(完)

    它通常涵盖创建站点、添加页面、部署portlet、自定义主题、集成外部服务等内容,让新用户能在短时间内上手并开始使用Liferay。 再者,`liferay_4_portlet_development_guide.pdf`是Liferay 4.x Portlet开发指南,...

    liferay经典书籍8本

    内容包括内容管理系统、移动支持、API设计以及与外部系统的集成,让读者了解如何充分利用Liferay DXP的潜力。 7. 《Liferay Portlet Development》 portlet是Liferay中核心的组件形式。本书深入浅出地介绍了portlet...

    liferay学习资源

    Liferay 是一个开源的企业级门户平台,用于构建和管理企业内部或外部的Web应用程序和网站。在Liferay中,Struts Portlet是一种常见的开发模式,它结合了Liferay的portlet功能与Struts框架的MVC设计模式。Struts ...

    liferay-webservice

    总结来说,Liferay Webservice提供了一种强大的方式,将portlet的功能暴露给外部系统,实现数据的交换和集成。通过遵循上述步骤,开发者可以轻松地创建、部署并测试自己的Liferay服务,从而扩展Liferay Portal的功能...

    liferay的那些东西

    Liferay允许用户和portlet创建内部和外部链接,以方便访问其他页面或资源。了解如何正确设置和管理这些链接对于提供良好的用户体验至关重要。 3. **[portlet-name].portlet.pdf**:这可能是一个具体的portlet开发...

    liferay6.1开发学习

    【Liferay 6.1 开发学习】 Liferay 是一款开源的企业级门户平台...同时,动手实践是非常重要的,可以通过创建简单的portlet、主题和集成外部系统来加深理解。在遇到问题时,积极寻求社区支持,可以快速提升技能水平。

    LIFERAY的ppt课件

    Liferay是一款开源的企业级门户平台,它提供了强大的内容管理、协作和社交功能,被广泛用于构建企业内部或外部的Web应用程序。Liferay的核心特性包括网站构建、内容管理、工作流程、社区参与以及与其他系统的集成。...

    LIferay6开发书籍

    这本书详细介绍了如何利用Liferay Portal构建企业级的应用,包括但不限于创建自定义portlet、主题和布局,以及如何集成外部系统如数据库、企业服务总线(ESB)等。开发者将学习到如何利用Liferay的服务架构来开发可...

    liferay 文档与例子

    Liferay 是一个开源的企业级门户平台,广泛应用于构建企业内部或外部的Web应用程序和服务。它以其灵活、可扩展和社区支持而闻名。本资源“liferay 文档与例子”提供了关于Liferay Portal的详细文档和实例,对于学习...

    liferay帮助文档

    - **liferay-portal-tomcat-6.2.0-ce-ga1-20131101192857659.zip**: 这是Liferay的基础安装包,包含了Liferay Portal与Apache Tomcat服务器的集成。 - **cas**: 单点登录(Single Sign-On, SSO)的工程文件,用于...

    sampleIvyPortlet:与 Liferay 插件 SDK 集成的 ivy portlet 存根示例

    "Ivy Portlet" 指的是一个使用 Ivy 进行依赖管理的 Liferay 平台上的portlet应用。"存根示例"表明这是一个基础模板或起始点,用于展示如何在实际开发中设置和使用 Ivy。 **描述详解:** 描述进一步明确了 ...

    Liferay开发指南(中文)和liferay详细介绍(英文)

    2. **portlet开发**:学习如何创建和部署Portlets,这是Liferay中的基本应用程序单元,可以集成各种Web内容和服务。 3. **主题和布局**:掌握如何自定义Liferay的外观,包括创建和修改主题以及调整页面布局,以满足...

Global site tag (gtag.js) - Google Analytics