View Javadoc
1   /**
2    * This file Copyright (c) 2003-2016 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.cms.i18n;
35  
36  import info.magnolia.context.MgnlContext;
37  import info.magnolia.objectfactory.Components;
38  
39  import java.util.Collection;
40  import java.util.Locale;
41  
42  /**
43   * From this class you get the i18n messages. You should pass a request, but if you can't the getMessages method will
44   * handle it properly. The get() methods are easy to use.
45   *
46   * @see info.magnolia.i18nsystem
47   * @deprecated since 5.1.1. You should use {@code info.magnolia.i18nsystem} mechanism instead.
48   */
49  public abstract class MessagesManager {
50  
51      /**
52       * Use this locale if no other provided.
53       */
54      public static final String FALLBACK_LOCALE = "en";
55  
56      /**
57       * Use this basename if no other is provided.
58       */
59      public static final String DEFAULT_BASENAME = "info.magnolia.ui.admincentral.messages";
60  
61      /**
62       * The node name where the configuration for i18n is stored.
63       */
64      public static final String I18N_CONFIG_PATH = "/server/i18n/system";
65  
66      /**
67       * The name of the property to store the current system language.
68       */
69      public static final String FALLBACK_NODEDATA = "fallbackLanguage";
70  
71      /**
72       * Under this node all the available languages are stored. They are showed in the user dialog.
73       */
74      public static final String LANGUAGES_NODE_NAME = "languages";
75  
76      /**
77       * @deprecated since 4.5, use IoC !
78       */
79      public static MessagesManager getInstance() {
80          return Components.getSingleton(MessagesManager.class);
81      }
82  
83      public static Messages getMessages() {
84          return getMessages(null, getCurrentLocale());
85      }
86  
87      public static Messages getMessages(String basename) {
88          return getMessages(basename, getCurrentLocale());
89      }
90  
91      public static Messages getMessages(Locale locale) {
92          return getMessages(null, locale);
93      }
94  
95      public static Messages getMessages(String basename, Locale locale) {
96          return getInstance().getMessagesInternal(basename, locale);
97      }
98  
99      public static String get(String key) {
100         return getMessages().get(key);
101     }
102 
103     /**
104      * Get a message with parameters inside: the value {0} must be a number.
105      *
106      * @param key key to find
107      * @param args replacement strings
108      * @return the message
109      */
110     public static String get(String key, Object[] args) {
111         return getMessages().get(key, args);
112     }
113 
114     /**
115      * Use a default string.
116      *
117      * @param key key to find
118      * @param defaultMsg default message
119      * @return the message
120      */
121     public static String getWithDefault(String key, String defaultMsg) {
122         return getMessages().getWithDefault(key, defaultMsg);
123     }
124 
125     /**
126      * Get a message with parameters inside: the value {0} must be a number. Use a default message.
127      *
128      * @param key key to find
129      * @param args replacement strings
130      * @param defaultMsg default message
131      * @return the message
132      */
133     public static String getWithDefault(String key, Object[] args, String defaultMsg) {
134         return getMessages().getWithDefault(key, args, defaultMsg);
135     }
136 
137     private static Locale getCurrentLocale() {
138         return MgnlContext.getInstance().getLocale();
139     }
140 
141     public abstract void init();
142 
143     public abstract Collection getAvailableLocales();
144 
145     public abstract Locale getDefaultLocale();
146 
147     protected abstract Messages getMessagesInternal(String basename, Locale locale);
148 
149     public abstract void reload();
150 }