1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.util;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  
20  import java.text.MessageFormat;
21  
22  import java.util.Locale;
23  import java.util.ResourceBundle;
24  
25  /**
26   * <a href="ResourceBundleUtil.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Neil Griffin
29   */
30  public class ResourceBundleUtil {
31  
32      public static String getString(
33          ResourceBundle resourceBundle, Locale locale, String key,
34          Object[] arguments) {
35  
36          String value = null;
37  
38          if (resourceBundle == null) {
39              if (_log.isErrorEnabled()) {
40                  _log.error("Resource bundle is null");
41              }
42          }
43          else {
44  
45              // Get the value associated with the specified key, and substitute
46              // any arguuments like {0}, {1}, {2}, etc. with the specified
47              // argument values.
48  
49              value = resourceBundle.getString(key);
50  
51              if (value == null) {
52                  if (_log.isWarnEnabled()) {
53                      _log.warn("No value found for key " + key);
54                  }
55              }
56              else {
57                  if ((arguments != null) && (arguments.length > 0)) {
58                      MessageFormat messageFormat = new MessageFormat(
59                          value, locale);
60  
61                      value = messageFormat.format(arguments);
62                  }
63              }
64          }
65  
66          if (value == null) {
67              value = key;
68          }
69  
70          return value;
71      }
72  
73      private static Log _log = LogFactoryUtil.getLog(ResourceBundleUtil.class);
74  
75  }