Tribune

Home » Liferay Portal » English » 6. Portal Framework

Vista Combinata Vista Piatta Vista ad Albero
Discussioni [ Precedente | Successivo ]
toggle
Vitaliy Koshelenko
Custom Resorce Bundle issue
7 aprile 2012 4.23
Risposta

Vitaliy Koshelenko

Punteggio: Regular Member

Messaggi: 225

Data di Iscrizione: 25 marzo 2011

Messaggi recenti

Hi, everyone.

I'm using database table for retrieving language resources (so, when following statement is processed: <liferay-ui:message key="save"/> , the value for key 'save' is taken not from Language.properties, but from database table (with 'key', 'value', and 'locale' columns) ) with the possibility of editing these resources, import/export etc.
Also when any new portlet is deployed - the resource bundle from it should be also saved to database. For this purposes I use custom HotDeployListener (the code is below, some trivial methods are excluded). The problem is in that fact, that resource bundles can be placed on any path withing portlet and can be named in different way (not only Language_**.properties). The path to these resources can be specified in portlet.xml file (<resource-bundle>), or in liferay-hook.xml (if hook is used - in <language-properties>), or may not be specified in any of them. So, system tries to read path to bundles firstly from portlet.xml, then from liferay-hook.xml, and if path is not specified - uses "default" path - 'Language.properties'. Can anyone suggest me a more universal approach (and less hard-coded emoticon ) of reading resource bundles when portlet is deployed (maybe, path to resources can be retrieved from hotDeployEvent in some other way)... I'll appreciate any help.

public class HotDeployListenerHook implements HotDeployListener {
public static final String DEFAULT_RESOURCE_NAME = "Language.properties";
@Override
public void invokeDeploy(HotDeployEvent hotDeployEvent) throws HotDeployException {

ServletContext servletContext = hotDeployEvent.getServletContext();

ClassLoader contextClassLoader = hotDeployEvent.getContextClassLoader();

String bundleName = hotDeployEvent.getPluginPackage().getName();

// determines whether <resource-bundle> specified in 'portlet.xml' or <language-properties> specified in 'liferay-hook.xml'
boolean resourceBundleSpecified = false;

String portletXML = StringUtils.EMPTY;
try {
portletXML = HttpUtil.URLtoString(servletContext.getResource("/WEB-INF/" + Portal.PORTLET_XML_FILE_NAME_STANDARD));
} catch (IOException e) {
_logger.error("Can not read portlet.xml");
}

if (StringUtils.isNotBlank(portletXML)) {
/*== try to read resource bundle from portlet.xml ===*/
try {
resourceBundleSpecified = readPortletXML(portletXML, contextClassLoader, bundleName);
} catch (DocumentException e) {
if (_logger.isDebugEnabled()) {
_logger.debug("Unable to read process xml. ");
}
}
}

// if <resource-bundle> not specified in portlet.xml - read from liferay-hook.xml
if (!resourceBundleSpecified) {
String liferayHookXml = StringUtils.EMPTY;
try {
liferayHookXml = HttpUtil.URLtoString(servletContext.getResource(LIFERAY_HOOK_PATH));
} catch (IOException e) {
_logger.error("Can not read liferay-hook.xml");
}

if (StringUtils.isNotBlank(liferayHookXml)) {

/*== try to read resource bundle from liferay-hook.xml ===*/
try {
resourceBundleSpecified = readLiferayHookXML(liferayHookXml, contextClassLoader, bundleName);
} catch (DocumentException e) {
if (_logger.isDebugEnabled()) {
_logger.debug("Unable to read process xml. ");
}
}
}
}

// if <resource-bundle> not specified - use default
if (!resourceBundleSpecified) {
processBundle(DEFAULT_RESOURCE_NAME, contextClassLoader, bundleName);
}
}
}


Regards, Vitaliy Koshelenko
David H Nebinger
RE: Custom Resorce Bundle issue
7 aprile 2012 9.15
Risposta

David H Nebinger

Punteggio: Liferay Legend

Messaggi: 7852

Data di Iscrizione: 1 settembre 2006

Messaggi recenti

Hmm, you're also missing any message bundles used by Spring... Those can be defined in Spring's context xml files. I think JSF has another method for handling message bundles (would have to check).

Basically I don't think you'll find anything that's universal. Many frameworks support using message bundles for localization, yet often times define their own methodology for where they go and how they're loaded.

If you're planning this hook for an internal thing only, you probably have the control to tell your folks to "always do language bundles like this".

If you're planning it for a contribution, you can tell folks that language bundles handled in this list of ways will work, but you'd be on your own for other mechanisms...