📄 cmsmessages.java
字号:
*
* @return the locale to use for looking up this messages
*/
public Locale getLocale() {
return m_locale;
}
/**
* Directly calls the getString(String) method of the wrapped ResourceBundle.<p>
*
* If you use this this class on a template, you should consider using
* the {@link #key(String)} method to get the value from the ResourceBundle because it
* handles the exception for you in a convenient way.
*
* @param keyName the key
* @return the resource string for the given key
*
* @throws CmsMessageException in case the key is not found or the bundle is not initialized
*/
public String getString(String keyName) throws CmsMessageException {
if (m_resourceBundle != null) {
try {
return m_resourceBundle.getString(keyName);
} catch (MissingResourceException e) {
throw new CmsMessageException(Messages.get().container(
Messages.ERR_CANT_FIND_RESOURCE_FOR_BUNDLE_2,
keyName,
m_bundleName));
}
} else {
throw new CmsMessageException(Messages.get().container(
Messages.ERR_MESSAGE_BUNDLE_NOT_INITIALIZED_1,
m_bundleName));
}
}
/**
* @see java.lang.Object#hashCode()
*/
public int hashCode() {
return m_locale.hashCode() + (m_bundleName == null ? 0 : m_bundleName.hashCode());
}
/**
* Checks if the bundle was properly initialized.
*
* @return <code>true</code> if bundle was initialized, <code>false</code> otherwise
*/
public boolean isInitialized() {
return (m_resourceBundle != null);
}
/**
* Returns the localized resource string for a given message key.<p>
*
* If the key was not found in the bundle, the return value is
* <code>"??? " + keyName + " ???"</code>. This will also be returned
* if the bundle was not properly initialized first.
*
* @param keyName the key for the desired string
* @return the resource string for the given key
*/
public String key(String keyName) {
return key(keyName, false);
}
/**
* Returns the localized resource string for a given message key.<p>
*
* If the key was not found in the bundle, the return value
* depends on the setting of the allowNull parameter. If set to false,
* the return value is always a String in the format
* <code>"??? " + keyName + " ???"</code>.
* If set to true, null is returned if the key is not found.
* This will also be returned
* if the bundle was not properly initialized first.
*
* @param keyName the key for the desired string
* @param allowNull if true, 'null' is an allowed return value
* @return the resource string for the given key
*/
public String key(String keyName, boolean allowNull) {
try {
if (m_resourceBundle != null) {
return m_resourceBundle.getString(keyName);
}
} catch (MissingResourceException e) {
// not found, return warning
if (allowNull) {
return null;
}
}
return formatUnknownKey(keyName);
}
/**
* Returns the selected localized message for the initialized resource bundle and locale.<p>
*
* Convenience method for messages with one argument.<p>
*
* @param key the message key
* @param arg0 the message argument
*
* @return the selected localized message for the initialized resource bundle and locale
*/
public String key(String key, Object arg0) {
return key(key, new Object[] {arg0});
}
/**
* Returns the selected localized message for the initialized resource bundle and locale.<p>
*
* Convenience method for messages with two arguments.<p>
*
* @param key the message key
* @param arg0 the first message argument
* @param arg1 the second message argument
*
* @return the selected localized message for the initialized resource bundle and locale
*/
public String key(String key, Object arg0, Object arg1) {
return key(key, new Object[] {arg0, arg1});
}
/**
* Returns the selected localized message for the initialized resource bundle and locale.<p>
*
* Convenience method for messages with three arguments.<p>
*
* @param key the message key
* @param arg0 the first message argument
* @param arg1 the second message argument
* @param arg2 the third message argument
*
* @return the selected localized message for the initialized resource bundle and locale
*/
public String key(String key, Object arg0, Object arg1, Object arg2) {
return key(key, new Object[] {arg0, arg1, arg2});
}
/**
* Returns the selected localized message for the initialized resource bundle and locale.<p>
*
* If the key was found in the bundle, it will be formatted using
* a <code>{@link MessageFormat}</code> using the provided parameters.<p>
*
* If the key was not found in the bundle, the return value is
* <code>"??? " + keyName + " ???"</code>. This will also be returned
* if the bundle was not properly initialized first.
*
* @param key the message key
* @param args the message arguments
*
* @return the selected localized message for the initialized resource bundle and locale
*/
public String key(String key, Object[] args) {
if ((args == null) || (args.length == 0)) {
// no parameters available, use simple key method
return key(key);
}
String result = key(key, true);
if (result == null) {
// key was not found
result = formatUnknownKey(key);
} else {
// key was found in the bundle - create and apply the formatter
MessageFormat formatter = new MessageFormat(result, m_locale);
result = formatter.format(args);
}
// return the result
return result;
}
/**
* Returns the localized resource string for a given message key.<p>
*
* If the key was not found in the bundle, the provided default value
* is returned.<p>
*
* @param keyName the key for the desired string
* @param defaultValue the default value in case the key does not exist in the bundle
* @return the resource string for the given key it it exists, or the given default if not
*/
public String keyDefault(String keyName, String defaultValue) {
String result = key(keyName, true);
return (result == null) ? defaultValue : result;
}
/**
* Returns the localized resource string for a given message key,
* treating all values appended with "|" as replacement parameters.<p>
*
* If the key was found in the bundle, it will be formatted using
* a <code>{@link MessageFormat}</code> using the provided parameters.
* The parameters have to be appended to the key separated by a "|".
* For example, the keyName <code>error.message|First|Second</code>
* would use the key <code>error.message</code> with the parameters
* <code>First</code> and <code>Second</code>. This would be the same as calling
* <code>{@link CmsMessages#key(String, Object[])}</code>.<p>
*
* If no parameters are appended with "|", this is the same as calling
* <code>{@link CmsMessages#key(String)}</code>.<p>
*
* If the key was not found in the bundle, the return value is
* <code>"??? " + keyName + " ???"</code>. This will also be returned
* if the bundle was not properly initialized first.
*
* @param keyName the key for the desired string, optinally containing parameters appended with a "|"
* @return the resource string for the given key
*
* @see #key(String, Object[])
* @see #key(String)
*/
public String keyWithParams(String keyName) {
if (keyName.indexOf('|') == -1) {
// no separator found, key has no parameters
return key(keyName, false);
} else {
// this key contains parameters
String[] values = CmsStringUtil.splitAsArray(keyName, '|');
String cutKeyName = values[0];
String[] params = new String[values.length - 1];
System.arraycopy(values, 1, params, 0, params.length);
return key(cutKeyName, params);
}
}
/**
* @see java.lang.Object#toString()
*/
public String toString() {
StringBuffer result = new StringBuffer();
result.append('[');
result.append(this.getClass().getName());
result.append(", baseName: ");
result.append(m_bundleName);
result.append(", locale: ");
result.append(getLocale());
result.append(']');
return result.toString();
}
/**
* Returns the name of the resource bundle this object was initialized with.<p>
*
* @return the name of the resource bundle this object was initialized with
*/
protected String getBundleName() {
return m_bundleName;
}
/**
* Sets the bundleName.<p>
*
* @param bundleName the bundleName to set
*/
protected void setBundleName(String bundleName) {
m_bundleName = bundleName;
}
/**
* Sets the locale.<p>
*
* @param locale the locale to set
*/
protected void setLocale(Locale locale) {
m_locale = locale;
}
/**
* Sets the resource bundle.<p>
*
* @param resourceBundle the resource bundle to set
*/
protected void setResourceBundle(ResourceBundle resourceBundle) {
m_resourceBundle = resourceBundle;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -