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>();
}