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.ArrayList;
022    import java.util.List;
023    import java.util.Locale;
024    import java.util.Map;
025    import java.util.MissingResourceException;
026    import java.util.ResourceBundle;
027    
028    /**
029     * @author Shuyang Zhou
030     * @author Neil Griffin
031     */
032    public class ResourceBundleUtil {
033    
034            public static ResourceBundle getBundle(String baseName, Class<?> clazz) {
035                    return getBundle(baseName, clazz.getClassLoader());
036            }
037    
038            public static ResourceBundle getBundle(
039                    String baseName, ClassLoader classLoader) {
040    
041                    return ResourceBundle.getBundle(
042                            baseName, Locale.getDefault(), classLoader, UTF8Control.INSTANCE);
043            }
044    
045            public static ResourceBundle getBundle(
046                    String baseName, Locale locale, Class<?> clazz) {
047    
048                    return getBundle(baseName, locale, clazz.getClassLoader());
049            }
050    
051            public static ResourceBundle getBundle(
052                    String baseName, Locale locale, ClassLoader classLoader) {
053    
054                    return ResourceBundle.getBundle(
055                            baseName, locale, classLoader, UTF8Control.INSTANCE);
056            }
057    
058            public static String getString(
059                    ResourceBundle resourceBundle, Locale locale, String key,
060                    Object[] arguments) {
061    
062                    String value = getString(resourceBundle, key);
063    
064                    if (value == null) {
065                            return null;
066                    }
067    
068                    // Get the value associated with the specified key, and substitute any
069                    // arguments like {0}, {1}, {2}, etc. with the specified argument
070                    // values.
071    
072                    if (ArrayUtil.isNotEmpty(arguments)) {
073                            MessageFormat messageFormat = new MessageFormat(value, locale);
074    
075                            value = messageFormat.format(arguments);
076                    }
077    
078                    return value;
079            }
080    
081            public static String getString(ResourceBundle resourceBundle, String key) {
082                    if (!resourceBundle.containsKey(key)) {
083                            return null;
084                    }
085    
086                    try {
087                            return resourceBundle.getString(key);
088                    }
089                    catch (MissingResourceException mre) {
090                            return null;
091                    }
092            }
093    
094            public static void loadResourceBundles(
095                    Map<String, ResourceBundle> resourceBundles, Locale locale,
096                    ResourceBundleLoader resourceBundleLoader) {
097    
098                    String languageId = LocaleUtil.toLanguageId(locale);
099    
100                    String[] languageIdParts = languageId.split("_");
101    
102                    if (ArrayUtil.isEmpty(languageIdParts)) {
103                            return;
104                    }
105    
106                    String currentLanguageId = StringPool.BLANK;
107                    List<ResourceBundle> currentResourceBundles = new ArrayList<>();
108    
109                    for (int i = 0; i < languageIdParts.length; i++) {
110                            if ( i > 0 ) {
111                                    currentLanguageId += "_";
112                            }
113    
114                            currentLanguageId += languageIdParts[i];
115    
116                            ResourceBundle resourceBundle =
117                                    resourceBundleLoader.loadResourceBundle(currentLanguageId);
118    
119                            if (resourceBundle != null) {
120                                    currentResourceBundles.add(resourceBundle);
121                            }
122    
123                            if (currentResourceBundles.isEmpty()) {
124                                    continue;
125                            }
126    
127                            resourceBundles.put(
128                                    currentLanguageId,
129                                    new AggregateResourceBundle(
130                                            currentResourceBundles.toArray(
131                                                    new ResourceBundle[currentResourceBundles.size()])));
132                    }
133            }
134    
135            public interface ResourceBundleLoader {
136    
137                    public ResourceBundle loadResourceBundle(String languageId);
138    
139            }
140    
141    }