Allow a default locale to be specified for a message manager.
git-svn-id: https://samskivert.googlecode.com/svn/trunk@1672 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -46,10 +46,11 @@ public class MessageManager
|
|||||||
*
|
*
|
||||||
* @see java.util.ResourceBundle
|
* @see java.util.ResourceBundle
|
||||||
*/
|
*/
|
||||||
public MessageManager (String bundlePath)
|
public MessageManager (String bundlePath, Locale deflocale)
|
||||||
{
|
{
|
||||||
// keep this for later
|
// keep these for later
|
||||||
_bundlePath = bundlePath;
|
_bundlePath = bundlePath;
|
||||||
|
_deflocale = deflocale;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -169,7 +170,7 @@ public class MessageManager
|
|||||||
if (reportMissing) {
|
if (reportMissing) {
|
||||||
// if there's no translation for this path, complain about it
|
// if there's no translation for this path, complain about it
|
||||||
Log.warning("Missing translation message [path=" + path +
|
Log.warning("Missing translation message [path=" + path +
|
||||||
", url=" + req.getRequestURL() + "].");
|
", url=" + getURL(req) + "].");
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,8 +185,10 @@ public class MessageManager
|
|||||||
{
|
{
|
||||||
// first look to see if we've cached the bundles for this request
|
// first look to see if we've cached the bundles for this request
|
||||||
// in the request object
|
// in the request object
|
||||||
ResourceBundle[] bundles = (ResourceBundle[])
|
ResourceBundle[] bundles = null;
|
||||||
req.getAttribute(BUNDLE_CACHE_NAME);
|
if (req != null) {
|
||||||
|
bundles = (ResourceBundle[])req.getAttribute(BUNDLE_CACHE_NAME);
|
||||||
|
}
|
||||||
if (bundles != null) {
|
if (bundles != null) {
|
||||||
return bundles;
|
return bundles;
|
||||||
}
|
}
|
||||||
@@ -216,7 +219,7 @@ public class MessageManager
|
|||||||
getClass().getClassLoader());
|
getClass().getClassLoader());
|
||||||
|
|
||||||
// if we found either or both bundles, cache 'em
|
// if we found either or both bundles, cache 'em
|
||||||
if (bundles[0] != null || bundles[1] != null) {
|
if (bundles[0] != null || bundles[1] != null && req != null) {
|
||||||
req.setAttribute(BUNDLE_CACHE_NAME, bundles);
|
req.setAttribute(BUNDLE_CACHE_NAME, bundles);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -231,31 +234,33 @@ public class MessageManager
|
|||||||
HttpServletRequest req, String bundlePath, ClassLoader loader)
|
HttpServletRequest req, String bundlePath, ClassLoader loader)
|
||||||
{
|
{
|
||||||
ResourceBundle bundle = null;
|
ResourceBundle bundle = null;
|
||||||
Enumeration locales = req.getLocales();
|
|
||||||
|
|
||||||
while (locales.hasMoreElements()) {
|
if (req != null) {
|
||||||
Locale locale = (Locale)locales.nextElement();
|
Enumeration locales = req.getLocales();
|
||||||
|
while (locales.hasMoreElements()) {
|
||||||
|
Locale locale = (Locale)locales.nextElement();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// java caches resource bundles, so we don't need to
|
// java caches resource bundles, so we don't need to
|
||||||
// reinvent the wheel here. however, java also falls back
|
// reinvent the wheel here. however, java also falls back
|
||||||
// from a specific bundle to a more general one if it
|
// from a specific bundle to a more general one if it
|
||||||
// can't find a specific bundle. that's real nice of it,
|
// can't find a specific bundle. that's real nice of it,
|
||||||
// but we want first to see whether or not we have exact
|
// but we want first to see whether or not we have exact
|
||||||
// matches on any of the preferred locales specified by
|
// matches on any of the preferred locales specified by
|
||||||
// the client. if we don't, then we can rely on java's
|
// the client. if we don't, then we can rely on java's
|
||||||
// fallback mechanisms
|
// fallback mechanisms
|
||||||
bundle = ResourceBundle.getBundle(
|
bundle = ResourceBundle.getBundle(
|
||||||
bundlePath, locale, loader);
|
bundlePath, locale, loader);
|
||||||
|
|
||||||
// if it's an exact match, off we go
|
// if it's an exact match, off we go
|
||||||
if (bundle.getLocale().equals(locale)) {
|
if (bundle.getLocale().equals(locale)) {
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (MissingResourceException mre) {
|
||||||
|
// no need to freak out quite yet, see if we have
|
||||||
|
// something for one of the other preferred locales
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (MissingResourceException mre) {
|
|
||||||
// no need to freak out quite yet, see if we have
|
|
||||||
// something for one of the other preferred locales
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -263,27 +268,30 @@ public class MessageManager
|
|||||||
// preferred locales, take their most preferred and let java
|
// preferred locales, take their most preferred and let java
|
||||||
// perform it's fallback logic on that one
|
// perform it's fallback logic on that one
|
||||||
if (bundle == null) {
|
if (bundle == null) {
|
||||||
|
Locale locale = (req == null) ? _deflocale : req.getLocale();
|
||||||
try {
|
try {
|
||||||
bundle = ResourceBundle.getBundle(
|
bundle = ResourceBundle.getBundle(bundlePath, locale, loader);
|
||||||
bundlePath, req.getLocale(), loader);
|
|
||||||
|
|
||||||
} catch (MissingResourceException mre) {
|
} catch (MissingResourceException mre) {
|
||||||
// if we were unable even to find a default bundle, we've
|
// if we were unable even to find a default bundle, we've
|
||||||
// got real problems. time to freak out
|
// got real problems. time to freak out
|
||||||
Log.warning("Unable to resolve any message bundle " +
|
Log.warning("Unable to resolve any message bundle " +
|
||||||
"[req=" + req.getRequestURI() +
|
"[req=" + getURL(req) + ", locale=" + locale +
|
||||||
", locale=" + req.getLocale() +
|
|
||||||
", bundlePath=" + bundlePath +
|
", bundlePath=" + bundlePath +
|
||||||
", classLoader=" + loader +
|
", classLoader=" + loader +
|
||||||
", siteBundlePath=" + _siteBundlePath +
|
", siteBundlePath=" + _siteBundlePath +
|
||||||
", siteLoader=" + _siteLoader +
|
", siteLoader=" + _siteLoader + "].");
|
||||||
"].");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return bundle;
|
return bundle;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Helper function. */
|
||||||
|
protected String getURL (HttpServletRequest req)
|
||||||
|
{
|
||||||
|
return (req == null) ? "<none>" : req.getRequestURL().toString();
|
||||||
|
}
|
||||||
|
|
||||||
/** The path, relative to the classpath, to our resource bundles. */
|
/** The path, relative to the classpath, to our resource bundles. */
|
||||||
protected String _bundlePath;
|
protected String _bundlePath;
|
||||||
|
|
||||||
@@ -299,6 +307,9 @@ public class MessageManager
|
|||||||
* request was made. */
|
* request was made. */
|
||||||
protected SiteIdentifier _siteIdent;
|
protected SiteIdentifier _siteIdent;
|
||||||
|
|
||||||
|
/** The locale to use if we are accessed without an HTTP request. */
|
||||||
|
protected Locale _deflocale;
|
||||||
|
|
||||||
/** The attribute name that we use for caching resource bundles in
|
/** The attribute name that we use for caching resource bundles in
|
||||||
* request objects. */
|
* request objects. */
|
||||||
protected static final String BUNDLE_CACHE_NAME =
|
protected static final String BUNDLE_CACHE_NAME =
|
||||||
|
|||||||
@@ -102,7 +102,7 @@ public class Application
|
|||||||
// instantiate our message manager if the application wants one
|
// instantiate our message manager if the application wants one
|
||||||
String bundlePath = getInitParameter(config, MESSAGE_BUNDLE_PATH_KEY);
|
String bundlePath = getInitParameter(config, MESSAGE_BUNDLE_PATH_KEY);
|
||||||
if (!StringUtil.blank(bundlePath)) {
|
if (!StringUtil.blank(bundlePath)) {
|
||||||
_msgmgr = new MessageManager(bundlePath);
|
_msgmgr = new MessageManager(bundlePath, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
// if we have a site-specific resource loader, configure the
|
// if we have a site-specific resource loader, configure the
|
||||||
|
|||||||
Reference in New Issue
Block a user