Seam记录器在内部使用Java的MessageFormat
类来格式化,例如。数字。如果我正确解释代码,那么用于此的语言环境来自org.jboss.seam.core.locale
。但是,我不知道如何从那里开始。我希望记录器只使用英语语言环境(任何英语语言环境都可以),因为我们所有的日志记录都是英文的。系统或用户可能有另一个区域设置,但这不应影响日志记录。
答案 0 :(得分:0)
这不是那么优雅,因为你必须复制几乎所有代码,除了一行,但你可以覆盖内插器组件的内置实现。
/**
* Interpolates EL expressions in Strings
*
* @author Gavin King
*/
@BypassInterceptors
@Scope(STATELESS)
@Name("org.jboss.seam.core.interpolator")
@Install(precedence=APPLICATION)
public class Interpolator extends org.jboss.seam.core.Interpolator
{
private static final LogProvider log = Logging.getLogProvider(Interpolator.class);
/**
* Replace all EL expressions in the form #{...} with their evaluated
* values.
*
* @param string a template
* @return the interpolated string
*/
@Override
public String interpolate(String string, Object... params)
{
if (params == null) {
params = new Object[0];
}
if (params.length>10) {
throw new IllegalArgumentException("more than 10 parameters");
}
if (string.indexOf('#')>=0 || string.indexOf('{')>=0) {
string = interpolateExpressions(string, params);
}
return string;
}
private String interpolateExpressions(String string, Object... params)
{
StringTokenizer tokens = new StringTokenizer(string, "#{}", true);
StringBuilder builder = new StringBuilder(string.length());
try {
while (tokens.hasMoreTokens()) {
String tok = tokens.nextToken();
if ("#".equals(tok) && tokens.hasMoreTokens()) {
String nextTok = tokens.nextToken();
while (nextTok.equals("#") && tokens.hasMoreTokens()) {
builder.append(tok);
nextTok = tokens.nextToken();
}
if ("{".equals(nextTok)) {
String expression = "#{" + tokens.nextToken() + "}";
try {
Object value = Expressions.instance().createValueExpression(expression).getValue();
if (value!=null) builder.append(value);
} catch (Exception e) {
log.debug("exception interpolating string: " + string, e);
}
tokens.nextToken(); // the trailing "}"
} else if (nextTok.equals("#")) {
// could be trailing #
builder.append("#");
} else {
int index;
try {
index = Integer.parseInt(nextTok.substring(0, 1));
if (index>=params.length) {
//log.warn("parameter index out of bounds: " + index + " in: " + string);
builder.append("#").append(nextTok);
} else {
builder.append(params[index]).append(nextTok.substring(1));
}
} catch (NumberFormatException nfe) {
builder.append("#").append(nextTok);
}
}
} else if ("{".equals(tok)) {
StringBuilder expr = new StringBuilder();
expr.append(tok);
int level = 1;
while (tokens.hasMoreTokens()) {
String nextTok = tokens.nextToken();
expr.append(nextTok);
if (nextTok.equals("{")) {
++level;
} else if (nextTok.equals("}")) {
if (--level == 0) {
try {
if (params.length == 0)
{
builder.append(expr.toString());
}
else
{
//String value = new MessageFormat(expr.toString(), Locale.instance()).format(params);
// always use Locale.ENGLISH
String value = new MessageFormat(expr.toString(), Locale.ENGLISH).format(params);
builder.append(value);
}
} catch (Exception e) {
// if it is a bad message, use the expression itself
builder.append(expr);
}
expr = null;
break;
}
}
}
if (expr != null) {
builder.append(expr);
}
} else {
builder.append(tok);
}
}
} catch (Exception e) {
log.debug("exception interpolating string: " + string, e);
}
return builder.toString();
}
}