View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  
21  package org.waarp.openr66.protocol.configuration;
22  
23  import org.waarp.common.logging.SysErrLogger;
24  import org.waarp.common.utility.ParametersChecker;
25  import org.waarp.common.utility.SystemPropertyUtil;
26  
27  import java.text.MessageFormat;
28  import java.util.Locale;
29  import java.util.MissingResourceException;
30  import java.util.ResourceBundle;
31  
32  public class Messages {
33    private static final String BUNDLE_NAME = "messages"; //$NON-NLS-1$
34  
35    private static ResourceBundle resourceBundle;
36    private static String slocale = "en";
37  
38    static {
39      try {
40        setSlocale(
41            SystemPropertyUtil.get(R66SystemProperties.OPENR66_LOCALE, "en"));
42        if (ParametersChecker.isEmpty(getSlocale())) {
43          setSlocale("en");
44        }
45        init(new Locale(getSlocale()));
46      } catch (final Throwable ignored) {//NOSONAR
47        SysErrLogger.FAKE_LOGGER //NOSONAR
48                                 .syserr("Error during static execution",//NOSONAR
49                                         ignored);//NOSONAR
50      }
51    }
52  
53    private Messages() {
54    }
55  
56    public static void init(Locale locale) {
57      if (locale == null) {
58        setSlocale("en");
59        locale = new Locale(getSlocale());
60      } else {
61        setSlocale(locale.getLanguage());
62      }
63      resourceBundle = ResourceBundle.getBundle(BUNDLE_NAME, locale);
64    }
65  
66    public static String getString(final String key) {
67      try {
68        return resourceBundle.getString(key);
69      } catch (final MissingResourceException e) {
70        return '!' + key + '!';
71      }
72    }
73  
74    public static String getString(final String key, final Object... args) {
75      try {
76        final String source = resourceBundle.getString(key);
77        return MessageFormat.format(source, args);
78      } catch (final MissingResourceException e) {
79        return '!' + key + '!';
80      }
81    }
82  
83    /**
84     * @return the slocale
85     */
86    public static String getSlocale() {
87      return slocale;
88    }
89  
90    /**
91     * @param slocale the slocale to set
92     */
93    public static void setSlocale(final String slocale) {
94      Messages.slocale = slocale;
95    }
96  }