View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 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  
40  import java.util.List;
41  import java.util.Locale;
42  
43  /**
44   * Utility class that has methods which allow passing multiple parameters
45   * from a freemarker template to a message string using the square bracket
46   * syntax (e.g. ${i18n.get('message', ['param1', 'param2']}). There are
47   * convenience methods which allow selecting the message bundle directly
48   * from within the template as well (by passing the basename parameter).
49   *
50   * @see {info.magnolia.freemarker.FreemarkerHelperTest for more syntax details.
51   * @author vsteller
52   * @version $Revision: $ ($Author: $)
53   */
54  public class MessagesWrapper {
55      private final Messages messages;
56      private final Locale locale;
57  
58      MessagesWrapper(String basename, Locale locale) {
59          final Messages msg = MessagesManager.getMessages(basename, locale);
60          final Messages defMsg = MessagesManager.getMessages(locale);
61          this.messages = new MessagesChain(msg).chain(defMsg);
62          this.locale = locale;
63      }
64  
65      public String get(String key) {
66          return this.get(key, this.messages);
67      }
68  
69      public String get(String key, List args) {
70          return this.get(key, args, this.messages);
71      }
72  
73      // TODO : this behaves differently than the constructor: no fallback to default resource bundle
74      public String get(String key, String basename) {
75          return this.get(key, MessagesManager.getMessages(basename, locale));
76      }
77  
78      // TODO : this behaves differently than the constructor: no fallback to default resource bundle
79      public String get(String key, List args, String basename) {
80          return this.get(key, args, MessagesManager.getMessages(basename, locale));
81      }
82  
83      // TODO : not tested
84      public String getWithDefault(String key, String defaultMsg) {
85          return this.getWithDefault(key, defaultMsg, this.messages);
86      }
87  
88      // TODO : not tested
89      public String getWithDefault(String key, String defaultMsg, String basename) {
90          return this.getWithDefault(key, defaultMsg, MessagesManager.getMessages(basename, locale));
91      }
92  
93      // TODO : not tested
94      public String getWithDefault(String key, List args, String defaultMsg) {
95          return this.getWithDefault(key, args, defaultMsg, this.messages);
96      }
97  
98      // TODO : not tested
99      public String getWithDefault(String key, List args, String defaultMsg, String basename) {
100         return this.getWithDefault(key, defaultMsg, MessagesManager.getMessages(basename, locale));
101     }
102 
103     protected String get(String key, Messages messages) {
104         return messages.get(key);
105     }
106 
107     protected String get(String key, List args, Messages messages) {
108         Object[] argsArray = new Object[args.size()];
109         return messages.get(key, args.toArray(argsArray));
110     }
111 
112     protected String getWithDefault(String key, String defaultMsg, Messages messages) {
113         return messages.getWithDefault(key, defaultMsg);
114     }
115 
116     protected String getWithDefault(String key, List args, String defaultMsg, Messages messages) {
117         Object[] argsArray = new Object[args.size()];
118         return messages.getWithDefault(key, args.toArray(argsArray), defaultMsg);
119     }
120 }