| ResourceBundleUtil.java |
1 /**
2 * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3 *
4 * The contents of this file are subject to the terms of the Liferay Enterprise
5 * Subscription License ("License"). You may not use this file except in
6 * compliance with the License. You can obtain a copy of the License by
7 * contacting Liferay, Inc. See the License for the specific language governing
8 * permissions and limitations under the License, including but not limited to
9 * distribution rights of the Software.
10 *
11 *
12 *
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 }