View Javadoc
1   /**
2    * This file Copyright (c) 2003-2015 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.freemarker;
35  
36  import info.magnolia.cms.i18n.Messages;
37  import info.magnolia.cms.i18n.MessagesChain;
38  import info.magnolia.cms.i18n.MessagesManager;
39  import info.magnolia.i18nsystem.FixedLocaleProvider;
40  import info.magnolia.i18nsystem.LocaleProvider;
41  import info.magnolia.i18nsystem.TranslationService;
42  import info.magnolia.i18nsystem.util.MessageFormatterUtils;
43  import info.magnolia.objectfactory.Components;
44  
45  import java.util.List;
46  import java.util.Locale;
47  
48  /**
49   * Utility class that has methods which allow passing multiple parameters
50   * from a freemarker template to a message string using the square bracket
51   * syntax (e.g. ${i18n.get('message', ['param1', 'param2']}). There are
52   * convenience methods which allow selecting the message bundle directly
53   * from within the template as well (by passing the basename parameter), but using those methods removed fallback to default message bundles so they should be used only when such custom bundle contains all messages requested in given context.
54   *
55   * @see info.magnolia.freemarker.FreemarkerHelperTest for more syntax details.
56   */
57  public class MessagesWrapper {
58  
59      private final TranslationService translationService;
60      private final String basename;
61      private final LocaleProvider localeProvider;
62  
63      @Deprecated
64      private MessagesChain messages;
65  
66  
67      MessagesWrapper(Locale locale, TranslationService translationService) {
68          this(null, locale, translationService);
69      }
70  
71      /**
72       * @deprecated since 5.4.4. You might still use this constructor if you need support for i18nBasename but the basename support will be removed in a future version. Use {@link #MessagesWrapper(java.util.Locale, info.magnolia.i18nsystem.TranslationService)} instead.
73       */
74      @Deprecated
75      MessagesWrapper(String basename, Locale locale, TranslationService translationService) {
76          this.translationService = translationService;
77          this.localeProvider = new FixedLocaleProvider(locale);
78          this.basename = basename;
79      }
80  
81      /**
82       * @deprecated since 5.4.4. Use {@link #MessagesWrapper(String, java.util.Locale, info.magnolia.i18nsystem.TranslationService)} instead.
83       */
84      @Deprecated
85      MessagesWrapper(String basename, Locale locale) {
86          this(basename, locale, Components.getComponent(TranslationService.class));
87      }
88  
89      /**
90       * We still use the deprecated method {@link TranslationService#translate(info.magnolia.i18nsystem.LocaleProvider, String, String[])} due to compatibility with obsolete i18nBasename, the method {@link TranslationService#translate(info.magnolia.i18nsystem.LocaleProvider, String[])} calls this method internally anyway.
91       */
92      public String get(String key) {
93          return translationService.translate(localeProvider, basename, new String[]{key});
94      }
95  
96      public String get(String key, List args) {
97          Object[] argsArray = new Object[args.size()];
98          return MessageFormatterUtils.format(this.get(key), localeProvider.getLocale(), args.toArray(argsArray));
99      }
100 
101     /**
102      * @deprecated since 5.4.4. Use {@link #get(String)} instead.
103      */
104     @Deprecated
105     public String get(String key, String basename) {
106         return translationService.translate(localeProvider, basename, new String[]{key});
107     }
108 
109     /**
110      * @deprecated since 5.4.4. Use {@link #get(String, java.util.List)} instead.
111      */
112     @Deprecated
113     public String get(String key, List args, String basename) {
114         final String translation = translationService.translate(localeProvider, basename, new String[]{key});
115         Object[] argsArray = new Object[args.size()];
116         return MessageFormatterUtils.format(translation, localeProvider.getLocale(), args.toArray(argsArray));
117     }
118 
119     /**
120      * @deprecated since 5.4.4. Use {@link #get(String, java.util.List)} instead.
121      */
122     @Deprecated
123     public String getWithDefault(String key, String defaultMsg) {
124         return this.getWithDefault(key, defaultMsg, this.getMessages());
125     }
126 
127     /**
128      * @deprecated since 5.4.4. Use {@link #get(String, java.util.List)} instead.
129      */
130     @Deprecated
131     public String getWithDefault(String key, String defaultMsg, String basename) {
132         return this.getWithDefault(key, defaultMsg, MessagesManager.getMessages(basename, localeProvider.getLocale()));
133     }
134 
135     /**
136      * @deprecated since 5.4.4. Use {@link #get(String, java.util.List)} instead.
137      */
138     @Deprecated
139     public String getWithDefault(String key, List args, String defaultMsg) {
140         return this.getWithDefault(key, args, defaultMsg, this.getMessages());
141     }
142 
143     /**
144      * @deprecated since 5.4.4. Use {@link #get(String, java.util.List)} instead.
145      */
146     @Deprecated
147     public String getWithDefault(String key, List args, String defaultMsg, String basename) {
148         return this.getWithDefault(key, args, defaultMsg, MessagesManager.getMessages(basename, localeProvider.getLocale()));
149     }
150 
151     /**
152      * @deprecated since 5.4.4. Use {@link #get(String)} instead.
153      */
154     @Deprecated
155     protected String get(String key, Messages messages) {
156         return messages.get(key);
157     }
158 
159     /**
160      * @deprecated since 5.4.4. Use {@link #get(String, java.util.List)} instead.
161      */
162     @Deprecated
163     protected String get(String key, List args, Messages messages) {
164         Object[] argsArray = new Object[args.size()];
165         return messages.get(key, args.toArray(argsArray));
166     }
167 
168     /**
169      * @deprecated since 5.4.4. Use {@link #get(String, java.util.List)} instead.
170      */
171     @Deprecated
172     protected String getWithDefault(String key, String defaultMsg, Messages messages) {
173         return messages.getWithDefault(key, defaultMsg);
174     }
175 
176     /**
177      * @deprecated since 5.4.4. Use {@link #get(String, java.util.List)} instead.
178      */
179     @Deprecated
180     protected String getWithDefault(String key, List args, String defaultMsg, Messages messages) {
181         Object[] argsArray = new Object[args.size()];
182         return messages.getWithDefault(key, args.toArray(argsArray), defaultMsg);
183     }
184 
185     private MessagesChain getMessages() {
186         if (messages == null) {
187             final Messages msg = MessagesManager.getMessages(basename, localeProvider.getLocale());
188             final Messages defMsg = MessagesManager.getMessages(localeProvider.getLocale());
189             messages = new MessagesChain(msg).chain(defMsg);
190         }
191         return messages;
192     }
193 }