Coverage Report - org.jtheque.core.managers.language.LanguageManager
 
Classes in this File Line Coverage Branch Coverage Complexity
LanguageManager
0 %
0/94
0 %
0/40
2.5
 
 1  
 package org.jtheque.core.managers.language;
 2  
 
 3  
 import org.jtheque.core.managers.IManager;
 4  
 import org.jtheque.core.managers.ManagerException;
 5  
 import org.jtheque.core.managers.Managers;
 6  
 import org.jtheque.core.managers.beans.IBeansManager;
 7  
 import org.jtheque.core.managers.log.ILoggingManager;
 8  
 import org.jtheque.core.managers.state.IStateManager;
 9  
 import org.jtheque.core.managers.state.StateException;
 10  
 import org.jtheque.utils.StringUtils;
 11  
 import org.jtheque.utils.collections.ArrayUtils;
 12  
 import org.springframework.context.MessageSource;
 13  
 import org.springframework.context.NoSuchMessageException;
 14  
 
 15  
 import java.util.ArrayList;
 16  
 import java.util.Collection;
 17  
 import java.util.HashMap;
 18  
 import java.util.HashSet;
 19  
 import java.util.Locale;
 20  
 import java.util.Map;
 21  
 import java.util.Scanner;
 22  
 import java.util.Set;
 23  
 
 24  
 /*
 25  
  * This file is part of JTheque.
 26  
  *
 27  
  * JTheque is free software: you can redistribute it and/or modify
 28  
  * it under the terms of the GNU General Public License as published by
 29  
  * the Free Software Foundation, either version 3 of the License.
 30  
  *
 31  
  * JTheque is distributed in the hope that it will be useful,
 32  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 33  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 34  
  * GNU General Public License for more details.
 35  
  *
 36  
  * You should have received a copy of the GNU General Public License
 37  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 38  
  */
 39  
 
 40  
 /**
 41  
  * @author Baptiste Wicht
 42  
  */
 43  
 public final class LanguageManager implements ILanguageManager, IManager {
 44  
     private final Map<String, Locale> languages;
 45  0
     private Locale locale = Locale.getDefault();
 46  
 
 47  
     private final Set<Internationalizable> internationalizables;
 48  
 
 49  
     private LanguageState state;
 50  
 
 51  0
     private static final String[] ZERO_LENGTH_ARRAY = new String[0];
 52  
 
 53  
     /**
 54  
      * Construct a new ResourceManager.
 55  
      */
 56  
     public LanguageManager() {
 57  0
         super();
 58  
 
 59  0
         languages = new HashMap<String, Locale>(2);
 60  
 
 61  0
         languages.put("fr", Locale.FRENCH);
 62  0
         languages.put("en", Locale.ENGLISH);
 63  0
         languages.put("de", Locale.GERMAN);
 64  
 
 65  0
         internationalizables = new HashSet<Internationalizable>(100);
 66  0
     }
 67  
 
 68  
     /**
 69  
      * Return the resource bundle.
 70  
      *
 71  
      * @return The resource bundle.
 72  
      */
 73  
     private static EditableResourceBundle getResourceBundle() {
 74  0
         return Managers.getManager(IBeansManager.class).getBean("messageSource");
 75  
     }
 76  
 
 77  
     @Override
 78  
     public void preInit(){
 79  
         //Nothing to do here
 80  0
     }
 81  
 
 82  
     @Override
 83  
     public void close(){
 84  
         //Nothing to do here
 85  0
     }
 86  
 
 87  
     @Override
 88  
     public void init() throws ManagerException {
 89  0
         if (state == null) {
 90  0
             initConfiguration();
 91  
         }
 92  
 
 93  0
         locale = languages.get(state.getLanguage());
 94  
 
 95  0
         if (locale == null) {
 96  0
             Managers.getManager(ILoggingManager.class).getLogger(getClass()).error("Unable to get the locale");
 97  
 
 98  0
             locale = Locale.FRENCH;
 99  
         }
 100  
 
 101  0
         Locale.setDefault(locale);
 102  0
     }
 103  
 
 104  
     /**
 105  
      * Init the language configuration.
 106  
      *
 107  
      * @throws ManagerException If an error occurs during the state creation or loading.
 108  
      */
 109  
     private void initConfiguration() throws ManagerException {
 110  0
         state = Managers.getManager(IStateManager.class).getState(LanguageState.class);
 111  
 
 112  0
         if (state == null) {
 113  
             try {
 114  0
                 state = Managers.getManager(IStateManager.class).createState(LanguageState.class);
 115  0
                 state.setLanguage("fr");
 116  0
             } catch (StateException e) {
 117  0
                 throw new ManagerException(e);
 118  0
             }
 119  
         }
 120  0
     }
 121  
 
 122  
     @Override
 123  
     public void addBaseName(String baseName) {
 124  0
         if (baseName != null) {
 125  0
             getResourceBundle().addBaseName(baseName);
 126  
         }
 127  0
     }
 128  
 
 129  
     @Override
 130  
     public void removeBaseName(String baseName) {
 131  0
         getResourceBundle().removeBaseName(baseName);
 132  0
     }
 133  
 
 134  
     @Override
 135  
     public void setCurrentLanguage(String language) {
 136  0
         String shortForm = convertToShortForm(language);
 137  
 
 138  0
         state.setLanguage(shortForm);
 139  
 
 140  0
         setCurrentLocale(languages.get(shortForm));
 141  0
     }
 142  
 
 143  
     /**
 144  
      * Convert the language to his short form.
 145  
      *
 146  
      * @param language The language to convert.
 147  
      * @return The short form of the language.
 148  
      */
 149  
     private static String convertToShortForm(CharSequence language) {
 150  0
         if (StringUtils.isEmpty(language)) {
 151  0
             return "en";
 152  
         }
 153  
 
 154  0
         if ("Français".equals(language) || "fr".equals(language)) {
 155  0
             return "fr";
 156  0
         } else if ("Deutsch".equals(language) || "de".equals(language)) {
 157  0
             return "de";
 158  
         }
 159  
 
 160  0
         return "en";
 161  
     }
 162  
 
 163  
     /**
 164  
      * Set the current locale.
 165  
      *
 166  
      * @param locale The new current locale.
 167  
      */
 168  
     private void setCurrentLocale(Locale locale) {
 169  0
         this.locale = locale;
 170  
 
 171  0
         Locale.setDefault(locale);
 172  
 
 173  0
         refreshInternationalizables();
 174  0
     }
 175  
 
 176  
     /**
 177  
      * Refresh all the internationalizables elements.
 178  
      */
 179  
     private void refreshInternationalizables() {
 180  0
         for (Internationalizable internationalizable : internationalizables) {
 181  0
             internationalizable.refreshText();
 182  
         }
 183  0
     }
 184  
 
 185  
     @Override
 186  
     public void addInternationalizable(Internationalizable internationalizable) {
 187  0
         internationalizables.add(internationalizable);
 188  0
     }
 189  
 
 190  
     @Override
 191  
     public Locale getCurrentLocale() {
 192  0
         return locale;
 193  
     }
 194  
 
 195  
     @Override
 196  
     public String getCurrentLanguage() {
 197  0
         String language = null;
 198  
 
 199  0
         for (Map.Entry<String, Locale> entry : languages.entrySet()) {
 200  0
             if (entry.getValue().equals(locale)) {
 201  0
                 language = entry.getKey();
 202  0
                 break;
 203  
             }
 204  
         }
 205  
 
 206  0
         return language;
 207  
     }
 208  
 
 209  
     @Override
 210  
     public String getMessage(String key) {
 211  0
         if (StringUtils.isEmpty(key)) {
 212  0
             return StringUtils.EMPTY_STRING;
 213  
         }
 214  
 
 215  
         String message;
 216  
 
 217  
         try {
 218  0
             message = getMessageSource().getMessage(key, ArrayUtils.ZERO_LENGTH_ARRAY, locale);
 219  0
         } catch (NoSuchMessageException e) {
 220  0
             message = key;
 221  
 
 222  0
             Managers.getManager(ILoggingManager.class).getLogger(getClass()).warn(
 223  
                     "No message found for {} with locale {}", key, locale.getDisplayName());
 224  0
         }
 225  
 
 226  0
         return message;
 227  
     }
 228  
 
 229  
     @Override
 230  
     public String[] getLinesMessage(String key) {
 231  0
         String message = getMessage(key);
 232  
 
 233  0
         if (StringUtils.isEmpty(message)) {
 234  0
             return ZERO_LENGTH_ARRAY;
 235  
         }
 236  
 
 237  0
         Collection<String> tokens = new ArrayList<String>(5);
 238  
 
 239  0
         Scanner scanner = new Scanner(message);
 240  
 
 241  0
         while (scanner.hasNextLine()) {
 242  0
             tokens.add(scanner.nextLine());
 243  
         }
 244  
 
 245  0
         return tokens.toArray(new String[tokens.size()]);
 246  
     }
 247  
 
 248  
     @Override
 249  
     public String getMessage(String key, Object... replaces) {
 250  0
         if (StringUtils.isEmpty(key)) {
 251  0
             return StringUtils.EMPTY_STRING;
 252  
         }
 253  
 
 254  
         String message;
 255  
 
 256  
         try {
 257  0
             message = getMessageSource().getMessage(key, replaces, locale);
 258  0
         } catch (NoSuchMessageException e) {
 259  0
             message = key;
 260  
 
 261  0
             Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(
 262  
                     "No message found for {} with locale {}", key, locale.getDisplayName());
 263  0
         }
 264  
 
 265  0
         return message;
 266  
     }
 267  
 
 268  
     /**
 269  
      * Return the message source to use.
 270  
      *
 271  
      * @return The message source to use.
 272  
      */
 273  
     private static MessageSource getMessageSource() {
 274  0
         return Managers.getManager(IBeansManager.class).getApplicationContext();
 275  
     }
 276  
 
 277  
     @Override
 278  
     public Collection<String> getPossibleLanguages() {
 279  0
         Collection<String> languagesLong = new ArrayList<String>(2);
 280  
 
 281  0
         for (String supportedLanguage : Managers.getCore().getApplication().getSupportedLanguages()) {
 282  0
             if ("fr".equals(supportedLanguage)) {
 283  0
                 languagesLong.add("Français");
 284  0
             } else if ("en".equals(supportedLanguage)) {
 285  0
                 languagesLong.add("English");
 286  0
             } else if ("de".equals(supportedLanguage)) {
 287  0
                 languagesLong.add("Deutsch");
 288  
             }
 289  
         }
 290  
 
 291  0
         return languagesLong;
 292  
     }
 293  
 }