Gets a integer for the given key from the given resource bundle or one of its parents. - Java java.util

Java examples for java.util:ResourceBundle

Description

Gets a integer for the given key from the given resource bundle or one of its parents.

Demo Code

/*******************************************************************************
 * Copyright 2010 Guy Davenport/*w  w  w.j av  a  2s  .c  o  m*/
 *
 * Licensed 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.
 *******************************************************************************/
//package com.java2s;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

public class Main {
    /**
     * Gets a integer for the given key from the given resource bundle or one of its parents.
     * Most commonly used for obtaining language specific integers required in user interfaces
     * 
     * @param resourceBundle the resource bundle containing the required integer
     * @param key the key of the integer in the resource bundle
     * @return the required integer
     */
    public static final Integer getInteger(ResourceBundle resourceBundle,
            String key) {
        try {
            return Integer.valueOf(resourceBundle.getString(key));
        } catch (Exception e) {
            return Integer.MIN_VALUE;
        }
    }

    /**
     * Gets a string for the given key from the given resource bundle or one of its parents.
     * Most commonly used for obtaining language specific text required in user interfaces
     * 
     * @param resourceBundle the resource bundle containing the required string
     * @param key the key of the string in the resource bundle
     * @return the required string
     */
    public static final String getString(ResourceBundle resourceBundle,
            String key) {
        try {
            return resourceBundle.getString(key);
        } catch (MissingResourceException e) {
            return '!' + key + '!';
        }
    }

    /**
     * Gets a parameterised string for the given key from the given resource bundle or one of its parents.
     * Most commonly used for obtaining language specific text required in user interfaces.  Occurrences of 
     * of {<code>i</code>} where n is an integer from 0 to n-1, and n is size of the parameters array, will be replaced 
     * by the <code>i</code>th element in the parameters array
     * 
     * @param resourceBundle the resource bundle containing the required string
     * @param key the key of the string in the resource bundle
     * @param parameters to be substituted into the returned string
     * @return the required string
     */
    public static final String getString(ResourceBundle resourceBundle,
            String key, String[] parameters) {
        String value = getString(resourceBundle, key);

        if (parameters != null && parameters.length > 0) {
            for (int i = 0; i < parameters.length; ++i) {
                if (parameters[i] != null)
                    value = value.replace("{" + i + "}", parameters[i]);
            }
        }

        return value;
    }

    /**
     * Gets a parameterised string for the given key from the given resource bundle or one of its parents.
     * Most commonly used for obtaining language specific text required in user interfaces.  Occurrences of 
     * of {<code>i</code>} where n is an integer from 0 to n-1, and n is size of the parameters array, will be replaced 
     * by the <code>i</code>th element in the parameters array
     * 
     * @param resourceBundle the resource bundle containing the required string
     * @param key the key of the string in the resource bundle
     * @param parameters to be substituted into the returned string
     * @return the required string
     */
    public static final String getString(ResourceBundle resourceBundle,
            String key, Object[] parameters) {
        String value = getString(resourceBundle, key);

        if (parameters != null && parameters.length > 0) {
            for (int i = 0; i < parameters.length; ++i) {
                value = value.replace("{" + i + "}",
                        parameters[i] != null ? parameters[i].toString()
                                : "");
            }
        }

        return value;
    }

    /**
     * Gets a parameterised string for the given key from the given resource bundle or one of its parents.
     * Most commonly used for obtaining language specific text required in user interfaces.  Occurrences of 
     * of {<code>i</code>} where n is an integer from 0 to n-1, and n is size of the parameters array, will be replaced 
     * by the <code>i</code>th element in the parameters array
     * 
     * @param resourceBundle the resource bundle containing the required string
     * @param key the key of the string in the resource bundle
     * @param parameters to be substituted into the returned string
     * @return the required string
     */
    public static final String getString(ResourceBundle resourceBundle,
            String key, int[] parameters) {
        String value = getString(resourceBundle, key);

        if (parameters != null && parameters.length > 0) {
            for (int i = 0; i < parameters.length; ++i) {
                value = value.replace("{" + i + "}",
                        String.valueOf(parameters[i]));
            }
        }

        return value;
    }

    /**
     * Gets a parameterised string for the given key from the given resource bundle or one of its parents.
     * Most commonly used for obtaining language specific text required in user interfaces.  Occurrences of 
     * of {<code>i</code>} where n is an integer from 0 to n-1, and n is size of the parameters array, will be replaced 
     * by the <code>i</code>th element in the parameters array
     * 
     * @param resourceBundle the resource bundle containing the required string
     * @param key the key of the string in the resource bundle
     * @param parameters to be substituted into the returned string
     * @return the required string
     */
    public static final String getString(ResourceBundle resourceBundle,
            String key, double[] parameters) {
        String value = getString(resourceBundle, key);

        if (parameters != null && parameters.length > 0) {
            for (int i = 0; i < parameters.length; ++i) {
                value = value.replace("{" + i + "}",
                        String.valueOf(parameters[i]));
            }
        }

        return value;
    }
}

Related Tutorials