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 java.text.MessageFormat;
018    
019    import java.util.Locale;
020    import java.util.MissingResourceException;
021    import java.util.ResourceBundle;
022    
023    /**
024     * @author Shuyang Zhou
025     * @author Neil Griffin
026     */
027    public class ResourceBundleUtil {
028    
029            public static String getString(
030                    ResourceBundle resourceBundle, Locale locale, String key,
031                    Object[] arguments) {
032    
033                    String value = getString(resourceBundle, key);
034    
035                    if (value == null) {
036                            return null;
037                    }
038    
039                    // Get the value associated with the specified key, and substitute any
040                    // arguments like {0}, {1}, {2}, etc. with the specified argument
041                    // values.
042    
043                    if (ArrayUtil.isNotEmpty(arguments)) {
044                            MessageFormat messageFormat = new MessageFormat(value, locale);
045    
046                            value = messageFormat.format(arguments);
047                    }
048    
049                    return value;
050            }
051    
052            public static String getString(ResourceBundle resourceBundle, String key) {
053                    if (!resourceBundle.containsKey(key)) {
054                            return null;
055                    }
056    
057                    try {
058                            return resourceBundle.getString(key);
059                    }
060                    catch (MissingResourceException mre) {
061                            return null;
062                    }
063            }
064    
065    }