View Javadoc
1   /**
2    * SpamSchutz - simple way to protect your mail adresses from naiive spammers
3    * Copyright (C) 2011, Aiki IT
4    * <p/>
5    * This program is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    * <p/>
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   * <p/>
15   * You should have received a copy of the GNU General Public License
16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  package de.aikiit.spamprotector.util;
19  
20  import com.google.common.base.Strings;
21  import lombok.AccessLevel;
22  import lombok.NoArgsConstructor;
23  import org.apache.logging.log4j.LogManager;
24  import org.apache.logging.log4j.Logger;
25  
26  import java.text.MessageFormat;
27  import java.util.Arrays;
28  import java.util.Locale;
29  import java.util.MissingResourceException;
30  import java.util.ResourceBundle;
31  
32  /**
33   * Helper to deal with localizations and ease the usage of
34   * resource bundles within spamprotector.
35   *
36   * @author hirsch
37   * @version 23.08.11
38   */
39  @NoArgsConstructor(access = AccessLevel.PRIVATE)
40  public final class LocalizationHelper {
41  
42      /**
43       * Logger.
44       */
45      private static final Logger LOG =
46              LogManager.getLogger(LocalizationHelper.class);
47  
48      /**
49       * ResourceBundle used for this application.
50       */
51      private static final String BASE_NAME = "spamprotector";
52      private static final Locale FALLBACK_LOCALE = Locale.GERMANY;
53  
54      private static ResourceBundle BUNDLE;
55      private static Locale LOCALE;
56      private static MessageFormat FORMAT;
57  
58      static {
59          setLocale();
60      }
61  
62      /**
63       * Set locale depending on system properties, in case of errors fallback is Locale.GERMANY.
64       */
65      public static void setLocale() {
66          String userLanguage = System.getProperty("user.language");
67          String userCountry = System.getProperty("user.country");
68  
69          LOG.info(String.format("Your system emits the following l10n-properties: language=%s, country=%s", userLanguage, userCountry));
70  
71          if (Strings.isNullOrEmpty(userLanguage) || Strings.isNullOrEmpty(userCountry)) {
72              LOCALE = FALLBACK_LOCALE;
73              LOG.info("Falling back to locale " + LOCALE);
74          } else {
75              LOCALE = new Locale(userLanguage, userCountry);
76              LOG.info("Setting locale to " + LOCALE);
77          }
78  
79          BUNDLE = ResourceBundle.getBundle(BASE_NAME, LOCALE);
80          FORMAT = new MessageFormat("");
81          FORMAT.setLocale(LOCALE);
82      }
83  
84      /**
85       * @return the currently set Locale of this application. Fallback is Locale.GERMANY.
86       */
87      public static Locale getLocale() {
88          if (LOCALE == null) {
89              LOG.warn("Returning fallback Locale for Germany - please make sure you've configured your environment correctly via system properties 'user.country'/'user.language'.");
90              return FALLBACK_LOCALE;
91          }
92          return LOCALE;
93      }
94  
95      /**
96       * @return the currently set language
97       */
98      public static String getLanguage() {
99          return getLocale().getLanguage();
100     }
101 
102     /**
103      * Helper function to retrieve a given key
104      * from the underlying resource bundle.
105      *
106      * @param key Key to retrieve from the bundle,
107      *            e.g. <i>spamprotector.foo.title</i>
108      * @return Returns the value from the bundle.
109      */
110     public static String getBundleString(final String key) {
111         LOG.debug("Retrieving key " + key);
112         try {
113             return BUNDLE.getString(key);
114         } catch (MissingResourceException mre) {
115             LOG.error("Retrieving unknown key " + key + ". Please fix your property files.");
116             return key;
117         }
118         // l18n basics:
119         // http://www.kodejava.org/examples/220.html
120         // l18n buttons:
121         // http://www.java2s.com/Code/Java/I18N/Createonebuttoninternationalizedly.htm
122         // l18n with parameters:
123         // http://www.java2s.com/Code/Java/I18N/ResourceBundlewithparameterposition.htm
124         // parameters are a but uneasier than with grails -
125         // http://download.oracle.com/javase/tutorial/i18n/format/messageFormat.html
126         // encoding issues / eclipse plugin:
127         // http://stackoverflow.com/questions/863838/problem-with-java-properties-utf8-encoding-in-eclipse
128     }
129 
130     /**
131      * Helper function to retrieve a given key
132      * from the underlying resource bundle while
133      * applying localization parameters.
134      *
135      * @param key        Key to retrieve from the bundle,
136      *                   e.g. <i>fotorenamer.foo.parameteredtitle</i>.
137      * @param parameters Object array with all parameters.
138      * @return Returns the value from the bundle
139      * with the given parameters applied.
140      * @see <a href="http://download.oracle.com/javase/tutorial/i18n/format/messageFormat.html">
141      * I18N-tutorial</a>
142      */
143     public static String getParameterizedBundleString(final String key, final Object... parameters) {
144         LOG.debug("Applying " + ((parameters == null) ? null : Arrays.toString(parameters)) + " parameters to " + key);
145         FORMAT.applyPattern(getBundleString(key));
146         return FORMAT.format(parameters);
147     }
148 
149 }