1 /** 2 * This file Copyright (c) 2003-2013 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 * @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_legacy"; 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 * 105 * @param key key to find 106 * @param args replacement strings 107 * @return message 108 */ 109 public static String get(String key, Object[] args) { 110 return getMessages().get(key, args); 111 } 112 113 /** 114 * Use a default string. 115 * 116 * @param key key to find 117 * @param defaultMsg default message 118 * @return message 119 */ 120 public static String getWithDefault(String key, String defaultMsg) { 121 return getMessages().getWithDefault(key, defaultMsg); 122 } 123 124 /** 125 * Get a message with parameters inside: the value {0} must be a number. Use a default message. 126 * 127 * @param key key to find 128 * @param args replacement strings 129 * @param defaultMsg default message 130 * @return message 131 */ 132 public static String getWithDefault(String key, Object[] args, String defaultMsg) { 133 return getMessages().getWithDefault(key, args, defaultMsg); 134 } 135 136 private static Locale getCurrentLocale() { 137 return MgnlContext.getInstance().getLocale(); 138 } 139 140 public abstract void init(); 141 142 public abstract Collection getAvailableLocales(); 143 144 public abstract Locale getDefaultLocale(); 145 146 protected abstract Messages getMessagesInternal(String basename, Locale locale); 147 148 public abstract void reload(); 149 }