📄 cmsmessages.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/org/opencms/i18n/CmsMessages.java,v $
* Date : $Date: 2006/03/27 14:53:01 $
* Version: $Revision: 1.23 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* For further information about Alkacon Software GmbH, please see the
* company website: http://www.alkacon.com
*
* For further information about OpenCms, please see the
* project website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.opencms.i18n;
import org.opencms.util.CmsDateUtil;
import org.opencms.util.CmsStringUtil;
import java.text.DateFormat;
import java.text.MessageFormat;
import java.util.Date;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
/**
* Reads localized resource Strings from a <code>java.util.ResourceBundle</code>
* and provides convenience methods to access the Strings from a template.<p>
*
* This class is frequently used from JSP templates. Because of that, throwing of
* exceptions related to the access of the resource bundle are suppressed
* so that a template always execute. The class provides an {@link #isInitialized()} method
* that can be checked to see if the instance was properly initialized.<p>
*
* @author Alexander Kandzior
*
* @version $Revision: 1.23 $
*
* @since 6.0.0
*/
public class CmsMessages {
/** The suffix of a "short" localized key name. */
public static final String KEY_SHORT_SUFFIX = ".short";
/** Prefix / Suffix for unknown keys. */
public static final String UNKNOWN_KEY_EXTENSION = "???";
/** The resource bundle base name this object was initialized with. */
private String m_bundleName;
/** The locale to use for looking up the messages from the bundle. */
private Locale m_locale;
/** The resource bundle this message object was initialized with. */
private ResourceBundle m_resourceBundle;
/**
* Constructor for the messages with an initialized <code>java.util.Locale</code>.
*
* @param bundleName the base ResourceBundle name
* @param locale the m_locale to use, eg. "de", "en" etc.
*/
public CmsMessages(String bundleName, Locale locale) {
try {
m_locale = locale;
m_bundleName = bundleName;
m_resourceBundle = CmsResourceBundleLoader.getBundle(bundleName, m_locale);
} catch (MissingResourceException e) {
m_resourceBundle = null;
}
}
/**
* Constructor for the messages with a language string.<p>
*
* The <code>language</code> is a 2 letter language ISO code, e.g. <code>"EN"</code>.<p>
*
* The Locale for the messages will be created like this:<br>
* <code>new Locale(language, "", "")</code>.<p>
*
* @param bundleName the base ResourceBundle name
* @param language ISO language indentificator for the m_locale of the bundle
*/
public CmsMessages(String bundleName, String language) {
this(bundleName, language, "", "");
}
/**
* Constructor for the messages with language and country code strings.<p>
*
* The <code>language</code> is a 2 letter language ISO code, e.g. <code>"EN"</code>.
* The <code>country</code> is a 2 letter country ISO code, e.g. <code>"us"</code>.<p>
*
* The Locale for the messages will be created like this:<br>
* <code>new Locale(language, country, "")</code>.
*
* @param bundleName the base ResourceBundle name
* @param language ISO language indentificator for the m_locale of the bundle
* @param country ISO 2 letter country code for the m_locale of the bundle
*/
public CmsMessages(String bundleName, String language, String country) {
this(bundleName, language, country, "");
}
/**
* Constructor for the messages with language, country code and variant strings.<p>
*
* The <code>language</code> is a 2 letter language ISO code, e.g. <code>"EN"</code>.
* The <code>country</code> is a 2 letter country ISO code, e.g. <code>"us"</code>.
* The <code>variant</code> is a vendor or browser-specific code, e.g. <code>"POSIX"</code>.<p>
*
* The Locale for the messages will be created like this:<br>
* <code>new Locale(language, country, variant)</code>.
*
* @param bundleName the base ResourceBundle name
* @param language language indentificator for the m_locale of the bundle
* @param country 2 letter country code for the m_locale of the bundle
* @param variant a vendor or browser-specific variant code
*/
public CmsMessages(String bundleName, String language, String country, String variant) {
this(bundleName, new Locale(language, country, variant));
}
/**
* Empty constructor for subclassing.<p>
*/
protected CmsMessages() {
// empty constructor for subclassing
}
/**
* Formats an unknown key.<p>
*
* @param keyName the key to format
* @return the formatted unknown key
*
* @see #isUnknownKey(String)
*/
public static String formatUnknownKey(String keyName) {
StringBuffer buf = new StringBuffer(64);
buf.append(UNKNOWN_KEY_EXTENSION);
buf.append(" ");
buf.append(keyName);
buf.append(" ");
buf.append(UNKNOWN_KEY_EXTENSION);
return buf.toString();
}
/**
* Returns <code>true</code> if the provided value matches the scheme
* <code>"??? " + keyName + " ???"</code>, that is the value appears to be an unknown key.<p>
*
* Also returns <code>true</code> if the given value is <code>null</code>.<p>
*
* @param value the value to check
* @return true if the value is matches the scheme for unknown keys
*
* @see #formatUnknownKey(String)
*/
public static boolean isUnknownKey(String value) {
return (value == null) || (value.startsWith(UNKNOWN_KEY_EXTENSION));
}
/**
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof CmsMultiMessages) {
return false;
}
if (obj instanceof CmsMessages) {
CmsMessages other = (CmsMessages)obj;
return other.getBundleName().equals(m_bundleName) && other.getLocale().equals(m_locale);
}
return false;
}
/**
* Returns the resource bundle this message object was initialized with.<p>
*
* @return the resource bundle this message object was initialized with or null if initialization was not successful
*/
public ResourceBundle getResourceBundle() {
return m_resourceBundle;
}
/**
* Returns a formated date String from a Date value,
* the format being {@link DateFormat#SHORT} and the locale
* based on this instance.<p>
*
* @param date the Date object to format as String
* @return the formatted date
*/
public String getDate(Date date) {
return CmsDateUtil.getDate(date, DateFormat.SHORT, m_locale);
}
/**
* Returns a formated date String from a Date value,
* the formatting based on the provided option and the locale
* based on this instance.<p>
*
* @param date the Date object to format as String
* @param format the format to use, see {@link CmsMessages} for possible values
* @return the formatted date
*/
public String getDate(Date date, int format) {
return CmsDateUtil.getDate(date, format, m_locale);
}
/**
* Returns a formated date String from a timestamp value,
* the format being {@link DateFormat#SHORT} and the locale
* based on this instance.<p>
*
* @param time the time value to format as date
* @return the formatted date
*/
public String getDate(long time) {
return CmsDateUtil.getDate(new Date(time), DateFormat.SHORT, m_locale);
}
/**
* Returns a formated date and time String from a Date value,
* the format being {@link DateFormat#SHORT} and the locale
* based on this instance.<p>
*
* @param date the Date object to format as String
* @return the formatted date and time
*/
public String getDateTime(Date date) {
return CmsDateUtil.getDateTime(date, DateFormat.SHORT, m_locale);
}
/**
* Returns a formated date and time String from a Date value,
* the formatting based on the provided option and the locale
* based on this instance.<p>
*
* @param date the Date object to format as String
* @param format the format to use, see {@link CmsMessages} for possible values
* @return the formatted date and time
*/
public String getDateTime(Date date, int format) {
return CmsDateUtil.getDateTime(date, format, m_locale);
}
/**
* Returns a formated date and time String from a timestamp value,
* the format being {@link DateFormat#SHORT} and the locale
* based on this instance.<p>
*
* @param time the time value to format as date
* @return the formatted date and time
*/
public String getDateTime(long time) {
return CmsDateUtil.getDateTime(new Date(time), DateFormat.SHORT, m_locale);
}
/**
* Returns the locale to use for looking up this messages.<p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -