Java String Format getFormat(String key, ResourceBundle bundle)

Here you can find the source of getFormat(String key, ResourceBundle bundle)

Description

Get the format string associated with key in the given resource bundle.

License

Apache License

Parameter

Parameter Description
key the key of the format string to retrieve
bundle the bundle to retrieve the format string from

Return

the format string.

Declaration

private static String getFormat(String key, ResourceBundle bundle) 

Method Source Code

//package com.java2s;
/*// w w  w  .  j  a  v a 2s.  c  o m
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.text.MessageFormat;

import java.util.MissingResourceException;

import java.util.ResourceBundle;

public class Main {
    /**
     * Get the format string associated with <code>key</code> in the
     * given resource bundle. If <code>key</code> is not present in
     * the bundle, a default format string is returned which will
     * identify the missing key when printed.
     *
     * @param key the key of the format string to retrieve
     * @param bundle the bundle to retrieve the format string from
     * @return the format string.
     */
    private static String getFormat(String key, ResourceBundle bundle) {
        String fmt = "no text found: \"" + key + "\" {0}";
        if (bundle != null) {
            try {
                fmt = bundle.getString(key);
            } catch (MissingResourceException e) {
            }
        }
        return fmt;
    }

    /**
     * Print out string according to resourceBundle format.
     *
     * @param key the key of the format string to retrieve
     * @param bundle the bundle to retrieve the format string from
     */
    public static String getString(String key, ResourceBundle bundle) {
        return MessageFormat.format(getFormat(key, bundle), null);
    }

    /**
     * Print out string according to resourceBundle format.
     *
     * @param key the key of the format string to retrieve
     * @param bundle the bundle to retrieve the format string from
     * @param val the value to substitute into the {0} parameter
     */
    public static String getString(String key, ResourceBundle bundle, Object val) {
        return MessageFormat.format(getFormat(key, bundle), new Object[] { val });
    }

    /**
     * Print out string according to resourceBundle format.
     *
     * @param key the key of the format string to retrieve
     * @param bundle the bundle to retrieve the format string from
     * @param val1 the value to substitute into the {0} parameter
     * @param val2 the value to substitute into the {1} parameter
     */
    public static String getString(String key, ResourceBundle bundle, Object val1, Object val2) {
        return MessageFormat.format(getFormat(key, bundle), new Object[] { val1, val2 });
    }

    /**
     * Print out string according to resourceBundle format.
     *
     * @param key the key of the format string to retrieve
     * @param bundle the bundle to retrieve the format string from
     * @param val1 the value to substitute into the {0} parameter
     * @param val2 the value to substitute into the {1} parameter
     * @param val3 the value to substitute into the {2} parameter
     */
    public static String getString(String key, ResourceBundle bundle, Object val1, Object val2, Object val3) {
        return MessageFormat.format(getFormat(key, bundle), new Object[] { val1, val2, val3 });
    }
}

Related

  1. formatWords(String words, String delimiter, boolean isHeaderUpperCase)
  2. getAnswerKey(String itemFormat)
  3. getCodeFileName(String fileNameFormat, String randomCode)
  4. getDBPediaFormatDescriptions( String descriptionStr)
  5. getEmailFormatError(String fieldPropertyName)
  6. getFormatted(String messageKey)
  7. getFormattedMessage(final String pattern, final Object[] arguments)
  8. getFormattedString(ResourceBundle b, String key, Object... params)
  9. getFormattedString(String key, Object arg)