Adding Custom I18N Strings

From AlfrescoWiki

Jump to: navigation, search

Contents

[edit] Introduction

Alfresco 1.3 introduced a mechanism allowing you to include custom I18N strings. Previously webclient.properties would have to be changed and then re-merged when you upgraded.

A webclient.properties file can now be placed in the alfresco.extension package. This will be automatically picked up by the web client, except for messages displayed via JSF message tags directly in JSPs.


[edit] Using custom strings in JSPs

If you want to use a custom string in any of your JSPs the standard JSF mechanism has to be used.

Add the following line to the top of your custom JSP page:

<f:loadBundle basename="alfresco.extension.webclient" var="yourMsgs"/>

This will make your strings available via the yourMsgs variable. For example, if you have a custom string with a key of custom_string, you can output that string in your JSP by using the code below.

<h:outputText value="#{yourMsgs.custom_string}" />

There is an example showing this approach in the Customising An Alfresco JSP example.

[edit] Using custom strings in configuration & beans

When we refer to I18N strings in configuration files or programtically via the Application.getMessage() call the standard webclient.properties and your custom webclient.properties (if present) are loaded.

For example, if you have a custom string with a key of custom_string, you could use it as the label for a property as follows:

<config>
   <property-sheet>
      <show-property name="name" display-label-id="custom_string" />
   </property-sheet>
</config>

Or if you want to refer to the string in your beans, for example as the label of the finish button in a dialog:

public String getFinishButtonLabel()
{
   return Application.getMessage(FacesContext.getCurrentInstance(), "custom_string");
}


[edit] Defining custom Message properties files (from Alfresco Labs 3 onwards)

You basically have 2 different beans dealing with messages:

  1. org.alfresco.i18n.ResourceBundleBootstrapComponent
  2. This is the normal Alfresco one (loading system-messages, dictionary-messages etc). It is defined and configured inside:

    WEB-INF/classes/alfresco/core-services-context.xml

  3. org.alfresco.web.app.ResourceBundleBootstrap
  4. This is the one used by the Webclient, but it is not defined anywhere. It is used by org.alfresco.web.app.ResourceBundleWrapper, which serves as an entry point for getting Webclient messages.

So, if you want to use another message properties filed in the Webclient then webclient.properties, you need to define a bean using the org.alfresco.web.app.ResourceBundleBootstrap.

In order to do so, you can create a file :

WEB-INF/classes/alfresco/extension/custom-webclient-context.xml

contents:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>

<beans>

   <bean id="resourceBundlesWebApp" class="org.alfresco.web.app.ResourceBundleBootstrap"> 
      <property name="resourceBundles">
         <list>
             <value>alfresco.extension.custom-webclient</value>
         </list>
      </property>       
   </bean>
   
   
</beans>

And then put you're properties into:

WEB-INF/classes/alfresco/extension/custom-webclient.properties

If you add a line to log4j.properties, you should see you're resource bundle loaded in the logs :

log4j.logger.org.alfresco.web.app.ResourceBundleWrapper=debug


Inside a jsp file, you still need to reference it directly:

<f:loadBundle basename="alfresco.extension.custom-webclient" var="customMsg"/>