View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.cli2.resource;
18  
19  import java.text.MessageFormat;
20  
21  import java.util.Locale;
22  import java.util.MissingResourceException;
23  import java.util.ResourceBundle;
24  
25  /**
26   * A utility class used to provide internationalisation support.
27   *
28   * @author John Keyes
29   */
30  public class ResourceHelper {
31      /** system property */
32      private static final String PROP_LOCALE = "org.apache.commons.cli2.resource.bundle";
33  
34      /** default package name */
35      private static final String DEFAULT_BUNDLE =
36          "org.apache.commons.cli2.resource.CLIMessageBundle_en_US";
37      private static ResourceHelper helper;
38  
39      /** resource bundle */
40      private ResourceBundle bundle;
41  
42      private String prop;
43  
44      /**
45       * Create a new ResourceHelper for the current locale.
46       */
47      private ResourceHelper() {
48          String bundleName = System.getProperty(PROP_LOCALE);
49  
50          if (bundleName == null) {
51              bundleName = DEFAULT_BUNDLE;
52          }
53  
54          this.prop = bundleName;
55  
56          int firstUnderscore = bundleName.indexOf('_');
57          int secondUnderscore = bundleName.indexOf('_', firstUnderscore + 1);
58  
59          Locale locale;
60          if (firstUnderscore != -1) {
61              String language = bundleName.substring(firstUnderscore + 1, secondUnderscore);
62              String country = bundleName.substring(secondUnderscore + 1);
63              locale = new Locale(language, country);
64          }
65          else {
66              locale = Locale.getDefault();
67          }
68          // initialize the bundle
69          try {
70              bundle = ResourceBundle.getBundle(bundleName, locale);
71          } catch (MissingResourceException exp) {
72              bundle = ResourceBundle.getBundle(DEFAULT_BUNDLE, locale);
73          }
74      }
75  
76      public String getBundleName() {
77          return this.prop;
78      }
79  
80      /**
81       * Gets the ResourceHelper appropriate to the current locale.
82       * @return a ResourceHelper
83       */
84      public static ResourceHelper getResourceHelper() {
85          String bundleName = System.getProperty(PROP_LOCALE);
86          if (helper == null || !helper.getBundleName().equals(bundleName)) {
87              helper = new ResourceHelper();
88          }
89  
90          return helper;
91      }
92  
93      /**
94       * Returns the message for the specified key.
95       *
96       * @param key the unique identifier of the message
97       * @return String the formatted String
98       */
99      public String getMessage(final String key) {
100         return getMessage(key, new Object[] {  });
101     }
102 
103     /**
104      * Returns the message for the specified key and argument.
105      *
106      * @param key the unique identifier of the message
107      * @param value the argument value
108      * @return String the formatted String
109      */
110     public String getMessage(final String key,
111                              final Object value) {
112         return getMessage(key, new Object[] { value });
113     }
114 
115     /**
116      * Returns the message for the specified key and arguments.
117      *
118      * @param key the unique identifier of the message
119      * @param value1 an argument value
120      * @param value2 an argument value
121      * @return String the formatted String
122      */
123     public String getMessage(final String key,
124                              final Object value1,
125                              final Object value2) {
126         return getMessage(key, new Object[] { value1, value2 });
127     }
128 
129     /**
130      * Returns the message for the specified key and arguments.
131      *
132      * @param key the unique identifier of the message
133      * @param value1 an argument value
134      * @param value2 an argument value
135      * @param value3 an argument value
136      *
137      * @return String the formatted String
138      */
139     public String getMessage(final String key,
140                              final Object value1,
141                              final Object value2,
142                              final Object value3) {
143         return getMessage(key, new Object[] { value1, value2, value3 });
144     }
145 
146     /**
147      * Returns the message for the specified key and arguments.
148      *
149      * @param key the unique identifier of the message
150      * @param values argument values
151      * @return String the formatted String
152      */
153     public String getMessage(final String key,
154                              final Object[] values) {
155         final String msgFormatStr = bundle.getString(key);
156         final MessageFormat msgFormat = new MessageFormat(msgFormatStr);
157 
158         return msgFormat.format(values);
159     }
160 }