001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import com.liferay.portal.kernel.language.UTF8Control;
018    
019    import java.text.MessageFormat;
020    
021    import java.util.Locale;
022    import java.util.MissingResourceException;
023    import java.util.ResourceBundle;
024    
025    /**
026     * @author Shuyang Zhou
027     * @author Neil Griffin
028     */
029    public class ResourceBundleUtil {
030    
031            public static ResourceBundle getBundle(String baseName, Class<?> clazz) {
032                    return getBundle(baseName, clazz.getClassLoader());
033            }
034    
035            public static ResourceBundle getBundle(
036                    String baseName, ClassLoader classLoader) {
037    
038                    return ResourceBundle.getBundle(
039                            baseName, Locale.getDefault(), classLoader, UTF8Control.INSTANCE);
040            }
041    
042            public static ResourceBundle getBundle(
043                    String baseName, Locale locale, Class<?> clazz) {
044    
045                    return getBundle(baseName, locale, clazz.getClassLoader());
046            }
047    
048            public static ResourceBundle getBundle(
049                    String baseName, Locale locale, ClassLoader classLoader) {
050    
051                    return ResourceBundle.getBundle(
052                            baseName, locale, classLoader, UTF8Control.INSTANCE);
053            }
054    
055            public static String getString(
056                    ResourceBundle resourceBundle, Locale locale, String key,
057                    Object[] arguments) {
058    
059                    String value = getString(resourceBundle, key);
060    
061                    if (value == null) {
062                            return null;
063                    }
064    
065                    // Get the value associated with the specified key, and substitute any
066                    // arguments like {0}, {1}, {2}, etc. with the specified argument
067                    // values.
068    
069                    if (ArrayUtil.isNotEmpty(arguments)) {
070                            MessageFormat messageFormat = new MessageFormat(value, locale);
071    
072                            value = messageFormat.format(arguments);
073                    }
074    
075                    return value;
076            }
077    
078            public static String getString(ResourceBundle resourceBundle, String key) {
079                    if (!resourceBundle.containsKey(key)) {
080                            return null;
081                    }
082    
083                    try {
084                            return resourceBundle.getString(key);
085                    }
086                    catch (MissingResourceException mre) {
087                            return null;
088                    }
089            }
090    
091    }