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