Liferay 国际化多个Portlet标题支持

Liferay 扩展一个只支持 单个Portlet标题国际化,如果有多个Portlet 需要实现标题国际化,可以如下操作:

修改 com.liferay.portlet.PortletConfigImpl中的 getResourceBundle 方法:

if (Validator.isNull(resourceBundleClassName)) {
			String resourceBundleId = _portlet.getPortletId();

			ResourceBundle resourceBundle = _resourceBundles.get(
				resourceBundleId);
			if ( resourceBundle != null && resourceBundle instanceof Utf8PropertyResourceBundle){
			    resourceBundle = Utf8PropertyResourceBundle.getResourceBundle(resourceBundleId , locale);
			}
			if (resourceBundle == null) {
				StringBundler sb = new StringBundler(16);

				try {
					PortletInfo portletInfo = _portlet.getPortletInfo();

					sb.append(JavaConstants.JAVAX_PORTLET_TITLE);
					sb.append(StringPool.EQUAL);
					sb.append(LanguageUtil.get(locale,portletInfo.getTitle()));
					sb.append(StringPool.NEW_LINE);

					sb.append(JavaConstants.JAVAX_PORTLET_SHORT_TITLE);
					sb.append(StringPool.EQUAL);
					sb.append(LanguageUtil.get(locale,portletInfo.getShortTitle()));
					sb.append(StringPool.NEW_LINE);

					sb.append(JavaConstants.JAVAX_PORTLET_KEYWORDS);
					sb.append(StringPool.EQUAL);
					sb.append(portletInfo.getKeywords());
					sb.append(StringPool.NEW_LINE);

					sb.append(JavaConstants.JAVAX_PORTLET_DESCRIPTION);
					sb.append(StringPool.EQUAL);
					sb.append(portletInfo.getDescription());
					sb.append(StringPool.NEW_LINE);

					resourceBundle = new Utf8PropertyResourceBundle(resourceBundleId,locale,
						new UnsyncByteArrayInputStream(
							sb.toString().getBytes(StringPool.UTF8)));
				}
				catch (Exception e) {
					_log.error(e, e);
				}

				_resourceBundles.put(resourceBundleId, resourceBundle);
			}

			return resourceBundle;
		}

Utf8PropertyResourceBundle 类如下:

public class Utf8PropertyResourceBundle extends PropertyResourceBundle {

	public Utf8PropertyResourceBundle(String resourceBundleId,Locale locale,InputStream inputStream)
		throws IOException {

		super(inputStream);
		resourceMap.put(resourceBundleId + ":" + locale.getLanguage(),this);
	}

	public Object handleGetObject(String key) {
	    
		String value = (String)super.handleGetObject(key);

		if (value == null) {
			return null;
			
		}

		try {
			return new String(
					value.getBytes(StringPool.ISO_8859_1), StringPool.UTF8);
		}
		catch (Exception e) {
			return null;
		}
	}
	public  static Utf8PropertyResourceBundle  getResourceBundle(String resourceBundleId, Locale locale){
	   return resourceMap.get(resourceBundleId + ":" + locale.getLanguage());
	}

	private static Map<String,Utf8PropertyResourceBundle> resourceMap = new HashMap<String,Utf8PropertyResourceBundle>();

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值