Esempio n. 1
0
  /**
   * Gets a message based on the specified code. If not found, returns an error message to denote
   * it.
   *
   * <p>If fmtArgs is not null, {@link org.zkoss.text.MessageFormats#format} is called to format the
   * message. However, unlike MessageFormat's default behavior, all null objects are treated as an
   * empty string rather than "null".
   *
   * <p>It also recognizes {@link org.zkoss.lang.Objects#UNKNOWN}.
   *
   * @param code the code
   * @param fmtArgs the argument lists to format the message
   * @param locale the locale of the message to load
   * @return the message; never be null
   */
  public static String get(int code, Object[] fmtArgs, Locale locale) {
    try {
      String s = getFromBundle(code, locale);
      if (s == null) return getNotFound(code, locale);

      if (fmtArgs != null && fmtArgs.length > 0) {
        final Object[] args = new Object[fmtArgs.length];
        final Formatter formatter = _formatter;
        for (int j = 0; j < fmtArgs.length; ++j) {
          final Object arg = fmtArgs[j];
          if (formatter != null) args[j] = formatter.format(arg);
          else if (arg == null || arg == Objects.UNKNOWN) args[j] = "";
          else if (arg instanceof Object[]) args[j] = Objects.toString(arg);
          else args[j] = arg;
        }
        s = MessageFormats.format(s, args, locale);
      }
      return s;
    } catch (Exception ex) {
      log.realCause(ex);
      return getNotFound(code, locale);
    }
  }