View Javadoc

1   /**
2    * This file Copyright (c) 2013 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.i18nsystem;
35  
36  import info.magnolia.cms.i18n.MessagesManager;
37  import org.apache.commons.lang.StringUtils;
38  import org.slf4j.Logger;
39  import org.slf4j.LoggerFactory;
40  import javax.inject.Singleton;
41  import java.util.Locale;
42  import java.util.Properties;
43  import java.util.Arrays;
44  
45  /**
46   * A TranslationService which relies on a "global" message bundle, as loaded per {@link DefaultMessageBundlesLoader}.
47   * Each given key is looked up in the given locale, then in less specific locales, then in the fallback locale.
48   * That is, if [a, b, c] are passed as keys, with country-specific Locale such as de_CH, and "b" is defined in the de_CH
49   * message bundle, it will be returned, even if "a" is defined in the less specific "de" Locale.
50   *
51   * When a basename is explicitly specified, it however uses {@link MessagesManager} to look the keys up, falls back
52   * to the default bundle of MessagesManager, then finally falls back on its own global message bundle.
53   */
54  @Singleton
55  public class TranslationServiceImpl implements TranslationService {
56      private static final Logger log = LoggerFactory.getLogger(TranslationServiceImpl.class);
57      private DefaultMessageBundlesLoader messageBundles;
58  
59      public TranslationServiceImpl() {
60          this.messageBundles = setupMessageBundles();
61      }
62  
63      protected DefaultMessageBundlesLoader setupMessageBundles() {
64          return new DefaultMessageBundlesLoader();
65      }
66  
67      @Override
68      public String translate(LocaleProvider localeProvider, String[] keys) {
69          return translate(localeProvider, null, keys);
70      }
71  
72      @Override
73      public String translate(LocaleProvider localeProvider, String basename, String[] keys) {
74          final Locale locale = localeProvider.getLocale();
75          if (locale == null) {
76              throw new IllegalArgumentException("Locale can't be null");
77          }
78          if (keys == null || keys.length < 1) {
79              throw new IllegalArgumentException("Keys can't be null or empty");
80          }
81  
82          if (basename != null) {
83              log.debug("Got an explicit basename ({}) for keys {}", basename, Arrays.asList(keys));
84          }
85  
86          final String message = lookUpKeyUntilFound(keys, locale, basename);
87          if (message != null) {
88              return message;
89          } else {
90              return handleUnknownKey(locale, basename, keys);
91          }
92      }
93  
94      private String lookUpKeyUntilFound(final String[] keys, final Locale locale, final String basename) {
95          String message = null;
96  
97          if (StringUtils.isNotBlank(basename)) {
98              log.debug("Looking up key [{}] with basename [{}] and Locale [{}] - legacy method", keys[0], basename, locale);
99              message = legacyLookup(locale, basename, keys[0]);
100         }
101 
102         if (message == null) {
103             log.debug("Looking up in global i18n message bundle with key [{}] and Locale [{}]", Arrays.asList(keys), locale);
104             message = doGetMessage(keys, locale);
105         }
106 
107         if (message == null) {
108             final String country = locale.getCountry();
109             if (country != null) {
110                 final Locale newLocale = new Locale(locale.getLanguage(), country);
111                 message = doGetMessage(keys, newLocale);
112             }
113         }
114 
115         if (message == null) {
116             final Locale newLocale = new Locale(locale.getLanguage());
117             message = doGetMessage(keys, newLocale);
118         }
119 
120         if (message == null) {
121             message = doGetMessage(keys, getFallbackLocale());
122         }
123 
124         if (message == null) {
125             message = handleUnknownKey(locale, basename, keys);
126         }
127 
128         return message;
129     }
130 
131     protected String handleUnknownKey(Locale locale, String basename, String[] keys) {
132         // TODO - this method could be context dependent, or delegate to a configured component. In dev mode, for instance, we could at least print this out, or return the key, while in production this is neither useful nor needed.
133         if (log.isDebugEnabled()) {
134             log.debug("No translation found for any of {} with locale {} {}", Arrays.asList(keys), locale, (basename != null ? " and basename " + basename : ""));
135         }
136         return keys[0];
137     }
138 
139     /**
140      * Looks up a particular key using the given basename and Locale, using the legacy MessagesManager.
141      */
142     private String legacyLookup(Locale locale, String basename, String key) {
143         // Note that this internally chains the given locale with the fallback locale (as known by the MessagesManager), so if a key is known in english, it will be returned in english before we lookup in the default bundles
144         String message = MessagesManager.getMessages(basename, locale).get(key);
145         if (legacyMessageNotFound(message)) {
146             message = MessagesManager.getMessages(MessagesManager.DEFAULT_BASENAME, locale).get(key);
147         }
148         if (legacyMessageNotFound(message)) {
149             // Let's not get any of the legacy "???" markers out of here
150             return null;
151         } else {
152             return message;
153         }
154     }
155 
156     private boolean legacyMessageNotFound(final String message) {
157         return message == null || message.startsWith("???");
158     }
159 
160     private String doGetMessage(String[] keys, Locale locale) {
161         final Properties properties = messageBundles.getMessages().get(locale);
162         if (properties != null) {
163             for (String key : keys) {
164                 final String message = properties.getProperty(key);
165                 if (message != null) {
166                     return message;
167                 }
168             }
169         }
170         return null;
171     }
172 
173     // TODO - move this to a SystemLocalesManager component
174     private Locale getFallbackLocale() {
175         return MessagesManager.getInstance().getDefaultLocale();
176     }
177 
178     @Override
179     public void reloadMessageBundles() {
180         log.info("Reloading message bundles");
181         this.messageBundles = setupMessageBundles();
182     }
183 }