类 ResourceBundleMessageSource
- 所有已实现的接口:
Aware,BeanClassLoaderAware,HierarchicalMessageSource,MessageSource
MessageSource implementation that
accesses resource bundles using specified basenames. This class relies
on the underlying JDK's ResourceBundle implementation,
in combination with the JDK's standard message parsing provided by
MessageFormat.
This MessageSource caches both the accessed ResourceBundle instances and
the generated MessageFormats for each message. It also implements rendering of
no-arg messages without MessageFormat, as supported by the AbstractMessageSource
base class. The caching provided by this MessageSource is significantly faster
than the built-in caching of the java.util.ResourceBundle class.
The basenames follow ResourceBundle conventions: essentially,
a fully-qualified classpath location. If it doesn't contain a package qualifier
(such as org.mypackage), it will be resolved from the classpath root.
Note that the JDK's standard ResourceBundle treats dots as package separators:
This means that "test.theme" is effectively equivalent to "test/theme".
On the classpath, bundle resources will be read with the locally configured
encoding: by default, ISO-8859-1; consider switching
this to UTF-8, or to null for the platform default encoding. On the JDK 9+
module path where locally provided ResourceBundle.Control handles are not
supported, this MessageSource always falls back to ResourceBundle.getBundle(java.lang.String)
retrieval with the platform default encoding: UTF-8 with a ISO-8859-1 fallback on
JDK 9+ (configurable through the "java.util.PropertyResourceBundle.encoding" system
property). Note that loadBundle(Reader)/loadBundle(InputStream)
won't be called in this case either, effectively ignoring overrides in subclasses.
Consider implementing a JDK 9 java.util.spi.ResourceBundleProvider instead.
- 作者:
- Rod Johnson, Juergen Hoeller, Qimiao Chen
- 另请参阅:
-
字段概要
从类继承的字段 cn.taketoday.context.support.MessageSourceSupport
logger -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明protected ResourceBundledoGetBundle(String basename, Locale locale) Obtain the resource bundle for the given basename and Locale.protected ClassLoaderReturn the ClassLoader to load resource bundles with.protected MessageFormatgetMessageFormat(ResourceBundle bundle, String code, Locale locale) Return a MessageFormat for the given bundle and code, fetching already generated MessageFormats from the cache.protected ResourceBundlegetResourceBundle(String basename, Locale locale) Return a ResourceBundle for the given basename and Locale, fetching already generated ResourceBundle from the cache.protected StringgetStringOrNull(ResourceBundle bundle, String key) Efficiently retrieve the String value for the specified key, or returnnullif not found.protected ResourceBundleloadBundle(InputStream inputStream) Load a property-based resource bundle from the given input stream, picking up the default properties encoding on JDK 9+.protected ResourceBundleloadBundle(Reader reader) Load a property-based resource bundle from the given reader.protected MessageFormatresolveCode(String code, Locale locale) Resolves the given message code as key in the registered resource bundles, using a cached MessageFormat instance per message code.protected StringresolveCodeWithoutArguments(String code, Locale locale) Resolves the given message code as key in the registered resource bundles, returning the value found in the bundle as-is (without MessageFormat parsing).voidsetBeanClassLoader(ClassLoader classLoader) voidsetBundleClassLoader(ClassLoader classLoader) Set the ClassLoader to load resource bundles with.toString()Show the configuration of this MessageSource.从类继承的方法 cn.taketoday.context.support.AbstractResourceBasedMessageSource
addBasenames, getBasenameSet, getCacheMillis, getDefaultEncoding, getDefaultLocale, isFallbackToSystemLocale, setBasename, setBasenames, setCacheMillis, setCacheSeconds, setDefaultEncoding, setDefaultLocale, setFallbackToSystemLocale从类继承的方法 cn.taketoday.context.support.AbstractMessageSource
getCommonMessages, getDefaultMessage, getDefaultMessage, getMessage, getMessage, getMessage, getMessageFromParent, getMessageInternal, getParentMessageSource, isUseCodeAsDefaultMessage, resolveArguments, setCommonMessages, setParentMessageSource, setUseCodeAsDefaultMessage从类继承的方法 cn.taketoday.context.support.MessageSourceSupport
createMessageFormat, formatMessage, isAlwaysUseMessageFormat, renderDefaultMessage, setAlwaysUseMessageFormat
-
构造器详细资料
-
ResourceBundleMessageSource
public ResourceBundleMessageSource()
-
-
方法详细资料
-
setBundleClassLoader
Set the ClassLoader to load resource bundles with.Default is the containing BeanFactory's
bean ClassLoader, or the default ClassLoader determined byClassUtils.getDefaultClassLoader()if not running within a BeanFactory. -
getBundleClassLoader
Return the ClassLoader to load resource bundles with.Default is the containing BeanFactory's bean ClassLoader.
-
setBeanClassLoader
- 指定者:
setBeanClassLoader在接口中BeanClassLoaderAware
-
resolveCodeWithoutArguments
Resolves the given message code as key in the registered resource bundles, returning the value found in the bundle as-is (without MessageFormat parsing).- 覆盖:
resolveCodeWithoutArguments在类中AbstractMessageSource- 参数:
code- the code of the message to resolvelocale- the locale to resolve the code for (subclasses are encouraged to support internationalization)- 返回:
- the message String, or
nullif not found - 另请参阅:
-
resolveCode
Resolves the given message code as key in the registered resource bundles, using a cached MessageFormat instance per message code.- 指定者:
resolveCode在类中AbstractMessageSource- 参数:
code- the code of the message to resolvelocale- the locale to resolve the code for (subclasses are encouraged to support internationalization)- 返回:
- the MessageFormat for the message, or
nullif not found - 另请参阅:
-
getResourceBundle
Return a ResourceBundle for the given basename and Locale, fetching already generated ResourceBundle from the cache.- 参数:
basename- the basename of the ResourceBundlelocale- the Locale to find the ResourceBundle for- 返回:
- the resulting ResourceBundle, or
nullif none found for the given basename and Locale
-
doGetBundle
protected ResourceBundle doGetBundle(String basename, Locale locale) throws MissingResourceException Obtain the resource bundle for the given basename and Locale.- 参数:
basename- the basename to look forlocale- the Locale to look for- 返回:
- the corresponding ResourceBundle
- 抛出:
MissingResourceException- if no matching bundle could be found- 另请参阅:
-
loadBundle
Load a property-based resource bundle from the given reader.This will be called in case of a
"defaultEncoding", includingResourceBundleMessageSource's default ISO-8859-1 encoding. Note that this method can only be called with aResourceBundle.Control: When running on the JDK 9+ module path where such control handles are not supported, any overrides in custom subclasses will effectively get ignored.The default implementation returns a
PropertyResourceBundle.- 参数:
reader- the reader for the target resource- 返回:
- the fully loaded bundle
- 抛出:
IOException- in case of I/O failure- 另请参阅:
-
loadBundle
Load a property-based resource bundle from the given input stream, picking up the default properties encoding on JDK 9+.This will only be called with
"defaultEncoding"set tonull, explicitly enforcing the platform default encoding (which is UTF-8 with a ISO-8859-1 fallback on JDK 9+ but configurable through the "java.util.PropertyResourceBundle.encoding" system property). Note that this method can only be called with aResourceBundle.Control: When running on the JDK 9+ module path where such control handles are not supported, any overrides in custom subclasses will effectively get ignored.The default implementation returns a
PropertyResourceBundle.- 参数:
inputStream- the input stream for the target resource- 返回:
- the fully loaded bundle
- 抛出:
IOException- in case of I/O failure- 另请参阅:
-
getMessageFormat
@Nullable protected MessageFormat getMessageFormat(ResourceBundle bundle, String code, Locale locale) throws MissingResourceException Return a MessageFormat for the given bundle and code, fetching already generated MessageFormats from the cache.- 参数:
bundle- the ResourceBundle to work oncode- the message code to retrievelocale- the Locale to use to build the MessageFormat- 返回:
- the resulting MessageFormat, or
nullif no message defined for the given code - 抛出:
MissingResourceException- if thrown by the ResourceBundle
-
getStringOrNull
Efficiently retrieve the String value for the specified key, or returnnullif not found.the default implementation checks
containsKeybefore it attempts to callgetString(which would require catchingMissingResourceExceptionfor key not found).Can be overridden in subclasses.
- 参数:
bundle- the ResourceBundle to perform the lookup inkey- the key to look up- 返回:
- the associated value, or
nullif none - 另请参阅:
-
toString
Show the configuration of this MessageSource.
-