对 Seam 记录器使用特定区域设置

发布于 2024-10-22 04:10:48 字数 206 浏览 1 评论 0原文

Seam 记录器在底层使用 Java 的 MessageFormat 类来格式化,例如。数字。如果我正确解释代码,则用于此的区域设置来自 org.jboss.seam.core.locale 。但是,我不知道如何从那里开始。我希望记录器仅使用英语区域设置(任何英语区域设置都可以),因为我们所有的日志记录都是英语的。系统或用户很可能有另一个区域设置,但这不应影响日志记录。

The Seam logger uses, deep down, Java’s MessageFormat class to format eg. numbers. The locale used for this comes from the org.jboss.seam.core.locale, if I interpret the code correctly. However, I do not know how to proceed from there. I want the logger to use the English locale only (any English locale would be fine) because all our logging is in English. The system or the user may well have another locale, but this should not influence logging.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧情勿念 2024-10-29 04:10:48

它不是那么优雅,因为您必须复制除一行之外的几乎所有代码,但您可以覆盖 Interpolator 组件的内置实现。

/**
 * 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();
    }

}

It's not that elegant because you have to duplicate almost all the code except one line, but you can override the built-in implementation of Interpolator component.

/**
 * 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();
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文