Gets a string array 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 string array for the given key from the given resource bundle or one of its parents.

Demo Code

/*******************************************************************************
 * Copyright 2010 Guy Davenport//from   w  ww  .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 {
    private static final String DEFAULT_DELIMITER = ",";

    /**
     * Gets a string array 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 array
     */
    public static String[] getStringArray(ResourceBundle resourceBundle,
            String key) {
        try {
            String string = resourceBundle.getString(key);

            String[] array = string.split(DEFAULT_DELIMITER);

            for (int i = 0; i < array.length; ++i)
                array[i] = array[i].trim();

            return array;
        } catch (MissingResourceException e) {
            return new String[] { '!' + key + '!' };
        }
    }

    /**
     * Gets a parameterised string array 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 array
     */
    public static String[] getStringArray(ResourceBundle resourceBundle,
            String key, String[] parameters) {
        try {
            String string = resourceBundle.getString(key);

            String[] array = string.split(DEFAULT_DELIMITER);

            for (int i = 0; i < array.length; ++i) {
                array[i] = array[i].trim();

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

            return array;
        } catch (MissingResourceException e) {
            return new String[] { '!' + key + '!' };
        }
    }

    /**
     * Gets a parameterised string array 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 array
     */
    public static String[] getStringArray(ResourceBundle resourceBundle,
            String key, int[] parameters) {
        try {
            String string = resourceBundle.getString(key);

            String[] array = string.split(DEFAULT_DELIMITER);

            for (int i = 0; i < array.length; ++i) {
                array[i] = array[i].trim();

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

            return array;
        } catch (MissingResourceException e) {
            return new String[] { '!' + key + '!' };
        }
    }

    /**
     * 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