- Extended GlobalizedMessage: It is now possible to pass a custom ResourceBundle.Control instance to a
GlobalizedMessage for controlling the loading of responsible ResourceBundle - Created RelationAttributeResourceBundle: A custom ResourceBundle.Control implementation which uses a RelationAttributeCollection as data source for creating a ListResourceBundle. This allows it to This allows it to use GlobalizedMessage when showing RelationAttribute values in Bebop Controls - Altered GenericOrganizationalUnitPersonAddForm to use GlobalizedMessage and RelationAttributeResourceControl git-svn-id: https://svn.libreccm.org/ccm/trunk@2746 8810af33-2d31-482b-a856-94f89814c4dfmaster
parent
1af42506ca
commit
fad219c1fe
|
|
@ -0,0 +1,46 @@
|
||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.arsdigita.cms;
|
||||||
|
|
||||||
|
import java.util.ListResourceBundle;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.ResourceBundle;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Jens Pelzetter <jens.pelzetter@scientificcms.org>
|
||||||
|
*/
|
||||||
|
public class RelationAttributeResourceBundleControl extends ResourceBundle.Control {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ResourceBundle newBundle(final String baseName,
|
||||||
|
final Locale locale,
|
||||||
|
final String format,
|
||||||
|
final ClassLoader classLoader,
|
||||||
|
final boolean reload) {
|
||||||
|
final RelationAttributeCollection values = new RelationAttributeCollection(baseName);
|
||||||
|
values.addLanguageFilter(locale.getLanguage());
|
||||||
|
|
||||||
|
final Object[][] contents = new Object[(int)values.size()][2];
|
||||||
|
int i = 0;
|
||||||
|
while(values.next()) {
|
||||||
|
contents[i][0] = values.getKey();
|
||||||
|
contents[i][1] = values.getName();
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ListResourceBundle() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Object[][] getContents() {
|
||||||
|
return contents;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -38,6 +38,7 @@ import com.arsdigita.cms.ContentType;
|
||||||
import com.arsdigita.cms.ItemSelectionModel;
|
import com.arsdigita.cms.ItemSelectionModel;
|
||||||
import com.arsdigita.cms.RelationAttribute;
|
import com.arsdigita.cms.RelationAttribute;
|
||||||
import com.arsdigita.cms.RelationAttributeCollection;
|
import com.arsdigita.cms.RelationAttributeCollection;
|
||||||
|
import com.arsdigita.cms.RelationAttributeResourceBundleControl;
|
||||||
import com.arsdigita.cms.contenttypes.GenericOrganizationalUnit;
|
import com.arsdigita.cms.contenttypes.GenericOrganizationalUnit;
|
||||||
import com.arsdigita.cms.contenttypes.GenericOrganizationalUnitPersonCollection;
|
import com.arsdigita.cms.contenttypes.GenericOrganizationalUnitPersonCollection;
|
||||||
import com.arsdigita.cms.contenttypes.GenericPerson;
|
import com.arsdigita.cms.contenttypes.GenericPerson;
|
||||||
|
|
@ -45,6 +46,7 @@ import com.arsdigita.cms.contenttypes.util.ContenttypesGlobalizationUtil;
|
||||||
import com.arsdigita.cms.ui.ItemSearchWidget;
|
import com.arsdigita.cms.ui.ItemSearchWidget;
|
||||||
import com.arsdigita.cms.ui.authoring.BasicItemForm;
|
import com.arsdigita.cms.ui.authoring.BasicItemForm;
|
||||||
import com.arsdigita.globalization.GlobalizationHelper;
|
import com.arsdigita.globalization.GlobalizationHelper;
|
||||||
|
import com.arsdigita.globalization.GlobalizedMessage;
|
||||||
import com.arsdigita.util.UncheckedWrapperException;
|
import com.arsdigita.util.UncheckedWrapperException;
|
||||||
import java.util.TooManyListenersException;
|
import java.util.TooManyListenersException;
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
|
|
@ -116,7 +118,12 @@ public class GenericOrganizationalUnitPersonAddForm
|
||||||
while (roles.next()) {
|
while (roles.next()) {
|
||||||
RelationAttribute role;
|
RelationAttribute role;
|
||||||
role = roles.getRelationAttribute();
|
role = roles.getRelationAttribute();
|
||||||
target.addOption(new Option(role.getKey(), role.getName()));
|
//target.addOption(new Option(role.getKey(), role.getName()));
|
||||||
|
target.addOption(new Option(role.getKey(),
|
||||||
|
new Label(new GlobalizedMessage(
|
||||||
|
role.getKey(),
|
||||||
|
getRoleAttributeName(),
|
||||||
|
new RelationAttributeResourceBundleControl()))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -148,7 +155,11 @@ public class GenericOrganizationalUnitPersonAddForm
|
||||||
while (statusColl.next()) {
|
while (statusColl.next()) {
|
||||||
RelationAttribute status;
|
RelationAttribute status;
|
||||||
status = statusColl.getRelationAttribute();
|
status = statusColl.getRelationAttribute();
|
||||||
target.addOption(new Option(status.getKey(), status.getName()));
|
//target.addOption(new Option(status.getKey(), status.getName()));
|
||||||
|
target.addOption(new Option(status.getKey(),
|
||||||
|
new Label(new GlobalizedMessage(status.getKey(),
|
||||||
|
getStatusAttributeName(),
|
||||||
|
new RelationAttributeResourceBundleControl()))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,13 +27,12 @@ import org.apache.log4j.Logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* Represents a key into a ResourceBundle, a target ResourceBundle, and
|
* Represents a key into a ResourceBundle, a target ResourceBundle, and possibly an array of
|
||||||
* possibly an array of arguments to interpolate into the retrieved message
|
* arguments to interpolate into the retrieved message using the MessageFormat class.
|
||||||
* using the MessageFormat class.
|
|
||||||
* </p>
|
* </p>
|
||||||
* <p>
|
* <p>
|
||||||
* This class should be used in any situation where the application needs to
|
* This class should be used in any situation where the application needs to output localizeable
|
||||||
* output localizeable objects.
|
* objects.
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @see java.text.MessageFormat
|
* @see java.text.MessageFormat
|
||||||
|
|
@ -44,22 +43,28 @@ import org.apache.log4j.Logger;
|
||||||
*/
|
*/
|
||||||
public class GlobalizedMessage {
|
public class GlobalizedMessage {
|
||||||
|
|
||||||
/** Internal logger instance to faciliate debugging. Enable logging output
|
/**
|
||||||
* by editing /WEB-INF/conf/log4j.properties int hte runtime environment
|
* Internal logger instance to faciliate debugging. Enable logging output by editing
|
||||||
* and set com.arsdigita.globalization.GlobalizedMessage=DEBUG
|
* /WEB-INF/conf/log4j.properties int hte runtime environment and set
|
||||||
* by uncommenting or adding the line. */
|
* com.arsdigita.globalization.GlobalizedMessage=DEBUG by uncommenting or adding the line.
|
||||||
private static final Logger LOGGER = Logger.getLogger(GlobalizedMessage
|
*/
|
||||||
.class.getName());
|
private static final Logger LOGGER = Logger.getLogger(GlobalizedMessage.class.getName());
|
||||||
private String m_key = "";
|
private String m_key = "";
|
||||||
private String m_bundleName = "";
|
private String m_bundleName = "";
|
||||||
|
/**
|
||||||
|
* {@link ResourceBundle.Control} used by this {@code GlobalizedMessage} for looking up
|
||||||
|
* the ResourceBundle. Defaults to {@link ResourceBundle.Control#getNoFallbackControl(java.util.List)}
|
||||||
|
* to avoid that the locale of the server is taken into account.
|
||||||
|
*/
|
||||||
|
private ResourceBundle.Control rbControl = ResourceBundle.Control.getNoFallbackControl(
|
||||||
|
ResourceBundle.Control.FORMAT_DEFAULT);
|
||||||
private Object[] m_args = null;
|
private Object[] m_args = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* Constructor. Takes in a key to be used to look up a message in the
|
* Constructor. Takes in a key to be used to look up a message in the ResourceBundle for the
|
||||||
* ResourceBundle for the current running application. The base name of
|
* current running application. The base name of the ResourceBundle to do the lookup in is
|
||||||
* the ResourceBundle to do the lookup in is retrieved from the
|
* retrieved from the ApplicationContext.
|
||||||
* ApplicationContext.
|
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @param key The key to use to look up a message in the ResourceBundle.
|
* @param key The key to use to look up a message in the ResourceBundle.
|
||||||
|
|
@ -71,8 +76,7 @@ public class GlobalizedMessage {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* Constructor. Takes in a key to be used to look up a message in the
|
* Constructor. Takes in a key to be used to look up a message in the ResourceBundle specified.
|
||||||
* ResourceBundle specified.
|
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @param key The key to use to look up a message in the ResourceBundle.
|
* @param key The key to use to look up a message in the ResourceBundle.
|
||||||
|
|
@ -85,16 +89,14 @@ public class GlobalizedMessage {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* Constructor. Takes in a key to be used to look up a message in the
|
* Constructor. Takes in a key to be used to look up a message in the ResourceBundle for the
|
||||||
* ResourceBundle for the current running application. The base name of
|
* current running application. The base name of the ResourceBundle to do the lookup in is
|
||||||
* the ResourceBundle to do the lookup in is retrieved from the
|
* retrieved from the ApplicationContext. Also takes in an Object[] of arguments to interpolate
|
||||||
* ApplicationContext. Also takes in an Object[] of arguments to
|
* into the retrieved message using the MessageFormat class.
|
||||||
* interpolate into the retrieved message using the MessageFormat class.
|
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @param key The key to use to look up a message in the ResourceBundle.
|
* @param key The key to use to look up a message in the ResourceBundle.
|
||||||
* @param args An Object[] of arguments to interpolate into the retrieved
|
* @param args An Object[] of arguments to interpolate into the retrieved message.
|
||||||
* message.
|
|
||||||
*/
|
*/
|
||||||
public GlobalizedMessage(final String key, final Object[] args) {
|
public GlobalizedMessage(final String key, final Object[] args) {
|
||||||
this(key);
|
this(key);
|
||||||
|
|
@ -103,15 +105,14 @@ public class GlobalizedMessage {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* Constructor. Takes in a key to be used to look up a message in the
|
* Constructor. Takes in a key to be used to look up a message in the ResourceBundle specified.
|
||||||
* ResourceBundle specified. Also takes in an Object[] of arguments to
|
* Also takes in an Object[] of arguments to interpolate into the retrieved message using the
|
||||||
* interpolate into the retrieved message using the MessageFormat class.
|
* MessageFormat class.
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @param key The key to use to look up a message in the ResourceBundle.
|
* @param key The key to use to look up a message in the ResourceBundle.
|
||||||
* @param bundleName The base name of the target ResourceBundle.
|
* @param bundleName The base name of the target ResourceBundle.
|
||||||
* @param args An Object[] of arguments to interpolate into the retrieved
|
* @param args An Object[] of arguments to interpolate into the retrieved message.
|
||||||
* message.
|
|
||||||
*/
|
*/
|
||||||
public GlobalizedMessage(final String key,
|
public GlobalizedMessage(final String key,
|
||||||
final String bundleName,
|
final String bundleName,
|
||||||
|
|
@ -120,6 +121,34 @@ public class GlobalizedMessage {
|
||||||
setArgs(args);
|
setArgs(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public GlobalizedMessage(final String key,
|
||||||
|
final ResourceBundle.Control rbControl) {
|
||||||
|
this(key);
|
||||||
|
this.rbControl = rbControl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public GlobalizedMessage(final String key,
|
||||||
|
final Object[] args,
|
||||||
|
final ResourceBundle.Control rbControl) {
|
||||||
|
this(key, args);
|
||||||
|
this.rbControl = rbControl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public GlobalizedMessage(final String key,
|
||||||
|
final String bundleName,
|
||||||
|
final ResourceBundle.Control rbControl) {
|
||||||
|
this(key, bundleName);
|
||||||
|
this.rbControl = rbControl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public GlobalizedMessage(final String key,
|
||||||
|
final String bundleName,
|
||||||
|
final Object[] args,
|
||||||
|
final ResourceBundle.Control rbControl) {
|
||||||
|
this(key, bundleName, args);
|
||||||
|
this.rbControl = rbControl;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* Get the key for this GlobalizedMessage.
|
* Get the key for this GlobalizedMessage.
|
||||||
|
|
@ -166,20 +195,17 @@ public class GlobalizedMessage {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* Localize this message. If no message is found the key is returned as
|
* Localize this message. If no message is found the key is returned as the message. This is
|
||||||
* the message. This is done so that developers or translators can see the
|
* done so that developers or translators can see the messages that still need localization.
|
||||||
* messages that still need localization.
|
|
||||||
* </p>
|
* </p>
|
||||||
* <p>
|
* <p>
|
||||||
* Any arguments this message has are interpolated into it using the
|
* Any arguments this message has are interpolated into it using the java.text.MessageFormat
|
||||||
* java.text.MessageFormat class.
|
* class.
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @return Object Represents the localized version of this
|
* @return Object Represents the localized version of this message. The reason this method
|
||||||
* message. The reason this method returns an Object and
|
* returns an Object and not a String is because we might want to localize resources other than
|
||||||
* not a String is because we might want to localize
|
* strings, such as icons or sound bites. Maybe this class should have been called
|
||||||
* resources other than strings, such as icons or sound
|
|
||||||
* bites. Maybe this class should have been called
|
|
||||||
* GlobalizedObject?
|
* GlobalizedObject?
|
||||||
*/
|
*/
|
||||||
public Object localize() {
|
public Object localize() {
|
||||||
|
|
@ -189,23 +215,20 @@ public class GlobalizedMessage {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* Localize this message according the specified request. If no message is
|
* Localize this message according the specified request. If no message is found the key is
|
||||||
* found the key is returned as the message. This is done so that
|
* returned as the message. This is done so that developers or translators can see the messages
|
||||||
* developers or translators can see the messages that still need
|
* that still need localization.
|
||||||
* localization.
|
|
||||||
* </p>
|
* </p>
|
||||||
* <p>
|
* <p>
|
||||||
* Any arguments this message has are interpolated into it using the
|
* Any arguments this message has are interpolated into it using the java.text.MessageFormat
|
||||||
* java.text.MessageFormat class.
|
* class.
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @param request The current running request.
|
* @param request The current running request.
|
||||||
*
|
*
|
||||||
* @return Object Represents the localized version of this
|
* @return Object Represents the localized version of this message. The reason this method
|
||||||
* message. The reason this method returns an Object and
|
* returns an Object and not a String is because we might want to localize resources other than
|
||||||
* not a String is because we might want to localize
|
* strings, such as icons or sound bites. Maybe this class should have been called
|
||||||
* resources other than strings, such as icons or sound
|
|
||||||
* bites. Maybe this class should have been called
|
|
||||||
* GlobalizedObject?
|
* GlobalizedObject?
|
||||||
*/
|
*/
|
||||||
public Object localize(final HttpServletRequest request) {
|
public Object localize(final HttpServletRequest request) {
|
||||||
|
|
@ -215,23 +238,20 @@ public class GlobalizedMessage {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* Localize this message with the provided locale. If no message is
|
* Localize this message with the provided locale. If no message is found the key is returned as
|
||||||
* found the key is returned as the message. This is done so that
|
* the message. This is done so that developers or translators can see the messages that still
|
||||||
* developers or translators can see the messages that still need
|
* need localization.
|
||||||
* localization.
|
|
||||||
* </p>
|
* </p>
|
||||||
* <p>
|
* <p>
|
||||||
* Any arguments this message has are interpolated into it using the
|
* Any arguments this message has are interpolated into it using the java.text.MessageFormat
|
||||||
* java.text.MessageFormat class.
|
* class.
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @param locale The locale to try to use to localize this message.
|
* @param locale The locale to try to use to localize this message.
|
||||||
*
|
*
|
||||||
* @return Object Represents the localized version of this
|
* @return Object Represents the localized version of this message. The reason this method
|
||||||
* message. The reason this method returns an Object and
|
* returns an Object and not a String is because we might want to localize resources other than
|
||||||
* not a String is because we might want to localize
|
* strings, such as icons or sound bites. Maybe this class should have been called
|
||||||
* resources other than strings, such as icons or sound
|
|
||||||
* bites. Maybe this class should have been called
|
|
||||||
* GlobalizedObject?
|
* GlobalizedObject?
|
||||||
*/
|
*/
|
||||||
public Object localize(final Locale locale) {
|
public Object localize(final Locale locale) {
|
||||||
|
|
@ -260,10 +280,13 @@ public class GlobalizedMessage {
|
||||||
// Therefore, all what was to do was to change the call of getBundle here from
|
// Therefore, all what was to do was to change the call of getBundle here from
|
||||||
// ResourceBundle#getBundle(String, Locale) to ResourceBundle#getBundle(String, Locale, ResourceControl)
|
// ResourceBundle#getBundle(String, Locale) to ResourceBundle#getBundle(String, Locale, ResourceControl)
|
||||||
// with ResourceBundle.Control.getNoFallbackControl(List<String>).
|
// with ResourceBundle.Control.getNoFallbackControl(List<String>).
|
||||||
|
// jensp 2014-07-10:
|
||||||
|
// It is now possible to pass the custom implementation of ResourceBundle.Control to
|
||||||
|
// a GlobalizedMessage
|
||||||
resourceBundle = ResourceBundle.getBundle(
|
resourceBundle = ResourceBundle.getBundle(
|
||||||
getBundleName(),
|
getBundleName(),
|
||||||
locale,
|
locale,
|
||||||
ResourceBundle.Control.getNoFallbackControl(ResourceBundle.Control.FORMAT_DEFAULT));
|
rbControl);
|
||||||
} catch (MissingResourceException e) {
|
} catch (MissingResourceException e) {
|
||||||
if (LOGGER.isDebugEnabled()) {
|
if (LOGGER.isDebugEnabled()) {
|
||||||
LOGGER.debug(
|
LOGGER.debug(
|
||||||
|
|
@ -304,9 +327,8 @@ public class GlobalizedMessage {
|
||||||
/**
|
/**
|
||||||
* For debugging, not for localizing.
|
* For debugging, not for localizing.
|
||||||
*
|
*
|
||||||
* If you need a String, use an additional localize() to get an object
|
* If you need a String, use an additional localize() to get an object and cast it to String.
|
||||||
* and cast it to String. e.g.
|
* e.g. String label = (String) GlobalizedMessage(key,bundleName).localize();
|
||||||
* String label = (String) GlobalizedMessage(key,bundleName).localize();
|
|
||||||
*
|
*
|
||||||
* @return The contents in String form for debugging.
|
* @return The contents in String form for debugging.
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue