Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Alias zanataMessages to messages; implement Map
Browse files Browse the repository at this point in the history
  • Loading branch information
seanf committed Jul 2, 2014
1 parent 7493d20 commit d4c0ab8
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 14 deletions.
Expand Up @@ -37,7 +37,7 @@
public abstract class AbstractAutocomplete<T> {

protected ZanataMessages zanataMessages =
ServiceLocator.instance().getInstance(ZanataMessages.class);
ServiceLocator.instance().getInstance("messages", ZanataMessages.class);

protected ConversationScopeMessages conversationScopeMessages =
ConversationScopeMessages.instance();
Expand Down
136 changes: 123 additions & 13 deletions zanata-war/src/main/java/org/zanata/util/ZanataMessages.java
Expand Up @@ -20,39 +20,149 @@
*/
package org.zanata.util;

import java.text.MessageFormat;
import java.util.AbstractMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Set;

import com.google.common.annotations.VisibleForTesting;
import lombok.NoArgsConstructor;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.core.Interpolator;
import org.jboss.seam.core.SeamResourceBundle;

import static org.jboss.seam.ScopeType.EVENT;
import static org.jboss.seam.annotations.Install.APPLICATION;

/**
* Utility component to help with programmatic access to the message resource
* bundle.
*
* Contrary to the {@link org.jboss.seam.international.Messages} component, this
* component does not hold already interpolated messages, but rather allows
* interpolation to happen on demmand. Use this component when there is a need
* to access parametrized messages.
* Unlike the {@link org.jboss.seam.international.Messages} component, this
* component can also format messages which use position arguments like {0} and
* {1}.
*
* @author Carlos Munoz <a
* href="mailto:camunoz@redhat.com">camunoz@redhat.com</a>
*/
@Name("zanataMessages")
@Scope(ScopeType.STATELESS)
@AutoCreate
public class ZanataMessages {
@In
public class ZanataMessages extends AbstractMap<String, String> {

@Name("org.jboss.seam.international.messagesFactory")
@Install(precedence = APPLICATION)
@Scope(ScopeType.EVENT)
public static class Factory {
// Seam ResourceBundle with built-in interpolation
@In
private ResourceBundle resourceBundle;

// components.xml adds an alias 'zanataMessages' but 'messages' is preferred
@org.jboss.seam.annotations.Factory(
value = "org.jboss.seam.international.messages",
autoCreate = true, scope = EVENT)
public Map<String, String> getMessages() {
// Generic ResourceBundle without built-in interpolation:
// ResourceBundle resourceBundle = ResourceBundle.getBundle(
// "messages", org.jboss.seam.core.Locale.instance());
return new ZanataMessages(resourceBundle);
}
}

private ResourceBundle resourceBundle;

@In
private Interpolator interpolator;
ZanataMessages(ResourceBundle resourceBundle) {
this.resourceBundle = resourceBundle;
}

@VisibleForTesting
public ZanataMessages() {
// Seam ResourceBundle with built-in interpolation:
this(SeamResourceBundle.getBundle());
// Generic ResourceBundle without built-in interpolation:
// this(ResourceBundle.getBundle("messages",
// org.jboss.seam.core.Locale.instance()));
}

// the default toString includes the entire list of properties,
// which makes a mess of the log file
@Override
public String toString() {
return getClass().getName();
// return getClass().getName()+"@"+Integer.toHexString(System.identityHashCode(this));
}

/**
* Gets a resource string. In future, this method will return a string
* without any interpolation or message formatting.
* @param key
* @return
*/
@Override
public String get(Object key) {
if (key instanceof String) {
String resourceKey = (String) key;
try {
String resource = resourceBundle.getString(resourceKey);
return (resource == null) ? resourceKey : resource;
} catch (MissingResourceException mre) {
return resourceKey;
}
} else {
return null;
}
}

// use messages.get() or messages.format(), not zanataMessages.getMessage()
@Deprecated
public String getMessage(String key, Object... args) {
String template = resourceBundle.getString(key);
return interpolator.interpolate(template, args);
return format(key, args);
}

/**
* Gets a resource string, and interpolates both Seam context variables
* and positional parameters. In future, positional parameters only.
* @param key
* @param args
* @return
*/
public String format(String key, Object... args) {
String template = get(key);
return MessageFormat.format(template, args);
}

@Override
public Set<Map.Entry<String, String>> entrySet() {
Set<Map.Entry<String, String>> entrySet =
new HashSet<Entry<String, String>>();

for (final String key : resourceBundle.keySet()) {
entrySet.add(new Map.Entry<String, String>() {

@Override
public String getKey() {
return key;
}

@Override
public String getValue() {
return get(key);
}

@Override
public String setValue(String val) {
throw new UnsupportedOperationException();
}
});
}
return entrySet;
}
}
Expand Up @@ -115,6 +115,11 @@
<factory name="session" value="#{entityManager.delegate}" scope="STATELESS"
auto-create="true" />

<!-- TODO remove zanataMessages -->
<factory name="zanataMessages"
value="#{org.jboss.seam.international.messages}" scope="STATELESS"
auto-create="true"/>

<component class="org.zanata.ApplicationConfiguration"
name="applicationConfiguration" precedence="30">
<property name="debug">@env.debug@</property>
Expand Down

0 comments on commit d4c0ab8

Please sign in to comment.