001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.cli2.resource;
018    
019    import java.text.MessageFormat;
020    
021    import java.util.Locale;
022    import java.util.MissingResourceException;
023    import java.util.ResourceBundle;
024    
025    /**
026     * A utility class used to provide internationalisation support.
027     *
028     * @author John Keyes
029     */
030    public class ResourceHelper {
031        /** system property */
032        private static final String PROP_LOCALE = "org.apache.commons.cli2.resource.bundle";
033    
034        /** default package name */
035        private static final String DEFAULT_BUNDLE =
036            "org.apache.commons.cli2.resource.CLIMessageBundle_en_US";
037        private static ResourceHelper helper;
038    
039        /** resource bundle */
040        private ResourceBundle bundle;
041    
042        private String prop;
043    
044        /**
045         * Create a new ResourceHelper for the current locale.
046         */
047        private ResourceHelper() {
048            String bundleName = System.getProperty(PROP_LOCALE);
049    
050            if (bundleName == null) {
051                bundleName = DEFAULT_BUNDLE;
052            }
053    
054            this.prop = bundleName;
055    
056            int firstUnderscore = bundleName.indexOf('_');
057            int secondUnderscore = bundleName.indexOf('_', firstUnderscore + 1);
058    
059            Locale locale;
060            if (firstUnderscore != -1) {
061                String language = bundleName.substring(firstUnderscore + 1, secondUnderscore);
062                String country = bundleName.substring(secondUnderscore + 1);
063                locale = new Locale(language, country);
064            }
065            else {
066                locale = Locale.getDefault();
067            }
068            // initialize the bundle
069            try {
070                bundle = ResourceBundle.getBundle(bundleName, locale);
071            } catch (MissingResourceException exp) {
072                bundle = ResourceBundle.getBundle(DEFAULT_BUNDLE, locale);
073            }
074        }
075    
076        public String getBundleName() {
077            return this.prop;
078        }
079    
080        /**
081         * Gets the ResourceHelper appropriate to the current locale.
082         * @return a ResourceHelper
083         */
084        public static ResourceHelper getResourceHelper() {
085            String bundleName = System.getProperty(PROP_LOCALE);
086            if (helper == null || !helper.getBundleName().equals(bundleName)) {
087                helper = new ResourceHelper();
088            }
089    
090            return helper;
091        }
092    
093        /**
094         * Returns the message for the specified key.
095         *
096         * @param key the unique identifier of the message
097         * @return String the formatted String
098         */
099        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    }