Package: BundleUtilities
BundleUtilities
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
getMessage(Class, Locale, String, Object[]) |
|
|
|
|
|
||||||||||||||||||||
getMessage(Class, String, Object[]) |
|
|
|
|
|
Coverage
1: /*
2: * #%L
3: * *********************************************************************************************************************
4: *
5: * These Foolish Things - Miscellaneous utilities
6: * http://thesefoolishthings.tidalwave.it - git clone git@bitbucket.org:tidalwave/thesefoolishthings-src.git
7: * %%
8: * Copyright (C) 2009 - 2018 Tidalwave s.a.s. (http://tidalwave.it)
9: * %%
10: * *********************************************************************************************************************
11: *
12: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
13: * the License. You may obtain a copy of the License at
14: *
15: * http://www.apache.org/licenses/LICENSE-2.0
16: *
17: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
18: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
19: * specific language governing permissions and limitations under the License.
20: *
21: * *********************************************************************************************************************
22: *
23: * $Id$
24: *
25: * *********************************************************************************************************************
26: * #L%
27: */
28: package it.tidalwave.util;
29:
30: import javax.annotation.Nonnull;
31: import java.util.Locale;
32: import java.util.ResourceBundle;
33: import lombok.AccessLevel;
34: import lombok.NoArgsConstructor;
35:
36: /***********************************************************************************************************************
37: *
38: * Facility class to manage resource bundles.
39: *
40: * @since 3.1-ALPHA-2
41: * @author Fabrizio Giudici (Fabrizio.Giudici@tidalwave.it)
42: * @version $Id: $
43: *
44: **********************************************************************************************************************/
45: @NoArgsConstructor(access = AccessLevel.PRIVATE)
46: public final class BundleUtilities
47: {
48: /*******************************************************************************************************************
49: *
50: * Returns a localised message.
51: *
52: * @param ownerClass the owner of the bundle
53: * @param resourceName the name of the resource inside the bundle
54: * @param params the parameters (used if the string in the bundle is a {@code String} format)
55: * @return the message
56: *
57: ******************************************************************************************************************/
58: @Nonnull
59: public static String getMessage (final @Nonnull Class<?> ownerClass,
60: final @Nonnull String resourceName,
61: final @Nonnull Object ... params)
62: {
63: return getMessage(ownerClass, Locale.getDefault(), resourceName, params);
64: }
65:
66: /*******************************************************************************************************************
67: *
68: * Returns a localised message.
69: *
70: * @since 3.1-ALPHA-4
71: * @param ownerClass the owner of the bundle
72: * @param locale the {@link Locale}
73: * @param resourceName the name of the resource inside the bundle
74: * @param params the parameters (used if the string in the bundle is a {@code String} format)
75: * @return the message
76: *
77: ******************************************************************************************************************/
78: @Nonnull
79: public static String getMessage (final @Nonnull Class<?> ownerClass,
80: final @Nonnull Locale locale,
81: final @Nonnull String resourceName,
82: final @Nonnull Object ... params)
83: {
84: final String packageName = ownerClass.getPackage().getName();
85: final ResourceBundle bundle = ResourceBundle.getBundle(packageName + ".Bundle", locale);
86: final String string = bundle.getString(resourceName);
87:
88:• return (params.length == 0) ? string : String.format(string, params);
89: }
90: }