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